【发布时间】:2014-12-06 07:33:01
【问题描述】:
我对第 23 行的代码有疑问。(请参阅下面的代码)
当我使用 "cin >> studentNum";我没有问题,程序读取一个字符串作为名字,但是如果我想使用 "getline(cin, studentNum)" 收集更多数据来读取更多字符串和全名一样,程序只是跳过命令并询问分数。
这是为什么呢?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int studentNum, testNum, i, j;
double sum, testScore, average;
string studentName;
i = 0;
// asking user for input
cout << "Please enter the number of students in this classroom: " << endl;
cin >> studentNum;
cout << "Now enter the number of tests each student has taken in this class: " << endl;
cin >> testNum;
while (i < studentNum)
{
cout << endl << "Now please enter the firstname of the student: " << endl;
cin >> studentName; //**My Problem is in this line ###########**
j = 0;
sum = 0;
while (j < testNum)
{
cout << "Please enter the score of each test, then hit enter: " << endl;
cin >> testScore;
sum += testScore;
j++;
}
i++;
}
}
【问题讨论】:
标签: c++