"TechNovice" <ap*********@hotmail.com> wrote in message
news:jWmjb.91103$k74.27929@lakeread05...
that is true.
Is there a function to test for int, or char and fail when there is a
mixture of both types?
No, but one can be written.
I need a function to check for numeric values anf fails when I enter a
char value
OK we've done this part already.
or accepts only char values and fail when I enter int values.
Think about this for a moment. The digits '1', '2', '3'
you type on your keyboard *are* characters. So of course
a character input routine will not reject them. But if
you do want do specify certain restrictions on a character
input, you can write the code to do so.
Also, you said "a function". Which type of data is being
input? It seems you're wanting to input two different
types at once. This doesn't make sense to me.
I think you need a function for numeric input
(which you have), and another for restrictive
character input:
#include <iostream>
#include <string>
void get_text(std::string& input, bool allow_digits = false)
{
do
{
std::cout << "Enter text "
<< (allow_digits
? ""
: "(no digits allowed)") << ": ";
std::getline(std::cin, input);
} while(!allow_digits &&
input.find_first_of("01234567890") != std::string::npos);
}
int main()
{
std::string s;
get_text(s, false);
std::cout << "You entered: " << s << '\n';
get_text(s, true);
std::cout << "You entered: " << s << '\n';
return 0;
}
BTW please don't top-post. Thanks.
-Mike