德赛法 2020-05-29 00:20 采纳率: 0%
浏览 74

为什么我的循环输入结构体时其内部元素无法全部显示?

1.描述问题:空格键造成cin不会显示其它好理解,但是循环3中为什么只显示了name,而没有volume和price?

2.代码:

// 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; 
}

3.截图

图片说明

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2020-09-15 13:46
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失