In: Computer Science
I can not get the summary report to show in the output it just keeps running after i enter the names, scores, and then / /. Can someone help me I am using codeblocks but I might be able to figure out even if you use another IDE>
This is my code
#include <iostream>
using namespace std;
int main()
{
string name;
double score =0;
int totalStudents = 0;
int A=0,B=0,C=0,D=0,F=0;
cout << "Enter student name" << endl;
cin >> name;
while (name!="//"){
cout << "Enter student score" << endl;
cin >> score;
if(score>=90){
A++;
cout<<name<<" "<<score<<"
A"<<endl;
}
else if(score>=80&&score<90){
B++;
cout<<name<<" "<<score<<"
B"<<endl;
}
else if(score>=70&&score<80){
C++;
cout<<name<<" "<<score<<"
C"<<endl;
}
else if(score>=60&&score<70){
D++;
cout<<name<<" "<<score<<"
D"<<endl;
}
else if (score>=0&&score<60) {
F++;
cout<<name<<" "<<score<<"
F"<<endl;
}
cout << "Enter student name" << endl;
cin >> name;
totalStudents++;
}
cout << "Enter student name" << endl;
cin >> name;
cout << "Summary Report" << endl;
cout << "Total Students count " << totalStudents
<< endl;
cout << "A student count " << A << endl;
cout << "B student count " << B << endl;
cout << "C student count " << C << endl;
cout << "D student " << D << endl;
cout << "F students " << F << endl;
return 0;
}
This was the question
Topics
Loops
while Statement
Description
Write a program that computes the letter grades of students in a class from knowing their scores in a test. A student test score varies from 0 to 100. For a student, the program first asks the student’s name and the student’s test score. Then, it displays the student name, the test score and the letter grade. It repeats this process for each student. The user indicates the end of student data by entering two consecutive forward slashes ( // ) when asked for the student name. At the end, the program displays a summary report including the following:
· The total number of students.
· The total number of students receiving grade “A”.
· The total number of students receiving grade “B”.
· The total number of students receiving grade “C”.
· The total number of students receiving grade “D”.
· The total number of students receiving grade “F”.
The program calculates a student's the letter grade from the student's test score as follows:
A is 90 to 100 points
B is 80 to 89 points
C is 70 to 79 points
D is 60 to 69 points
F is 0 to 59 points.
Requirements
Do this exercise using a While statement and an If/Else If statement.
Testing
For turning in the assignment, perform the test run below using the input data shown
Test Run (User input is shown in bold).
Enter Student Name
Alan
Enter Student Score
75
Alan 75 C
Enter Student Name
Bob
Enter Student Score:
90
Bob 90 A
Enter Student Name
Cathy
Enter Student Score
80
Cathy 80 B
Enter Student Name
Dave
Enter Student Score:
55
Dave 55 F
Enter Student Name
Eve
Enter Student Score
85
Eve 85 B
Enter Student Name
//
Summary Report
Total Students count 5
A student count 1
B student count: 2
C student count 1
D student 0
F students 1
Sample Code
string name;
double score;
//Initias setup
cout << "Enter student name" << endl;
cin >> name;
//Test
while (name != "//")
{
cout << "Enter student score" << endl;
cin >> score;
//more code here
//Update setup
out << "Enter student name" << endl;
cin >> name;
}
//display summary report
The problem is not with the IDE their is a small mistake in your code due to which your code is not able to even reach the lines of code that print the summary report. Let's discuss it in detail. As soon as you enter // you will go out of your while loop and after while loop you have lines:-
cout << "Enter student name" << endl;
cin >> name;
So these lines would ask the user to input name again and these lines would stop lines of code that print the summary report.And you would never be able to print the summary report. Hence remove these lines from your code before this line cout << "Summary Report" << endl; And you will be able to print the summary report
Let's DryRun Your code
Enter Student Name
Alan
Enter Student Score
75
Alan 75 C
Enter Student Name
Bob
Enter Student Score:
90
Bob 90 A
Enter Student Name
Cathy
Enter Student Score
80
Cathy 80 B
Enter Student Name
Dave
Enter Student Score:
55
Dave 55 F
Enter Student Name
Eve
Enter Student Score
85
Eve 85 B
Enter Student Name
//
Enter Student Name(Instead of Summary Report)
Now as soon as you enter // you will go out of your while loop and after while loop you have lines:-
cout << "Enter student name" << endl;
cin >> name;
So these lines would ask the user to input name again and these lines would stop lines of code that print the summary report.And you would never be able to print the summary report. Hence remove these lines from your code before this line cout << "Summary Report" << endl; And you will be able to print the summary report. Use the code as follows:-
#include <iostream>
using namespace std;
int main()
{
string name;
double score =0;
int totalStudents = 0;
int A=0,B=0,C=0,D=0,F=0;
cout << "Enter student name" << endl;
cin >> name;
while (name!="//"){
cout << "Enter student score" << endl;
cin >> score;
if(score>=90){
A++;
cout<<name<<" "<<score<<"
A"<<endl;
}
else if(score>=80&&score<90){
B++;
cout<<name<<" "<<score<<"
B"<<endl;
}
else if(score>=70&&score<80){
C++;
cout<<name<<" "<<score<<"
C"<<endl;
}
else if(score>=60&&score<70){
D++;
cout<<name<<" "<<score<<"
D"<<endl;
}
else if (score>=0&&score<60) {
F++;
cout<<name<<" "<<score<<"
F"<<endl;
}
cout << "Enter student name" << endl;
cin >> name;
totalStudents++;
}
cout << "Summary Report" << endl;
cout << "Total Students count " << totalStudents
<< endl;
cout << "A student count " << A << endl;
cout << "B student count " << B << endl;
cout << "C student count " << C << endl;
cout << "D student " << D << endl;
cout << "F students " << F << endl;
return 0;
}
This would print the summary report.