Program Reading Letter Grades

This is what I have so far. any and all help is greatly appreciated. My program has a class of 20 students and D or better is passing at the end i have to tell how many students passed or failed.

#include<iostream>
using namespace std;
int main()
{
int grade;
int passing=0;
int failing=0;
cout<<"enter a grade: ";
cin>>grade;
for (int student=20; (grade>0 && grade<100); student--){
while (student>0){
if (grade== "a")
passing++;
if (grade== "b")
passing++;
if (grade=="c")
passing++;
if (grade=="d")
passing++;
if (grade=="e");
failing++;
if (grade=="f");
failing++;
}
}
cout<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
system("pause");
return(0);
}

Consider moving the. cout and cin statements within the while loop block.
I've been getting error C2440: '=' : cannot convert from 'const char [2]' to 'int'
It just won't add the grades for passing and failing up.

#include<iostream>
using namespace std;
int main()
{
int grade;
int passing=0;
int failing=0;
int student=20;

while (student>0) {
cout<<"enter a grade: ";
cin>>grade;
student--;
if (grade== 'a')
passing++;
else if (grade== 'b')
passing++;
else if (grade=='c')
passing++;
else if (grade=='d')
passing++;
else if (grade=='e')
failing++;
else (grade=='f');
failing++;
}

cout<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
system("pause");
return(0);
}
You almost have it working

Take out the system("pause");
most people here consider it poor programming, even though it does work, there are better ways. Run it from a command line is better.

Change int grade; to
string grade;

For each grade change
if (grade== 'a')
to
if (grade== "a")

Test your program, try using invalid letters and numbers such as 1 2 3 4 A B C D. It needs more work but your getting there.

Results from my test
C:\Temp>teststudentgrade2
enter a grade: a
enter a grade: b
enter a grade: c
enter a grade: d
enter a grade: e
enter a grade: f
enter a grade: a
enter a grade: b
enter a grade: c
enter a grade: d
enter a grade: e
enter a grade: f
enter a grade: a
enter a grade: b
enter a grade: c
enter a grade: d
enter a grade: e
enter a grade: f
enter a grade: a
enter a grade: b
14 students passed
23 students failed

C:\Temp>teststudentgrade2
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
enter a grade: a
20 students passed
20 students failed

I bet if you read the next 2 lines you can figure the above problem out.
else (grade=='f');
failing++;

There will still be a logic problem you need to figure out.
C:\Temp>teststudentgrade2
enter a grade: aa
enter a grade: bb
enter a grade: cc
enter a grade: dd
enter a grade: ee
enter a grade: ff
enter a grade: dd
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
enter a grade: f
0 students passed
13 students failed
Last edited on
I'm not quite sure how to apply the string, I do see the issue with my else statement but i'm not sure how to fix it I tried substituting the ifs for while loops but it didnt go well it only executes the first number.
Im not sure where to go if anyone could please help me with the errors in my program. I'd really appreciate it.

#include<iostream>
#include<string>
using namespace std;
int main()
{
string grade;
int passing=0;
int failing=0;
int student=20;

for (student>0; student--;) {
cout<<"enter a grade: ";
cin>>grade;

while (grade=='a' || 'b' || 'c' || 'd');{
passing++;
}
while (grade== 'e' || 'f'){
failing++;
}
}

cout<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
return(0);
}
I can't get my program to repeat the loop it's in please help me.

#include<iostream>
using namespace std;
int main()
{
char ch1='a', ch2='b', ch3='c', ch4='d', ch5='e', ch6='f';
char ch;
int passing=0;
int failing=0;
int student=20;

for (student > 0; student--;) {
cout<<"enter a grade: ";
cin>>ch;

while (ch == ch1 || ch2 || ch3 || ch4);{
passing++;
}
while (ch == ch5 || ch6){
failing++;
}
}

cout<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
return(0);
}

It only gives me 10 students passed 10 students failed. How best to fix that? Any and all help is appreciated.

#include<iostream>
using namespace std;
int main()
{
char ch1='a', ch2='b', ch3='c', ch4='d', ch5='e', ch6='f';
char ch=0;
int passing=0;
int failing=0;
int student=20;

while (student > 0){

if (ch == ch1 || ch2 || ch3 || ch4);{
passing++;
cout<<"enter a grade: ";
cin>>ch;
student--;
}
if (ch == ch5 || ch6){
failing++;
cout<<"enter a grade: ";
cin>>ch;
student--;
}
}

cout<<passing<<" students passed"<<endl;
cout<<failing<<" students failed"<<endl;
system ("pause");
return(0);
}
First off, you have the program set up so that it only takes one grade.
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
using namespace std;
int main()
{
int grade;
int passing=0;
int failing=0;
//this next line declares the top of the program so it knows where to go
//back to at the bottom
Top1:
cout << "Enter a grade: ";
cin >> grade;
//for these you needed ' instead of "
if (grade== 'a')
{
passing++;
}
else if (grade== 'b')
{
passing++;
}
else if (grade== 'c')
{
passing++;
}
else if (grade== 'd')
{
passing++;
}
else if (grade== 'e');
{
failing++;
}
else if (grade== 'f');
{
failing++;
}
else
{
cout << "Please enter a valid choice" << endl;
//this command tells it to go back to the top where you declared Top1
goto Top1;
}
cout << passing << " students passed" << endl;
cout << failing << " students failed" << endl;
system("pause");
return(0);
}

That should work if you are using microsoft visual c++ 2010 express, but if you use another compiler
then you might need to change the syntax a bit.
Last edited on
Topic archived. No new replies allowed.