the code not work

If you enter a word then it shows "please write a number" and close the program quickly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() {
	cout << "please write a number" << endl;
	int x;
	cin >> x;
	if(cin.fail()) {
		cerr << "has a error please try again";
		cin.clear();
	}

	cin.get();
	cin.ignore();
	return 0;
}
Last edited on
First of all, what the hell is "cerr"? You might want to try using the correct word...
Second, what exactly are you trying to do? We can't help you if you don't tell us what the problem is.

Finally, your "pause" is not a pause at all... what is your statement ignoring? Nothing... you need to pass arguments to the function... try this.

1
2
3
4
5
6
7
void pause()
{
    cin.clear();
    cin.ignore(1, '\n');
    cout << "Press Enter to continue...";
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}


Then just call the function. You'll have to include the <limits> header file.
packetpirate wrote:
First of all, what the hell is "cerr"? You might want to try using the correct word...


cerr is the right word^^... it is the "standard error stream"... it has been unbuffered formerly, afaik...

packetpirate wrote:
Finally, your "pause" is not a pause at all... what is your statement ignoring? Nothing... you need to pass arguments to the function... try this.


well... cin.ignore() ignores the next character in the stream...
(ofc it is not very useful while using it, as he does^^)...
Last edited on
Topic archived. No new replies allowed.