【发布时间】:2021-01-12 19:11:45
【问题描述】:
这是我的代码和结果 我只是先创建一个结构,然后设置一个 for 循环来使用该元素并从用户那里获取输入并在代码部分的最后显示。 请帮帮我
#include <iostream>
using namespace std;
struct person
{
char name[30];
int age;
int phone_no;
}p[5];
int main()
{
for (int i=0; i<5; ++i)
{
cout<<"Enter you details \n\n\n";
cout<<" Enter your name : ";
cin.get(p[i].name,30);
cout<<"\n Enter your age : ";
cin>>p[i].age;
cout<<"\n Enter your phone no : ";
cin>>p[i].phone_no;
}
for (int i=0; i<5; ++i)
{
cout<<"\n\n\n Person's name : "<<p[i].name<<"\n Age : "<<p[i].age<<"\n Phone no : "<<p[i].phone_no;
}
return 0;
}
结果
Enter you details
Enter your name : Ram
Enter your age : 12
Enter your phone no : 9898874645
Enter you details
Enter your name :
Enter your age :
Enter your phone no : Enter you details
Enter your name :
Enter your age :
Enter your phone no : Enter you details
Enter your name :
Enter your age :
Enter your phone no : Enter you details
Enter your name :
Enter your age :
Enter your phone no :
Person's name : Ram
Age : 12
Phone no : 2147483647
Person's name :
Age : 0
Phone no : 0
Person's name :
Age : 0
Phone no : 0
Person's name :
Age : 0
Phone no : 0
Person's name :
Age : 0
Phone no : 0
Process exited after 12.16 seconds with return value 0
Press any key to continue . . .
如果您对我的代码技能有疑问,我还想给您一些有用的建议
【问题讨论】:
-
1.电话号码太大,无法存储在
int中。它应该存储为字符串。 -
@MikeCAT "2. ..." 不,问题(解决方案)被描述为here。你建议的那个只与
std::getline()一起出现。
标签: c++ loops for-loop structure