How to prevent a user to enter a space between characters and numbers

Hi, I'm trying to prevent the user to input a space between characters or numbers. The user can only enter 15 character with any combination with numbers. Any ideas on how to do it? this is my code:
if (input.length() <= 15) {
index = 0;
do {
// check if characters are letters and digits
for (int i = 0; i < input.length(); i++){

if (!(isalpha(input[i])) && (!(isdigit(input[i]))) && ((isspace(input[i]))) )
valid = false;
else
valid = true;
index++;

}
As far as I know you can't prevent them from not typing something (maybe some OS specific function to manipulate the console can?), but you can format what they type to your liking.

Example:

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

int main()
{
	std::string str = "This String Has Spaces!";

	std::cout << "Unformatted string: " << str << "\n\n";

	std::streamsize pos;

	while ((pos = str.find_first_of(' ')) != std::string::npos)
	{
		str.erase(pos, 1);
	}

	std::cout << "Formatted string: " << str;

	return 0;
}
Last edited on
You comment "As far as I know you can't prevent them from not typing something" is funny
I was talking in software terms, not mechanical !
Last edited on
I meant software-wise. On windows you can hook into the console and do all sorts of stuff. I'm not sure if what you want is something you can do though.
Topic archived. No new replies allowed.