【发布时间】:2012-02-01 15:35:30
【问题描述】:
大家好,我正在做关于结构化数据的编程作业,我相信我了解结构的工作原理。
我正在尝试读取学生姓名、身份证号(A-Numbers)及其余额的列表。
不过,当我编译我的代码时,它会在第一次读取所有内容,但第二次循环时,每次之后,它都会提示输入用户名但跳过 getline 并直接进入 A-Number 和 A -数字条目。
任何帮助将不胜感激。只是想弄清楚每次循环时如何使 getline 工作。
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main(){
const int maxStudents = 30;
struct Students{
string studentName;
int aNumber;
double outstandingBalance;};
Students students[maxStudents];
for(int count = 0; count < maxStudents-1; count++)
{
cout<<"Student Name:";
cin.ignore();
getline(cin,students[count].studentName);
cout<<"\nA-Number:";
cin>>students[count].aNumber;
if(students[count].aNumber == -999)
break;
cout<<"\nOutstanding Balance:";
cin>>students[count].outstandingBalance;
}
cout<<setw(20)<<"A-Number"<<"Name"<<"Balance";
for(int count2 = 29; count2 >= maxStudents-1; count2--)
cout<<setw(20)<<students[count2].aNumber<<students[count2].studentName<<students[count2].outstandingBalance;
system("pause");
return 0;
}
【问题讨论】:
-
你不能只使用
cin>>students[count].studentName;。顺便说一句 -
@Neox - 如果学生的名字中有空格,则不会,就像“John Hancock”那样。