Create a c++ program that asks the user to input the full name 10 times using for, do while and while loop. Then display.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name[10];
int i;
cout << "Input the full name 10 times: " << endl;
//1. for. storing information
for( i = 0; i < 10; ++i) {
cout<< i+1 <<". ";
getline(cin, name[i]);
}
// display information
cout<<endl<<"Names: "<<endl;
for( i = 0; i < 10; ++i)
cout<< name[i]<<endl;
//2. do while. storing information
cout << "Input the full name 10 times: " << endl;
i=0;
do{
cout<< i+1 <<". ";
getline(cin, name[i]);
i++;
} while (i<10);
// display information
i=0;
cout<<endl<<"Names: "<<endl;
do{
cout<< name[i]<<endl;
i++;
} while (i<10);
//3. while. storing information
cout << "Input the full name 10 times: " << endl;
i=0;
while(i<10){
cout<< i+1 <<". ";
getline(cin, name[i]);
i++;
}
// display information
i=0;
cout<<endl<<"Names: "<<endl;
while(i<10){
cout<< name[i]<<endl;
i++;
}
return 0;
}
Comments
Leave a comment