|
-
March 6th, 2008, 10:58 PM
#1
Count Grades
My assignment was to create a program that inputs letter grades (A, B, C, D, F), counts how many of each they are, and outputs the results.
Here is what I have so far:
#include <iostream>
#include <string>
using namespace std;
int main()
{string grade;
int gradeA=0, gradeB=0, gradeC=0, gradeD=0, gradeF=0;
cout<<"Please enter a letter grade or '-1' to quit: ";
cin>>grade;
while(grade!=-1)
{if(grade==A)
gradeA++;
else if(grade==B)
gradeB++;
else if(grade==C)
gradeC++;
else if(grade==D)
gradeD++;
else
gradeF++;
cout<<"Please enter another letter grade or '-1' to quit: ";
cin>>grade;
}
cout<<endl<<"The total number of letter grades are:"<<endl;
cout<<gradeA<<" As."<<endl;
cout<<gradeB<<" Bs."<<endl;
cout<<gradeC<<" Cs."<<endl;
cout<<gradeD<<" Ds."<<endl;
cout<<gradeF<<" Fs."<<endl<<endl;
return 0;
}
I believe my problem is with this line:
while(grade!=-1)
Can anyone help me understand my mistakes? Thanks!
-
March 6th, 2008, 11:06 PM
#2
Re: Count Grades
You need to compare grade, which is a string, to other strings.
Code:
while (grade != -1)
That should be
Code:
while (grade != "-1")
And so on for the letters.
Also, a more logical keypress for the user to quit would be Q. -1 is thinking in programmer's terms, not end-user terms.
-
March 6th, 2008, 11:11 PM
#3
Re: Count Grades
Thanks for the speedy reply. That did the trick. The assignment asked us to use '-1'. Thanks again!
-
March 7th, 2008, 08:10 AM
#4
Re: Count Grades
Also, use white space for readability. Your code is kind of cluttered. In a bigger project it will be hard to read.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|