我创建了这个程序来计算人们测试的分数并计算成绩的数量。我想要发生的是当用户输入" //"当程序询问"输入学生姓名"时,while语句应该结束,或者我认为应该发生的事情。实际发生的是我输入" //"然后程序要求得分,然后while语句结束。为什么这个以及我该怎么做才能解决它?
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, grade;
int score;
int count1 = 0;
int acount = 0;
int bcount = 0;
int ccount = 0;
int dcount = 0;
int fcount = 0;
while (name != "//") {
cout << "Enter Student Name \n";
cin >> name;
cout << "Enter Student Score \n";
cin >> score;
count1 += 1;
if (score >= 90) {
grade = "A";
acount += 1;
cout << name << " " << score << " " << grade << endl;
}
else if (score >= 80) {
grade = "B";
bcount += 1;
cout << name << " " << score << " " << grade << endl;
}
else if (score >= 70) {
grade = "C";
ccount += 1;
cout << name << " " << score << " " << grade << endl;
}
else if (score >= 60) {
grade = "D";
dcount += 1;
cout << name << " " << score << " " << grade << endl;
}
else if (score <= 59) {
grade = "F";
fcount += 1;
cout << name << " " << score << " " << grade << endl;
}
};
cout << "Summary Report \n" << "Total Students Count: " << count1 << endl;
cout << "A student count: " << acount << endl;
cout << "B student count: " << bcount << endl;
cout << "C student count: " << ccount << endl;
cout << "D student count: " << dcount << endl;
cout << "F student count: " << fcount << endl;
return 0;
}
答案 0 :(得分:2)
评论中已经说过大部分内容:
你的问题是,你希望循环能够连续检查它的状态,而不是。
while(name != "//") // here the condition is evaluated
{
name = "//"; // here it is not
// the following code is executed, independent of the current name value
} // here the program jumps back to the loop condition, which is now false and it will not enter another time.
如评论中所述,一个可行的替代方案可能是:
while(true)
{
std::cin >> name;
if(name == "//")
break;
else
evaluateScore();
}
答案 1 :(得分:0)
试试这个:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name, grade;
int score;
int count1 = 0;
int acount = 0;
int bcount = 0;
int ccount = 0;
int dcount = 0;
int fcount = 0;
while (true) { // keep running the loop
cout << "Enter Student Name \n";
cin >> name;
if(name == "//") // if "//" found, break the loop
break;
cout << "Enter Student Score \n";
cin >> score;
count1 += 1;
if (score >= 90) {
grade = "A";
acount += 1;
cout << name << " " << score << " " << grade << endl;
}
else if (score >= 80) {
grade = "B";
bcount += 1;
cout << name << " " << score << " " << grade << endl;
}
else if (score >= 70) {
grade = "C";
ccount += 1;
cout << name << " " << score << " " << grade << endl;
}
else if (score >= 60) {
grade = "D";
dcount += 1;
cout << name << " " << score << " " << grade << endl;
}
else if (score <= 59) {
grade = "F";
fcount += 1;
cout << name << " " << score << " " << grade << endl;
}
};
cout << "Summary Report \n" << "Total Students Count: " << count1 << endl;
cout << "A student count: " << acount << endl;
cout << "B student count: " << bcount << endl;
cout << "C student count: " << ccount << endl;
cout << "D student count: " << dcount << endl;
cout << "F student count: " << fcount << endl;
return 0;
}