mybangbangtang 2017-04-30 09:45 采纳率: 0%
浏览 854

关于c++输入流的问题,百度后仍然无法解决我的问题

 #include<iostream>
#include<ctime>
using namespace std;

struct Car
{
    char name[500];
    int year;
};
int main()
{
    int num;
    //int ch;
    cout << "how many cars do you wish to catalog?";
    cin >> num;
    //ch=cin.get();
    //cout << ch << endl;
    //while (cin.get() != '\n');
    Car *car = new Car[num];
    for (int i = 0; i < num; i++)
    { 
        cout << "Car #" << i + 1 << ":" << endl;
        cout << "please enter the make: ";
        cin.get(car[i].name,500);
        //cin.get();
        //cin.getline(car[i].name, 500).get();
        cout << "please enter the year made: ";
        cin >> car[i].year;
        //cin.get();
        //while (cin.get() != '\n');
    }
    cout << "hear is you collection: " << endl;
    for (int i = 0; i < num; i++)
        cout << car[i].year << " " << car[i].name << endl;
    delete[]car;
    return 0;
}

以上注释是问题的解决方案,使用cin.get()就可以解决问题,我会用,但是我不知道原理以及为什么出错

图片说明

  • 写回答

6条回答 默认 最新

  • devmiao 2017-05-01 04:56
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置