Reputation: 27
Hi guys please check this code. I want to create a program in which I will be prompted to enter first name and last name then output it in chronological order without having to create variable for each first name and last name.
#include<iostream>
using namespace std;
int main(){
int fname[9];
int lname[9];
int x;
while (x < 10){
cout<<"Enter first name: ";
cin>>fname[0];
cout<<"Enter last name: ";
cin>>lname[0];
x = x + 10;
}
x = 0;
while (x < 10){
cout<<fname[0]<<" "<<lname[0]<<"\n";
x = x + 1;
}
return 0;
}
Upvotes: 1
Views: 2264
Reputation: 61
Your arrays should be type strings, rest of the code should look like this:
for(int x=0;x<10;x++) {
cout<<"Enter first name: ";
cin>>fname[x];
count<<"Enter last name: ";
cin>>lname[x];
}
for(x=0;x<10;x++){
cout<<fname[x]<<" "<<lname[x]<<"\n";
}
return 0;
}
In your own snippet you're printing the first element of your arrays 10 times, this will print the x-th element while x goes from 0 to 9.
Upvotes: 1