2
我有這個程序,但隨機跳過cin ..我的意思是有時它會,有時它不會。任何想法如何解決這一問題?C++ cin cin隨機跳過
int main(){
/** get course name, number of students, and assignment name **/
string course_name;
int numb_students;
string assignment_name;
Assignment* assignment;
cout << "Enter the name of the course" << endl;
cin >> course_name;
cout << "Enter the number of students" << endl;
cin >> numb_students;
cout << "Enter the name of the assignment" << endl;
cin >> assignment_name;
assignment = new Assignment(assignment_name);
/** iterate asking for student name and score **/
int i = 0;
string student_name;
double student_score = 0.0;
while(i < numb_students){
cout << "Enter the name for student #" << i << endl;
cin >> student_name;
cout << "Enter the score for student #" << i << endl;
cin >> student_score;
assignment->addScore(Student(student_name, student_score));
i++;
}
}
好的我想通了。對於任何人想知道這裏是更新後的代碼:
int main(){
/** get course name, number of students, and assignment name **/
string course_name;
int numb_students;
string assignment_name;
cout << "Enter the name of the course" << endl;
getline(cin, course_name);
cout << "Enter the number of students" << endl;
string temp;
getline(cin, temp);
numb_students = atoi(temp.c_str());
cout << "Enter the name of the assignment" << endl;
getline(cin, assignment_name);
Assignment assignment(assignment_name);
/** iterate asking for student name and score **/
int i = 0;
string student_name;
double student_score = 0.0;
while(i < numb_students){
cout << "Enter the name for student #" << i+1 << endl;
getline(cin, student_name);
cout << "Enter the score for student #" << i+1 << endl;
getline(cin, temp);
student_score = atof(temp.c_str());
assignment.addScore(Student(student_name, student_score));
i++;
}
'隨機跳過'是什麼意思?你可以說得更詳細點嗎? – 2010-04-06 17:00:25
'numb_students'岩石:D – pajton 2010-04-06 17:01:02
numb_students:如何真實;-) – JRL 2010-04-06 17:01:29