Program auto-exits

I wrote this program to input roll no, class, section and marks in five subjects with total marks in the end with percentage, but it auto exits at the cout << "Marks -\n"; line, here's the full program :

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
#include <iostream>
int main ()
{int roll_no,grade,sec,hindi,eng,sst,mat,sc,tmarks,perc;
cout << "Enter your roll no.";
cin >> roll_no;
cout << "Enter your class -\n";
cin >> grade;
cout << "Enter your section -\n";
cin >> sec;
cout << "Marks -\n";
cout << "Hindi -\n";
cin >> hindi;
cout << "English -\n";
cin >> eng;
cout << "Maths -\n";
cin >> mat;
cout << "Social Studies -\n";
cin >> sst;
cout << "Science -\n";
cin >> sc;
tmarks = hindi+eng+sst+mat+sc;
cout << "Total marks -\n";
cout << tmarks;
perc = tmarks/500*100;
cout << "Percentage -\n";
cout << perc;
} 

Help please.
Last edited on
What input causes it to exit like that?
More specifically do you enter sec as number?
@LB it enters as soon I hit the section there, the cmd window exits.
@Catfish No I enter sec as an aplhabet
Bump, please guys help.
You have not answered my question. Tell us EXACTLY, letter for letter, what you type in.
I'm not sure what problems you're having but the following program worked (executed without runtime errors) for me:

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

using namespace std;
int main ()
{int roll_no,grade,sec,hindi,eng,sst,mat,sc,tmarks,perc;
cout << "Enter your roll no.";
cin >> roll_no;
cout << "Enter your class -\n";
cin >> grade;
cout << "Enter your section -\n";
cin >> sec;
cout << "Marks -\n";
cout << "Hindi -\n";
cin >> hindi;
cout << "English -\n";
cin >> eng;
cout << "Maths -\n";
cin >> mat;
cout << "Social Studies -\n";
cin >> sst;
cout << "Science -\n";
cin >> sc;
tmarks = hindi+eng+sst+mat+sc;
cout << "Total marks -\n";
cout << tmarks;
perc = tmarks/500*100;
cout << "Percentage -\n";
cout << perc;
system("pause");
return 0;
} 


I don't know if this is what you want or not since I have no idea what this program is supposed to do. If you're using Visual then you need something like the line system("pause"); or else the program will just exit as soon as it's finished executing all the lines, which sometimes may seem like the program just spontaneously stops working. I think line 26 isn't what you want either. I think you may want the variable "perc" to be a type double and on line 26 I think you may want to use 500.0 instead of 500 but again I have no idea what this program is supposed to do.
harry14 wrote:
@Catfish No I enter sec as an aplhabet


Did you know sec is of type int (for numbers) and not string (for character arrays)?
I've already given the program word to word what I wrote, no I didn't know sec was for just numbers, so I tried changing the variable name to sect but that didn't work either, also I tried bool your program letter to letter but it's the same problem again, the program loads gets to the section line I enter a single alphabet 'A', 'B' without the quotes but then the program skips all the marks inputs for some reason, it just shows the output lines like "Hindi -" and thats it, then press any key to continue and the program fails, to be more precise without the system pause, debugging showed whenever I enter the section alphabet, there were couple of missing PDB file errors with kernel.dll and many others, some reason??
Bump, what now guys?
Your program is asking for an integer (number) and you are giving it a character (letter). This causes the input stream to error and it will not work anymore, which is why it skips all the inputs.
Last edited on
So what can I do to solve this problem?
Console input is always tricky. Best approach I can think of is always assume the input is string. Then once your program read in the input, go and figure out if it is a "number string".
Sorry but I'm not really familiar with C++ strings, can you please explain it more specifically or with examples?
Alright guys after some thinking I finally found the solution! As you guys told me with int function you can't write a character, so I thought would it be possible to include 2 functions in one driver function? turns it out it did, so I used float for all the other variables and char for just section and magic! It worked! Now I am having a minor problem though, when total marks and percentage are displayed, they're displayed like this :
Total marks-
450Percentage -
90Press any key to continue...


Any way to separate the components out? I tried \n
Topic archived. No new replies allowed.