Problem with imputing anything that's not an int

So, I'm new to programing (this is my first year, just started college a few months ago) and I've been asked to do this program that runs a game called "Los chinos" in which you play vs the computer machine. The rules are simple, both players have a maximum amount of coins (it's a variable the user can change) and has to pick any amount of coins, from 0 to the max value of coins. Then, the program displays the random bet based of the random number of coins the machine chose and the user bets anythinhg equal or higher than his amount of coins, but bnever equal to the machine, and tries to guess the total number of coins. Then, the program displays the total amount of coins, and displays the winner of each round. The program runs perfectly, and if I sent it I'd probably get a 10/10 but I'm an overachiever and I'm pretty intrigued about this. My main isue is with the cins.

You see, when I input anything that's not an int, the program goes crazy and does weird stuff. So far I've been able to patch it against inputing any strings or decimal values. Now, I'm facing this problem, in which if i input, let's say 1+1 or 1a or anything that has an int first and then something else, the program does weird things. So, I was wondering if there is a fix for that. I thought of converting the cin to a string, reading it and if it contained anything that was not a number, it'd just loop back to the cin. Any help is appreciated ^^.

PS: Most of the couts have std:: in front of them even though using namespace std is declared because I had some issues with the couts being abmbiguous and that was the only soution I found.

the program is quite long and doesn't fit here, so I'll just add the menu function and the one that checks doubles.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 int menu() {
	int option = 0;
	double optionDouble = 0;
	std::cout << "Welcome to Los Chinos." << endl;
	std::cout << "Please, input an option." << endl;
	std::cout << "1. - Change maximum score" << endl;
	std::cout << "2. - Change maximum number of coins" << endl;
	std::cout << "3. - About 'Los chinos'" << endl;
	std::cout << "4. - Play!" << endl;
	std::cout << "5. - Automatic game" << endl;
	std::cout << "0. - Exit" << endl << endl;
	std::cout << "Your option: ";

	while (!(cin >> optionDouble)) {
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout << "Invalid input.  Try again: ";
	}

	option = checkForDouble(option, optionDouble);

	while ((option < 0) || (option > 5)) {
		if (option > 5) {
			std::cout << "ERROR. Value can't be higher than 5. Try again: ";
			cin >> option;
		}

		else if (option < 0) {
			std::cout << "ERROR. Value can't be lower than 0. Try again: ";
			cin >> option;
		}
	}
	std::cout << endl << endl;
	return option;
}

int checkForDouble(int a, double b) {
	a = lround(b);
	while ((a - b) != 0) {
		cout << "Error, only integers allowed, try again: ";
		cin >> b;
		a = lround(b);
	}
	return a;
}
Last edited on
You may need to use cin.ignore()
Topic archived. No new replies allowed.