Edited to: Checking input

I have a simple program like this:

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

using namespace std;

int main () {
	int n;
	do {
		cout << "Enter number of players: ";
		cin >> n; //replaced with scanf("%d", n)
		if (n > 0 && n <= 5) break;
		else cout << "Value unexpected!";
	} while (1);
	cout << n;
	cin.get();
	return 0;
}


If n is bigger than 0, equal or less than 5 and also an integer, the program will print n. (If user enter some character, n will be some big number like 12340 so I can easily check if n is an integer or not).
If I enter a string, the loop will repeat of course but it will never ask user for input at line 9: cin >> n (scanf will, too). That's my problem.
Last edited on
if (n > 0 && n <= 5) break;

Why not use this as the while condition?


(If user enter some character, n will be some big number like 12340 so I can easily check if n is an integer or not).
That isn't a very good approach for that. I would suggest something like
1
2
3
4
5
6
while(std::cout << "Please enter an integer: " && !(std::cin >> n)) //inputs and fails
{
    std::cin.clear():
    std::cin.ignore(1024, '\n'); //1024 can be a large number of std::numeric_limits<std::streamsize>::max()
    std::cerr << "Error - The \"number\" entered was not valid." << std::endl;
}

The problem is that you have your input stream in a failed state then you try to read and it doesn't know what to do.

I would also suggest checking to make sure that the next character is a newline.
1
2
3
4
char c;
//read in number
std::cin.get(c);
//if c == '\n it is good otherwise failed.  


Another approach would be to read into a string with std::getline then use a stringstream and make sure it is the correct format.
Last edited on
So how can I check if the next character is '\n' (Press Enter with empty input)? This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <sstream>

void Check(int& n) {
	std::string s;
	std::cout << "Enter a number: ";
	std::cin >> s;
	std::cout << "Input: '" << s << "'\n"; // Pressed Enter with empty input, would not print
	if (s == "\n" || !(std::stringstream(s) >> n)) // s == "", s == "\0" not gonna work
	{
		std::cout << "Input is invalid\n";
		Check(n);
	}
}

int main () {
	int n;
	Check(n);
	std::cout << n;
	System::Console::ReadLine();
	return 0;
}
Last edited on
dleanjeanz wrote:
So how can I check if the next character is '\n' (Press Enter with empty input)?


Giblit wrote:
Another approach would be to read into a string with std::getline then use a stringstream and make sure it is the correct format.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    cout << "Press ENTER: ";
    string s;
    getline(cin, s);
    if(s.empty())
        cout << "Yay";
    else
        cout << "Problem: s is " << s;

    return 0;
}
Oops. Thanks.
Topic archived. No new replies allowed.