I am taking a C++ class and I don't seem to be getting any help from my instructor. I am trying to create a program that can take four grades, average them out to a final grade and then set the grade to a letter. The output should look something like this.
"Your final grade is 89"
"Your letter grade is B"
Here is the code that I have done.
#include <iostream>
using namespace std;
int main()
{
int grade1;
int grade2;
int grade3;
int grade4;
double finalGrade;
double letter;
cout << "Input your first grade please" << endl;
cin >> grade1;
cout << "Input your second grade please" << endl;
cin >> grade2;
cout << "Input your third grade please" << endl;
cin >> grade3;
if (letter<60)
{
cout << "Your letter grade is F" << endl; }
else
{
if (letter<70)
{
cout << "Your letter grade is D" << endl; }
else
{
if (letter<80)
{
cout << "Your letter grade is C" << endl; }
else
{
if (letter<90)
{
cout << "Your letter grade is B" << endl; }
else
{
cout << "Your letter grade is A" << endl; }
}
}
}
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
return 0;
}
However the output that I am getting is this.
Input your first grade please
88
Input your second grade please
88
Input your third grade please
88
Input your fourth grade please
88
de please
Your letter grade is B
The weird thing is the 9th line where it shows "de please". I am really frustrated here and am in need of some advise. I don't really need the answer just a point in the right direction. I have gone over the code several times and tried changing up a few things but I still have not found the problem.
It works with Visual C++ 2010 (the exact output that I get from the command line):
Input your first grade please
88
Input your second grade please
88
Input your third grade please
88
Input your fourth grade please
88
88
Your letter grade is B
You have four cin's and nineteen cin.get's. Add to it that you don't tell the user to hit enter over a dozen times, so it's a bit confusing. Try cin.clear (); or cin.sync (); and a single cin.get ();.
Oh, that reminds me, please put your code into [code][/code] tags and indent it please.
Ok I'm a bit confused by your comment about hitting enter over a dozen times.
As for the cin.get(); I was told by my instructor to use a bunch of those to correct the problem of the console window going away before I could see the output. So what you are saying is that if I use a cin.clear (); or cin.sync (); and then just one cin.get (); it will keep the console window from going away?
The output that I am getting is a bit different from what you are getting. What I get is
1 2 3 4 5 6 7 8 9 10
Input your first grade please
88
Input your second grade please
88
Input your third grade please
88
Input your fourth grade please
88
de please
Your letter grade is B
I truely don't understand why it is giving me this output when it works perfectly on your computer.
I was told by my instructor to use a bunch of those [cin.get()'s] to correct the problem of the console window going away before I could see the output.
Either you misunderstood what they said, or you need to ask for your money back. This goes on the list of the worst kludges I've ever seen, regarding the "console closing down" problem.
I would agree with you on that one, but with me being very green behind the ears when it comes to programing I really don't know any better. And really I don't need to ask for my money back because using grants I am able to go to school for free right now.
Anyway this is getting away from the real issue of me not seeing the correct output. I have tried another IDE and it gave me the same problem. So it seems to me that it is either my coding is incorrect or my computer is having an issue.
#include <iostream>
usingnamespace std;
int main()
{
int grade1;
int grade2;
int grade3;
int grade4;
int finalGrade;
int letter;
cout << "Input your first grade please" << endl;
cin >> grade1;
cout << "Input your second grade please" << endl;
cin >> grade2;
cout << "Input your third grade please" << endl;
cin >> grade3;
cout << "Input your fourth grade please" << endl;
cin >> grade4;
finalGrade =grade1+grade2+grade3+grade4;
letter =finalGrade / 4;
switch(letter)
{
case 1:
letter < 60;
cout << "Your letter grade is F" << endl;
break;
case 2:
letter < 70;
cout << "Your letter grade is D" << endl;
break;
case 3:
letter <80;
cout << "Your letter grade is C" << endl;
break;
case 4:
letter < 90;
cout << "Your letter grade is B" << endl;
default:
cout << "Your letter grade is A" << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
and the ouput i get is:
1 2 3 4 5 6 7 8 9 10
Input your first grade please
88
Input your second grade please
88
Input your third grade please
88
Input your fourth grade please
88
Your letter grade is A
Press any key to continue . . .