I dont understand why please help

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;

cout << "Input your fourth grade please" << endl;
cin >> grade4;

finalGrade =grade1+grade2+grade3+grade4;

letter =finalGrade / 4;
cout << letter << endl;

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.

Any help is greatly appreciated.
Compiles and runs fine on VS2008. What compiler/IDE are you using?
I'm using MS Visual C++ 2010 Express. This is the software that the instructor told us to download and use.
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.
noobee wrote:
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.
I don't see anything in your code that would cause "de please" to output. Are you sure your compiling the code that you posted, exactly?
as for waiting for the user to do somthing before the console closes try:

1
2
3
    system("PAUSE");
    return EXIT_SUCCESS;
}

at the end of your program instead of return 0; }

and like @mathhead200 asks the code there does not compile with the "de please" in it.

and instead of using all them if statements you can use a switch which is the same as doing multiple ifs.

here is a edit of your code:

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
49
50
51
52
53
54
55
56
#include <iostream>

using namespace 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 . . .


@olzi:
You should definitely read this before suggesting system("PAUSE") to anyone.
http://www.cplusplus.com/articles/j3wTURfi/

ooo thanks @Nisheeth, i never knew that. that will have to be a hibbit to break, so instead i should use cin.get() return 0; } ?
You can use that.
There is a long discussion here about it:
http://www.cplusplus.com/forum/beginner/1988/
(120 posts. Happy reading)

And another article:
http://www.cplusplus.com/articles/iw6AC542/
thanks @Nisheeth really good read :D jus wat i needed wednesday morning lol
it got pointed out to me today that the swich () wont take conditions so my code wont work, so:

if

else if

else if

else

looks like the way forward, and to wait for the user to press enter to stop it closing,

'cin.get();
return 0;
}

works.
Topic archived. No new replies allowed.