.exe file suddenly stops running

Hello everyone, I'm attempting to teach myself C++ and I've started to create a custom GPA calculator using the principles I've learned so far (although I'm sure there are easier ways to do it). My code compiles and asks the first two questions as expected (one at a time) but upon answering the second question (What is your grade in class 1?) All of the next 4 questions appear in a run-on statement and windows tells me the .exe file has stopped working without explanation.

If there is a simple fix I am all ears, but if I'm in way over my head I'll scrap the whole thing, as I'm only experimenting and trying to learn more. Again, there is no error because the program compiles fine via CodeBlocks and the GNU compiler, Thanks everybody!

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
#include <iostream>
#include <cmath>

using namespace std;

int classes;
float A = 4.0;
float Am= 3.7;
float Bp = 3.3;
float B = 3.0;
float Bm = 2.7;
float Cp = 2.3;
float C = 2.0;
float Cm = 1.7;
float Dp = 1.3;
float D = 1.0;
float F = 0.0;
int class1;
int class2;
int class3;
int class4;
int class5;
int class6;

int main ()
{
    cout << "How many classes do you take? Indicate pluses and minuses with a lower case p or m, so a B+ would be written Bp ";
    cin>> classes;

    cout << "What was your grade in class 1? ";
    cin>> class1;

    cout << "What was your grade in class 2? ";
    cin>> class2;

    cout << "What was your grade in class 3? ";
    cin>> class3;

    cout << "What was your grade in class 4? ";
    cin>> class4;

    cout <<"What was your grade in class 5? ";
    cin>> class5;

    cout <<"What was your grade in class 6? ";
    cin>> class6;

    cout << "Your GPA is: " << classes / (class1 + class2 + class3 + class4 + class5 + class6) << endl;

    return 0;
}
use

 
system("pause");


at the end of main before
 
return 0;
Last edited on
Do not use system("pause"); ever.
http://www.cplusplus.com/forum/beginner/1988/ <-- How to fix the problem
http://www.cplusplus.com/articles/j3wTURfi/ <-- Why you shouldn't use system
Last edited on
windows tells me the .exe file has stopped working
This doesn't sound like a "Console Closing Before Output" problem.

@F1n
You're putting cin into a fail state when you try to read "Ap" or something similar into an int. The program then blows right past all your other cins, leaving you with no data in class1, class2, etc. It looks like your runtime environment is configured in debug mode, so these probably happen to be initialized to zero for you (even though you should be doing this yourself). Then, what happens on line 48 when you perform division you attempt to divide by zero.

You'll have to get strings from the user and turn them into values before you add them, or get actual integers from the user instead of "A", "Bp", "Cm", etc.
Last edited on
Topic archived. No new replies allowed.