[英]C++ Array with while loop
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;
}
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. 在您自己的代码段中,您将打印数组的第一个元素10次,这将在x从0变为9时打印第x个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.