I guess part of the confusion might be that if you look at the docs it says that isdigit expects an int as argument.
https://cplusplus.com/reference/cctype/isdigit/
But it actually expects you to pass a char. And the int that it returns is meant to be treated as a bool. Note that both char and bool are implicitly convertible to/from int so this works without having to do anything special most of the time. I think the reason why they choose to do it like this has to do with the fact that old C (from where this function originates) didn't have a bool type and for consistency with other functions that work on chars and could be passed the special value EOF that is outside the char range of values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <cctype>
int main()
{
char ch;
while (std::cin >> ch)
{
if (std::isdigit(ch))
{
std::cout << "'" << ch << "' is a digit.\n";
}
else
{
std::cout << "'" << ch << "' is not a digit.\n";
}
}
}
| |
Use Ctrl+Z (Windows) or Ctrl+D (elsewhere) to exit the loop.
char is actually an integer type. Each number represents a character. Look at an ASCII table to see how it's usually done:
https://www.asciitable.com/
Note that the character '0' is represented as the value 48.
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>
int main()
{
std::cout << "Enter a character: ";
char c;
std::cin >> c;
std::cout << "Enter an integer: ";
int i;
std::cin >> i;
std::cout << "You have entered the character '" << c << "' which is represented by the character code " << int(c) << ".\n";
std::cout << "You have also entered the integer " << i << ".\n";
if (c == i)
{
std::cout << "'" << c << "' and " << i << " is the same value (but the type is different).\n";
}
else
{
std::cout << "'" << c << "' and " << i << " are two different values.\n";
}
}
| |
Enter a character: 5
Enter an integer: 5
You have entered the character '5' which is represented by the character code 53.
You have also entered the integer 5.
'5' and 5 are two different values.
|
Enter a character: 7
Enter an integer: 55
You have entered the character '7' which is represented by the character code 55.
You have also entered the integer 55.
'7' and 55 is the same value (but the type is different). |
So when you use cin to read an integer you won't get the character representation (what would that even mean if the user entered an integer with multiple digits?).
You will instead get the integer representation and passing that to isdigit doesn't make sense.
Note that an int variable can only contain integer values. You can never try to read a value and then check the value to see if it's an integer. An int ALWAYS has an integer value. What you need to do instead is to check if cin failed. There are a few different ways you can do this...
1 2 3 4 5 6
|
int answer;
std::cin >> answer;
if (std::cin.fail())
{
// deal with incorrect input...
}
| |
1 2 3 4 5 6
|
int answer;
std::cin >> answer;
if (!std::cin)
{
// deal with incorrect input...
}
| |
1 2 3 4 5
|
int answer;
if (!(std::cin >> answer))
{
// deal with incorrect input...
}
| |
All of these do the same thing. Normally I prefer the last one. It's especially convenient when writing it as a loop.
If an input operation fails cin will enter a "fail state" and any input operation that follows will automatically fail. What you normally need to do is to first get out of the "fail state" and then remove the incorrect input before continuing.
1 2 3 4 5 6 7
|
int answer;
if (!(std::cin >> answer))
{
std::cout << "You entered an invalid integer!\n";
std::cin.clear(); // leave "fail state"
std::cin.ignore(1000, '\n'); // discard whole line of input (or up to 1000 character whichever comes first)
}
| |
Going back to your code...
You might want to replace line 23-28 with something like the following.
1 2 3 4 5 6
|
while (!(cin >> answer))
{
cout << "invalid response type, please tell enter an int";
cin.clear();
cin.ignore(1000, '\n');
}
| |
Note that if you forgot to call clear() here the ignore and >> would continue to fail forever which would result in an infinite loop.