If-Else Statement not working correctly

Jun 27, 2015 at 5:20pm
Hello

So recently I got back into programming with C++ in Visual Studio 2013 Express. I am just a beginner, so I searched up some easy exercises and started playing around with if-else statements.
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
#include <iostream>

using namespace std;

int main()
{
	int score;

	cout << "Please enter your score.\n";
	cin >> score;

	if(score >= 100)
	{
		cout << "HIGH\n";
		cout << "Press enter to continue\n";
	}
	else
	{
		cout << "LOW\n";
		cout << "Press enter to continue\n";
	}

	cin.get();

	return 0;
}


For some reason, when I use the debug tool to run the program, it ends the program whenever I enter the score. Any input is greatly appreciated.
Jun 27, 2015 at 5:27pm
When I built it. it worked perfectly. make sure you are "running it right"
Jun 27, 2015 at 5:32pm
Yeah, I just hit F5 but for some reason it doesn't work the way I want it too in Visual Studio.
Jun 27, 2015 at 5:37pm
when I use the debug tool to run the program, it ends the program whenever I enter the score
Debug run does not keep window opened after program finishes. It is ntended to find errors in program and if program finished, there is no need to keep it open anymore.

Either place a breakpoint at closing brace of main, or run without debugging.
Jun 27, 2015 at 5:40pm
Ok, I thought the cin.get(); line would make the program wait for me to press enter, then it would end. Sorry if I act like a nob, its because I am one.
Jun 27, 2015 at 5:43pm
I thought the cin.get(); line would make the program wait for me to press enter
It might. Or might not. Depending on state of input buffer.
In your case there is an unread character here (newline — operator>> does not extract whitespace characters after actual value).
Jun 27, 2015 at 5:45pm
Ok, thanks. So how would one go about running it without the debugger? Thanks for the help so far.
Jun 27, 2015 at 5:46pm
Debug → Start without debugging or Ctrl + F5
Jun 27, 2015 at 5:59pm
Ok, that still gave me the same problem. Would you mind enlightening me on the reason why cin.get() is not working?
Jun 27, 2015 at 6:02pm
Make sure that you are linking it to console subsystem: http://stackoverflow.com/a/1152873
Jun 27, 2015 at 6:09pm
Ok, that made it work. This was my first time using this forum and it was a big help. Thanks again all.
Topic archived. No new replies allowed.