Grading Program

I began learning how to code this month and attended a c++ camp for a week, so I am familiar with all of the basics. I found these exercises, which I found very helpful. Could one look over my grading program because whenever I start debugging the program, it asks me to imput my grade, but after I type 80 for example and press enter, the program dissapears?

I used a bunch of "if statements" and depending on the value of "int grade" the "char letter_grade" changes.

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
// Grading Program (1).cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int grade; //Number grade that the user imputs
char letter_grade; //Letter grade that the user receives


int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Please enter your grade!\n";
	cin >> grade; //Receive grade from user
	if(grade == 100)
	{
		cout << "You received a perfect score! Good Work!\n";
	}
	if(grade >= 90 && grade <= 100)
	{
		letter_grade = 'A';
		cout << "You received an: " << letter_grade << endl;
	}
	if(grade >= 80 && grade <= 89)
	{
		letter_grade = 'B';
		cout << "You received an: " << letter_grade << endl;
	}
	if(grade >= 70 && grade <= 79)
	{
		letter_grade = 'C';
		cout << "You received an: " << letter_grade << endl;
	}
	if(grade >= 60 && grade <= 69)
	{
		letter_grade = 'D';
		cout << "You received an: " << letter_grade << endl;
	}
	if(grade == 0 && grade <= 59)
	{
		letter_grade = 'F';
		cout << "You received an: " << letter_grade << endl;
	}

	return (0);
}



You have two options, you can either run your code from the command line (Command Prompt for Windows) or you can add a way to "pause" the code at the end so it doesn't just flash away. Most people opt for the later simply because people hate the command line.

Simple add cin.ignore(); and the code will wait for you to hit a button. If one doesn't work, two might. If you want to read up on why this happens, here is a very long thread that explains all of the different options:
http://www.cplusplus.com/forum/beginner/1988/
Please don't use system()

The best option suggested so far has been Duoas' article, here: http://www.cplusplus.com/articles/iw6AC542/

If you're not much of a reader, I'd start with the link to Duoas' article. It explains just about everything you need.
Thanks for the reply! How do you run the code from the command line (Command prompt window)? Adding the cin.ignore(); didn't work. Are you supposed to add that line anywhere or after each "if statement".
options

1)open program from project folder\debug\programname
2)use system("pause") if you are beginner and on windows to hold screen
3)
1
2
3
4
std::cin.ignore();    //conditional if you are inputting anything before this anywhere
//you can use flush function too and is better one
std::cout<<"Press any key to End program.....";
std::cin.get();

4)using conio.h function getch();
5)Best one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Close
{
public:
~Close()
{
std::cout<<std::endl<<"Press any key to end program";
std::cin.get();             //can use some more methods here
}
};
int main()
{
Close C;
//code here
return 0;
}
Last edited on
Thanks a lot Akshit and Volatile Pulse for your suggestions! I got it to work (:
Topic archived. No new replies allowed.