我刚刚输入了这个小问题,当我完成#34;学生No.1"的细节时,当它到第二个时,它会跳过这个名字。有帮助吗? 到目前为止,这是我的代码:
#include<stdlib.h>
#include<iostream>
#include <string>
using namespace std;
int main(){
int age[3];
double gpa[3];
string name[3], level[3];
cout<<"< < < < < Enter 3 Student Data > > > > >"<<endl;
for (int x=0; x<3; x++){
cout<<"-------------------------------"<<endl;
cout<<"Student No."<<x+1<<endl;
cout<<"Enter name: ";
getline(cin, name[x]);
cout<<"Enter your age: ";
cin>>age[x];
cout<<"Enter your GPA: ";
cin>> gpa[x];
do{
cout<<"Enter you grade level [freshman, sophomore, junior, or senior]: ";
cin>>level[x];
}while(level[x] != "freshman" && level[x] != "sophomore" && level[x] != "junior" && level[x] != "senior");
cout<<"-------------------------------"<<endl;
}
system("pause");
cout<<endl<<endl<<"< < < < < Enter 3 Student Datas > > > > >"<<endl;
for (int x=0; x<3; x++){
cout<<"-------------------------------"<<endl;
cout<<"Name: "<<name[x]<<endl<<"Age: "<<age[x]<<endl<<"GPA: "<<gpa[x]<<endl<<"Level: "<<level[x];
cout<<endl<<"-------------------------------"<<endl;
//system ("pause");
}
return 0;
}
答案 0 :(得分:0)
如果你混合使用cin和getline,你必须记住cin通常在键盘缓冲区中留下\ n字符。当getline检查缓冲区时,它会将\ n字符视为第一个并立即结束。在getline函数之前使用cin.ignore删除\ n字符。