Dev-C++ Debug Problem.

Hey!

Okay. I got this book "Beginning C++ Game Programming" and im suppost to make this code in Dev-C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Score Rater 2.0
// Demonstrates an else clause

#include <iostream>
using namespace std;

int main() 
{
	int score;
	cout << "Enter your score: ";
	cin >> score;

	if (score > 500)
		cout << "\nYou got over 500. Nice score.\n";
	else
		cout << "\nYou got 500 or less. Nothing to brag about.\n";
		
    
    return 0;
}


The consol pops up and it works great. Untill i write a number (as im suppost) in the consol. Then it closes down really quick. No error message or anything.

I quess its something with the compiler. That i have to change some settings somewhere or something like that. Or maybe its the code. But i dont think so.

This have happen in some other programs i have made. Where I have to type something in (A number, letters, etc).

Thanks! Please help.
Last edited on
Some IDEs allow the console staying open but if you want to be sure to keep the console window open, try one of these solutions:
http://www.cplusplus.com/forum/articles/7312/

- the best way is cin.ignore( numeric_limits<streamsize>::max(), '\n' ); -
# Bazzy

I have now tryed serveral things in that thread.

And still, when it says "Enter Your Score" and i press "500" it closes down.

I type the "cin.ignore( numeric_limits<streamsize>::max(), '\n' );" in right before the "Return 0;" - Is that correct?
I type the "cin.ignore( numeric_limits<streamsize>::max(), '\n' );" in right before the "Return 0;" - Is that correct?
Yes

And still, when it says "Enter Your Score" and i press "500" it closes down.
The problem should be that you are using >> to get input and it leaves the last newline character.
There are many ways of removing it, one is calling cin.ignore another time.
If you instead want to get input in a safer way, read this article: http://www.cplusplus.com/forum/articles/6046/ (In this way you would need to call cin.ignore only once)
Okay..

It still doesn't work with the "cin.ignore( numeric_limits<streamsize>::max(), '\n' );" code, and the post didn't help me.

It just sounds wierd, that he uses the code like this in the Book, but it doesn't work for me. There must be something that helps.

It still keeps shuting down when i type in a number and hit "Enter". Its wierd.

Thanks for your help so far!
Topic archived. No new replies allowed.