菜鸟一个问题问两遍--郁闷

Emert_Hill 2009-06-20 08:25:23
#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
using namespace std;
struct car
{
string Manufacturer;
int ProductionYear;
};

cout << "How many cars do you wish to catalog ? ";
int n;
cin >> n;
car * cars = new car[n];
for(int i= 0;i < n; i++)
{
cout << "car #"<< i + 1 << ":\n" << "please enter the make:";
getline(cin,cars[i].Manufacturer); // 执行到此处,未等待输入,直接执行下一行,为什么?
cout << "please enter the year made:";
cin >> cars[i].ProductionYear;
};
cout << "Here is your collection:\n";
for(int i= 0;i < n; i++)
cout << cars[i].ProductionYear << "\t" << cars[i].Manufacturer << endl;
};
...全文
41 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangkunhenry 2009-06-20
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;
int main()
{
using namespace std;
struct car
{
string Manufacturer;
int ProductionYear;
};

cout << "How many cars do you wish to catalog ? ";
int n;
cin >> n;
cin.ignore(); //Here
car * cars = new car[n];
for(int i= 0;i < n; i++)
{
cout << "car #" << i + 1 << ":\n" << "please enter the make:";
getline(cin,cars[i].Manufacturer); // 执行到此处,未等待输入,直接执行下一行,为什么?
cout << "please enter the year made:";
cin >> cars[i].ProductionYear;
cin.ignore(); //Here
};
cout << "Here is your collection:\n";
for(int i= 0;i < n; i++)
cout << cars[i].ProductionYear << "\t" << cars[i].Manufacturer << endl;
};
//cin.ignore()这个方法用于将cin对象中缓冲区的部份字符丢弃,缺省状态是丢一个字符
//这个地方丢弃了你的回车符
lixing01 2009-06-20
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
using namespace std;
struct car
{
string Manufacturer;
int ProductionYear;
};

cout < < "How many cars do you wish to catalog ? ";
int n;
cin >> n; // cin获得输入的数据,换行符仍留在输入流中
car * cars = new car[n];
for(int i= 0;i < n; i++)
{
cout < < "car #" < < i + 1 < < ":\n" < < "please enter the make:";
//因为前面的cin获得输入后把换行符留在了输入流中,所以下面的getline相当于获得了一个空行
while (cin.get() != '\n') //先清除输入流中的所有字符,包括换行符
continue;

getline(cin,cars[i].Manufacturer); // 执行到此处,未等待输入,直接执行下一行,为什么?

cout < < "please enter the year made:";
cin >> cars[i].ProductionYear;
};
cout < < "Here is your collection:\n";
for(int i= 0;i < n; i++)
cout < < cars[i].ProductionYear < < "\t" < < cars[i].Manufacturer < < endl;
};
lixing01 2009-06-20
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
using namespace std;
struct car
{
string Manufacturer;
int ProductionYear;
};

cout < < "How many cars do you wish to catalog ? ";
int n;
cin >> n; // cin获得输入的数据,换行符仍留在输入流中
car * cars = new car[n];
for(int i= 0;i < n; i++)
{
cout < < "car #" < < i + 1 < < ":\n" < < "please enter the make:";
//因为前面的cin获得输入后把换行符留在了输入流中,所以下面的getline相当于获得了一个空行
while (cin.get() != '\n') //先清除输入流中的所有字符,包括换行符
continue;
getline(cin,cars[i].Manufacturer); // 执行到此处,未等待输入,直接执行下一行,为什么?

cout < < "please enter the year made:";
cin >> cars[i].ProductionYear;
};
cout < < "Here is your collection:\n";
for(int i= 0;i < n; i++)
cout < < cars[i].ProductionYear < < "\t" < < cars[i].Manufacturer < < endl;
};
Emert_Hill 2009-06-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 liao05050075 的回复:]
因为你的上一次输入之后,残留了一个回车下来。
之后你再getline就不会停,只读到一个回车
[/Quote]
谢谢啊,看来不实践即使看书看到了也想不起来啊,多谢你了!!!
liao05050075 2009-06-20
  • 打赏
  • 举报
回复
因为你的上一次输入之后,残留了一个回车下来。
之后你再getline就不会停,只读到一个回车
Emert_Hill 2009-06-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liao05050075 的回复:]
C/C++ code
#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
using namespace std;
struct car
{
string Manufacturer;
int ProductionYear;
};

cout << "How many cars do you wish to catalog ? ";
int n;
cin >> n;

car * cars = new car[n];
for(int i= 0;i < n; i++)
{
cout << "car #" << i + 1 << ":\n" << "please enter the make:";

[/Quote]
貌似这样也可以,能解释一下吗?谢谢!!
Emert_Hill 2009-06-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lingyin55 的回复:]
for(int i= 0;i < n; i++)
{
cout < < "car #" < < i + 1 < < endl < < "please enter the make:";
fflush(stdin);///
getline(cin,cars[i].Manufacturer); // 执行到此处,未等待输入,直接执行下一行,为什么?
cout < < "please enter the year made:";
fflush(stdin);////////////
cin >> cars[i].ProductionYear;
};
[/Quote]
确实见效,高手能给解释下吗?
liao05050075 2009-06-20
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <iostream>
#include <string>

int main()
{
using namespace std;
struct car
{
string Manufacturer;
int ProductionYear;
};

cout << "How many cars do you wish to catalog ? ";
int n;
cin >> n;

car * cars = new car[n];
for(int i= 0;i < n; i++)
{
cout << "car #" << i + 1 << ":\n" << "please enter the make:";
getchar();getchar();
getline(cin,cars[i].Manufacturer); // 执行到此处,未等待输入,直接执行下一行,为什么?
cout << "please enter the year made:";
cin >> cars[i].ProductionYear;
};
cout << "Here is your collection:\n";
for(i= 0;i < n; i++)
cout << cars[i].ProductionYear << "\t" << cars[i].Manufacturer << endl;
};
lingyin55 2009-06-20
  • 打赏
  • 举报
回复
for(int i= 0;i < n; i++)
{
cout << "car #" << i + 1 << endl << "please enter the make:";
fflush(stdin);///
getline(cin,cars[i].Manufacturer); // 执行到此处,未等待输入,直接执行下一行,为什么?
cout << "please enter the year made:";
fflush(stdin);////////////
cin >> cars[i].ProductionYear;
};
Emert_Hill 2009-06-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lingyin55 的回复:]
for(int i= 0;i < n; i++)
{
cout < < "car #" < < i + 1 < < ":\n" < < "please enter the make:";
fflush(stdin);///加个清空缓冲的操作试试
getline(cin,cars[i].Manufacturer); // 执行到此处,未等待输入,直接执行下一行,为什么?
cout < < "please enter the year made:";
cin >> cars[i].ProductionYear;
};
[/Quote]
貌似无效啊!!!
lingyin55 2009-06-20
  • 打赏
  • 举报
回复
for(int i= 0;i < n; i++)
{
cout < < "car #" < < i + 1 < < ":\n" < < "please enter the make:";
fflush(stdin);///加个清空缓冲的操作试试
getline(cin,cars[i].Manufacturer); // 执行到此处,未等待输入,直接执行下一行,为什么?
cout < < "please enter the year made:";
cin >> cars[i].ProductionYear;
};

65,210

社区成员

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

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