C++循环输入结构体元素为什么会出错?

德赛法 2020-05-29 12:52:48
输入:aa aa,导致后面无法输入,且无法正常显示,尤其是循环3.原因是什么?
我知道是cin的缘故,但是不理解为什么是cin。我改成cin.getline,又会有产生新的问题。


代码:

// structur.cpp -- a simple structure
#include <iostream>
#include <cstring>
struct inflatable // structure declaration
{
char name[20];
float volume;
double price;
};

int main()
{
using namespace std;
inflatable guest =
{
"Glorious Gloria", // name value
1.88, // volume value
29.99 // price value
}; // guest is a structure variable of type inflatable
// It's initialized to the indicated values
inflatable pal =
{
"Audacious Arthur",
3.12,
32.99
}; // pal is a second variable of type inflatable
// NOTE: some implementations require using
// static inflatable guest =

cout << "Expand your guest list with " << guest.name;
cout << " and " << pal.name << "!\n";
// pal.name is the name member of the pal variable
cout << "You can have both for $";
cout << guest.price + pal.price << "!\n";
// cin.get();

// my structure array test
inflatable guest_array[3];
for (int i = 0; i < 3; i++)
{
cout << "Please input name: " << endl;
// cin.getline(guest_array[i].name, 20);
cin >> guest_array[i].name;

cout << "Please input volume: " << endl;
cin >> guest_array[i].volume;

cout << "Please input price: " << endl;
cin >> guest_array[i].price;

cout << "name: " << guest_array[i].name << endl;
cout << "volume: " << guest_array[i].volume << endl;
cout << "price: " << guest_array[i].price << endl;
}


return 0;
}


...全文
249 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
源代码大师 2021-05-06
  • 打赏
  • 举报
回复
希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html 希望对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10768339.html
德赛法 2020-05-29
  • 打赏
  • 举报
回复
引用 1 楼 Simple-Soft 的回复:
每输入一个数据,就按下回车
截图中循环3不理解是怎么回事。
Simple-Soft 2020-05-29
  • 打赏
  • 举报
回复
每输入一个数据,就按下回车
德赛法 2020-05-29
  • 打赏
  • 举报
回复
不考虑这个问题了,我在另一个帖子中搞清了“c++如何保证输入始终是数字而不是字符”,循环输入数字或者字符才是我的目的。 https://bbs.csdn.net/topics/396698161 最终代码:
// my_structur.cpp -- 创建结构体数组,循环输入结构体元素。
// 1 创建结构体;
// 2 创建结构体数组;
// 3 循环输入结构体元素时保证始终输入字符串或数字;
//  3.1 while (1) {}
#include <iostream>
#include <cstring>
struct inflatable // structure declaration
{
    char name[20];
    float volume;
    double price;
};

int main()
{
    using namespace std;
    // my structure array test
    inflatable guest_array[3];
    for (int i = 0; i < 3; i++)
    {
        cout << "Please input name: " << endl;
        // cin.getline(guest_array[i].name, 20);
        // cin>> noskipws >> guest_array[i].name;

        cin.get(guest_array[i].name, 20);

        cout << "Please input volume: " << endl;
        while (!(cin >> guest_array[i].volume))
        {
            cout << "无效输入! 请重新输入数字:" << endl;
            cin.clear();
            cin.ignore(1024, '\n');
        }

        // while (!scanf("%d", &guest_array[i].volume) || guest_array[i].volume < 0)
        // {
        //     printf("无效输入!请重新输入:\n");
        //     scanf("%*[^\n]");
        //     scanf("%*c");
        // }

        cin.get();

        cout << "Please input price: " << endl;
        cin >> guest_array[i].price;
        cin.get();

        cout << "name" << i << ": " << guest_array[i].name << endl;
        cout << "volume: " << guest_array[i].volume << endl;
        cout << "price: " << guest_array[i].price << endl;
    }

    return 0;
}
德赛法 2020-05-29
  • 打赏
  • 举报
回复
引用 3 楼 Simple-Soft 的回复:
你调试一下就知道了
就是因为调试不明白才发帖的
Simple-Soft 2020-05-29
  • 打赏
  • 举报
回复
你调试一下就知道了

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧