Reading only numbers and not letters?

Hello beginner question, I had a similar problem before. I'm trying to get this program to not crash if a letter is entered instead of a number. Before I used cin.ignore() to solve the problem but it isn't working this time. I think I can use a trailing else statement to catch it but I'm unsure. any advice would be appreciated!


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
#include <iostream>
using namespace std;
int main()
{
	system("title Book Club Points");
	system("color f4");

	int numberOfBooks, points;
	cout << "Hello, and welcome to Serendipity Booksellers rewards program!\n"
		<< "Please enter the amount of books you purchased this month.\n";
	cin >> numberOfBooks;
	cin.get();
	if (numberOfBooks == 0)
		cout << "I'm sorry, you did not earn any points this month\n";
	else if (numberOfBooks == 1)
		cout << "Congratulations you earned 5 points!\n";
	else if (numberOfBooks == 2)
		cout << "Congratulations you earned 15 points!\n";
	else if (numberOfBooks == 3)
		cout << "Congratulations you earned 30 points!\n";
	else if (numberOfBooks >= 4)
		cout << "Congratulations you earned 60 points!\n";
	cin.ignore(1000, '\n');
	cin.get();
	cout << "Thank you for participating in Serendipity Booksellers rewards program!\n";
	cout << "Have a plesent day!";
	cin.get();
	return 0;
}
Last edited on
This is perennial problem with obtaining numeric data as input and dealing with non-numeric input.

There is no quick answer in C++. Entering a non-numeric char when numeric is expected puts the input stream state into fail mode. No further input can be obtained until the stream state has been reset to good (.clear()). Once done, the invalid char(s) need to be removed from the stream before further input is attempted - otherwise it just tries to repeatedly read the same invalid chars over and over.

cin >> returns the stream. ! can be used with this to determine if the stream state is bad. So:

1
2
3
4
5
if (!cin << numberofBooks) {
   // Code to execute if non-numeric input
} else {
    // Good input
}


So if bad, reset the state (.clear()) and then get and ignore remaining chars (.ignore()). Then the whole thing can be executed again in a loop until good input is obtained.

For an example of a function that obtains valid numeric input, see getInp() in http://www.cplusplus.com/forum/lounge/279954/

can be fun in guis too.. I have a 1/2 page real time entry validator for doubles that has to do something like check the value, if its bad revert to last good value (to the user, it is as if it rejected the last typed letter eg 123x would just revert to 123) and move the cursor back (so its where it was when the x was typed) and more. It took a while to get it foolproof.
Try using scanf_s.

int n;

scanf_s("%d",&n) saves alot of messing around imo.
Oops! I didn't read @OP's problem as thoroughly as I should. The input is used numerically. char input would be a pain.
That's not to say the comments by others on validating the input isn't important. Especially when end-users will deliberately or innocently tell lies to get points or crash the system.
Topic archived. No new replies allowed.