问题描述
我创建了一个C ++程序来计算学生的平均成绩。第一部分可以工作,但是当我输入done退出时,我的程序会经历一个无限循环并且不会停止生成数字。
这是我的代码:
I created a C++ program to compute a student's average grade. The first part works, but when I type "done" to exit, my program goes through an infinite loop and won't stop producing numbers.
Here is my code:
#include <iostream>
using namespace std ;
void studentGrade (string) ;
int main ()
{
string name ;
int scoreOne, scoreTwo, scoreThree, scoreFour, scoreFive, scoreSix, scoreSeven, scoreEight, scoreNine, scoreTen ;
float average = 0 ; //stores the average of all grades
float total = 0 ; //stores sum of grades
char letterGrade ; //stores the final letter grade of student
name = "zero" ;
studentGrade (name) ;
}
void studentGrade (string name)
{
int scoreOne, scoreTwo, scoreThree, scoreFour, scoreFive, scoreSix, scoreSeven, scoreEight, scoreNine, scoreTen ;
char letterGrade ;
float average = 0 ;
float total = 0 ;
while (name != "done")
{
cout << "Enter student's name." << endl ;
getline (cin, name) ;
cin.ignore () ;
if (name != "done")
{
cout << "Enter " << name << "'s 10 test scores." ;
cin >> scoreOne >> scoreTwo >> scoreThree >> scoreFour >> scoreFive >> scoreSix >> scoreSeven >> scoreEight >> scoreNine >> scoreTen ;
total = scoreOne + scoreTwo + scoreThree + scoreFour + scoreFive + scoreSix + scoreSeven + scoreEight + scoreNine + scoreTen ;
average = total / 10 ;
if (average > 89.5)
letterGrade = 'A' ;
else if (average > 79.5)
letterGrade = 'B' ;
else if (average > 69.5)
letterGrade = 'C' ;
else if (average > 59.5)
letterGrade = 'D' ;
else
letterGrade = 'F' ;
cout << "Student's name: " << name << endl ;
cout << "Average Grade: " << average << endl ;
cout << "Letter Grade: " << letterGrade << endl ;
name = "zero" ;
}
else
cout << "All student's done." << endl ;
}
cout << "All student's done." << endl ;
}
我的尝试:
当我输入名称时,程序正常工作,但当我输入完成退出时,它不会。我只是想知道我是否错过了退出循环或者我没有做正确的事情。感谢您的帮助!
What I have tried:
When I type names, the program works properly, but when I enter "done" to exit, it doesn't. I was just wondering if I am missing something to exit the loop or if I didn't do something right. Thank you for helping!
推荐答案
while(name!=完成)
{
cout<< 输入学生的姓名。 << endl;
getline(cin,name);
cin.ignore();
if(name!=done)
{
cout<< 输入<<名称<< 有10个考试成绩。 ;
cin>> scoreOne>>得分2>>得分三>> scoreFour>> scoreFive>> scoreSix>>得分>> scoreEight>> scoreNine>>得分10分;
总计=得分一分+得分两分+得分三分+得分四分+得分五分+得分六分+得分七分+得分八分数+分数十分;
while (name != "done")
{
cout << "Enter student's name." << endl ;
getline (cin, name) ;
cin.ignore () ;
if (name != "done")
{
cout << "Enter " << name << "'s 10 test scores." ;
cin >> scoreOne >> scoreTwo >> scoreThree >> scoreFour >> scoreFive >> scoreSix >> scoreSeven >> scoreEight >> scoreNine >> scoreTen ;
total = scoreOne + scoreTwo + scoreThree + scoreFour + scoreFive + scoreSix + scoreSeven + scoreEight + scoreNine + scoreTen ;
移动 cin.ignore();
声明,以获取
Move the cin.ignore();
statement, in order to obtain
while (name != "done")
{
cout << "Enter student's name." << endl ;
getline (cin, name) ;
if (name != "done")
{
cout << "Enter " << name << "'s 10 test scores." ;
cin >> scoreOne >> scoreTwo >> scoreThree >> scoreFour >> scoreFive >> scoreSix >> scoreSeven >> scoreEight >> scoreNine >> scoreTen ;
cin.ignore (); // <--- moved here
total = scoreOne + scoreTwo + scoreThree + scoreFour + scoreFive + scoreSix + scoreSeven + scoreEight + scoreNine + scoreTen ;
while( true )
{
cout << "Enter student's name." << endl;
getline( cin, name );
cin.ignore();
if( name == "done" )
break;
// continue logic here
}
break
您认为需要停止循环。
这篇关于如何退出循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!