I've created this program to calculate scores of peoples tests and count the number of grades. What i want to happen is that when the user enters "//" when the program asks "Enter student Name", the while statement should end, or that's what i assume should happen. What actually happens is that i type "//" then the program asks for the score, then the while statement ends. Why is this and what should i do to fix it?
#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;
}
Most of the things have been already said in the comments:
Your problem is, that you expect the loop to check its condition continuously, which it does not.
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.
Like mentioned in the comments, a working alternative could be:
while(true)
{
std::cin >> name;
if(name == "//")
break;
else
evaluateScore();
}
Try this:
#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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.