关于cin.get()与cin,以及换行符问题
猫头鹰与猫 2013-08-04 10:59:09 程序
#include <iostream>
using namespace std;
struct user
{
char name[20];
char volume[20];
double price;
};
int main()
{
user* ps = new user;
cout<<"Input Your Name:";
cin.get(ps->name,20);
cout<<"Enter volume in cubic feet:";
//cin.get(); 如果加上这一句, 程序就没有问题
cin.get((*ps).volume,20);
cout<<"Enter price:";
cin>>(*ps).price;
cout<<"Hello,"<<ps->name<<",Your Volume is "<<ps->volume<<" with "<<ps->price<<" Dollars."<<endl;
}
按理说第一次使用完cin.get()后换行符没有被抛弃,那么换行符被第二次的cin.get()给接受了,第三次的 cin>>(*ps).price;应该可以正常使用才对。
但实际上只有第一次 cin.get(ps->name,20);起到了作用,
剩下的cin.get((*ps).volume,20);和cin>>(*ps).price;
都被跳过了,怎么回事?