[英]C++ Array with while loop
大家好,请检查此代码。 我想创建一个程序,在该程序中将提示我输入名字和姓氏,然后按时间顺序输出它,而不必为每个名字和姓氏创建变量。
#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;
}
您的数组应为字符串类型,其余代码应如下所示:
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;
}
在您自己的代码段中,您将打印数组的第一个元素10次,这将在x从0变为9时打印第x个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.