新手学习C++ primer plus 遇到的问题,高手指教。。

i诙谐 2011-10-16 06:48:35
#include<iostream>
#include<string>
using namespace std;
const int Max=30;
struct car
{
string make;
int year_made;
};

void main()
{
int num_car;int i=0;
cout<<" How many cars do you wish to catolog?____\b\b";
cin>>num_car;
car *c=new car [num_car];
for(i=0;i<num_car;i++)
{
cout<<"Car #"<<(i+1)<<":"<<endl;
cout<<"Please enter the make:";
getline(cin,c[i].make); //// 问题在这行
//整行输入存在错误。。要怎样修改才行a ?
//char make[]我也试过,用cin.get(c[i].make,30).get()等都不行,

cout<<"Please enter the year made:";
cin>>c[i].year_made;//cin.get(c[i].year_made,Max);
}
////////
cout<<"\n\aHere is your collection:"<<endl;
for(i=0;i<num_car;i++)
{
cout<<c[i].year_made<<" "<<c[i].make<<endl;
}
delete []c;
/////////

}
...全文
108 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
i诙谐 2011-10-17
  • 打赏
  • 举报
回复
谢谢你们了,楼主找到一个好的解决方法了。。

#include<iostream>
#include<string>
using namespace std;
const int Max=30;
struct car
{
char make[Max];
int year_made;
};

void main()
{
int num_car;int i=0;
cout<<" How many cars do you wish to catolog?____\b\b";
cin>>num_car;
while(cin.get() != '\n') //①
;

car *c=new car [num_car];
for(i=0;i<num_car;i++)
{
cout<<"Car #"<<(i+1)<<":"<<endl;
cout<<"Please enter the make:";
cin.getline(c[i].make,20); //// 整行输入存在错误。。
cout<<"Please enter the year made:";
cin>>c[i].year_made;//cin.get(c[i].year_made,Max);
while(cin.get()!='\n')
;
//②
}
////////
cout<<"\n\aHere is your collection:"<<endl;
for(i=0;i<num_car;i++)
{
cout<<c[i].year_made<<" "<<c[i].make<<endl;
}
delete []c;
/////////

}
i诙谐 2011-10-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 mougaidong 的回复:]

C/C++ code
#include<iostream>
#include<string>


void main()
{
std::string buffer_str;
std::getline(std::cin, buffer_str);
std::cout << buffer_str << std::endl;
}


实践证明这段代码,输入空格是没有问题的……
[/Quote]
谢谢,之前我试过这种,getline(cin,str)可以运行成功;但是换成getline(cin,c[i].make)就不行,让人很DT;
turing-complete 2011-10-16
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>


void main()
{
std::string buffer_str;
std::getline(std::cin, buffer_str);
std::cout << buffer_str << std::endl;
}


实践证明这段代码,输入空格是没有问题的,LZ可以亲试
i诙谐 2011-10-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 linwhwylb 的回复:]

引用楼主 wgw274607900 的回复:
#include<iostream>
#include<string>
using namespace std;
const int Max=30;
struct car
{
string make;
int year_made;
};

void main()
{
int num_car;int i=0;
cout<<"……
[/Quote]
string 试过了,不行。主要是那一行怎样解决整行输入的问题。。
i诙谐 2011-10-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mougaidong 的回复:]

C/C++ code
#include<iostream>
#include<string>
using namespace std;
const int Max=30;
struct car
{
string make;
int year_made;
};

void main()
{
int num_car;int i=0;
cout<<" How m……
[/Quote]

如果make是分开的名字就行不通了,比如Had Sun这种..
turing-complete 2011-10-16
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
using namespace std;
const int Max=30;
struct car
{
string make;
int year_made;
};

void main()
{
int num_car;int i=0;
cout<<" How many cars do you wish to catolog?____\b\b";
cin>>num_car;
car *c=new car [num_car];
for(i=0;i<num_car;i++)
{
cout<<"Car #"<<(i+1)<<":"<<endl;
cout<<"Please enter the make:";

cin >> c[i].make;// 这么做不可以?

cout<<"Please enter the year made:";
cin>>c[i].year_made;
}
////////
cout<<"\n\aHere is your collection:"<<endl;
for(i=0;i<num_car;i++)
{
cout<<c[i].year_made<<" "<<c[i].make<<endl;
}
delete []c;
/////////

}
Linux-Torvalds 2011-10-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 wgw274607900 的回复:]
#include<iostream>
#include<string>
using namespace std;
const int Max=30;
struct car
{
string make;
int year_made;
};

void main()
{
int num_car;int i=0;
cout<<" How many cars do you wis……
[/Quote]换成string类型。

64,662

社区成员

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

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