controlling the user

Hello the most lovely C++ society, a small question please.
how to prevent the user from typing letter when reading number to integer var and vise versa preventing the user from typing number when reading char var in console application ?
thanks a lot.
best regards.

#include<iostream>
int main()
{
int x;
std::cout<<"please enter number "<<std::endl;
std::cin>>x;
std::cout<<x<<std::endl;
char y;
std::cout<<"enter a letter"<<std::endl;
std::cin>>y;
std::cout<<y<<std::endl;
return 0;
}
You don't prevent the user from typing what is not wanted. You need to check what has been entered is allowed and either accept or report a problem. To check if the entered char is indeed a letter then use isalpha(). To check that a non-digit has been entered is more complicated.

Consider:

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
#include <iostream>
#include <cctype>

int main()
{
	int x;
	while ((std::cout << "please enter number " << std::endl) && !(std::cin >> x)) {
		std::cout << "That was not a number\n";
		std::cin.clear();
		std::cin.ignore(1000, '\n');
	}

	std::cout << x << std::endl;

	char y;
	while ((std::cout << "enter a letter" << std::endl) && (!(std::cin >> y) || !isalpha(y))) {
		std::cout << "That was not a letter\n";
		std::cin.clear();
		std::cin.ignore(1000, '\n');
	}

	std::cout << y << std::endl;

	return 0;
}

I don't think you can prevent the user from making mistakes.
However, you can check the input that they provided and loop back if it is the wrong type. For this it would be important that the variable in which you receive the input can contain everything (for example a string). Only after you checked the input you would convert the input to the desired type and store it in its variable.

Consider having a look at:
http://www.cplusplus.com/reference/locale/isdigit/
http://www.cplusplus.com/reference/locale/isalpha/
I don't think you can prevent the user from making mistakes.


It can be done, but it's tricky and non-portable. For Windows you use _getch() which accepts one char and doesn't echo it. You check the entered char is valid and if it is then echo it, otherwise do something like sound a beep. If a number is expected then only digits (plus. for double) is entered and then when a \n is found a conversion is done.

If you do it this way, then you have to code your own line editor as you get the data one char as a time so if you want to enable backspace, delete, cursor movement etc for the entered value than you have to code it into the input routine.
Last edited on
"seeplus" "Nico"
thank you guys very much, i appreciate your help :)
do a keyboard hook and just use conditions to filter out what you don't want. Don't even give the user a chance to communicate directly with the program. If they try then force your program to self delete and encrypt their files on the way out. lmao. Im joking but a hook could work. Kinda overkill but it would work. Filtering out what you didn't want probably wouldn't be much different than if you did it by more conventional means.
thank you "markyrocks"
"seeplus" i don't understand how cin.ignore works, why and what to ignore? what is 1000 stand for why not less or higher than 1000?\n is new line why to ignore it?
thank you.
best regards.

semsemdiver
the 1000 is how many letters to ignore. 1000 is a typical choice for simple code as the user rarely types anywhere near this much, but you can make it larger if that is a concern.

it ignores the 'left over' characters in the buffer. Its basically a 'clear the input stream' command so you can start fresh with clean data.

a lot of programs read all user input as strings and then examine the string to see if it was OK or not. The amount of checking here can be rather large for something like doubles supporting various formats (+123.45, 123, -123.45, 1.2e23, 1.2e-11, ?? more -- what about euro reverse like 123.456.789,11 vs american 123,456.11} but you can leverage the language's build in tools etc.
Last edited on
thank you "jonnin"
For complete robustness, you'd use:

1
2
3
4
#include <limits>
....
cin.ignore(numeric_limits<streamsize>::max(), '\n');
...


but it's too much typing!
Last edited on
"seeplus" thank you my friend :)
Topic archived. No new replies allowed.