【问题标题】:Why my C++ code is not taking all inputs before execution? It just take a input and print all cout content [duplicate]为什么我的 C++ 代码在执行前没有接受所有输入?它只需要输入并打印所有 cout 内容[重复]
【发布时间】: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 . . . 

如果您对我的代码技能有疑问,我还想给您一些有用的建议

【问题讨论】:

标签: c++ loops for-loop structure


【解决方案1】:

问题是:

  • 电话号码太大,无法在此环境中放入int。它应该存储为字符串。
  • &gt;&gt; cin 的运算符在流中留下换行符,因此下一个 cin.get() 将读取并停在那里。您可以阅读直到换行符并删除通过cin.ignore() 阅读的内容。请注意,cin.get() 也会留下换行符,但 &gt;&gt; 运算符请忽略这一点。

试试这个:

#include <iostream>
#include <limits>
using namespace std;
struct person
{
    char name[30]; 
    int age;
    char phone_no[30];
    
}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;
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        cout<<"\n       Enter your phone no : ";
        cin.get(p[i].phone_no,30);
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    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;

}

【讨论】:

  • 感谢您的回答和时间,我是初学者,正在寻找该问题的解决方案并重定向到该网站我找不到我的答案所以发布我的问题,再次感谢教我
【解决方案2】:

啊,我真的不知道为什么,但是 c++ 对 cin.get(); 有问题,它对 cin 工作正常:

#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>>p[i].name;
    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;

} 

【讨论】:

  • 您可以尝试使用 scanf() 而不是 cin 来处理带有空格的字符串,它们从来都不是麻烦的。
  • 这段代码没有解决int的范围问题。
  • 另外cin&gt;&gt;p[i].name;在这段代码中可能会导致缓冲区溢出,而cin.get(p[i].name,30);不会。
  • @SooryaSRajan 更不用说cin&gt;&gt;p[i].name 会因Ram Ramanujian 之类的输入而失败。
猜你喜欢
  • 2021-03-05
  • 2017-03-02
  • 2023-02-03
  • 2021-12-24
  • 1970-01-01
  • 2021-08-28
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多