Verify numeric input

Hi, i'm trying to make a simple program that asks for numeric input and then tells the user if it is numeric or not. The program will catch if you enter a single letter or multiple letters followed by numbers, problem is if you enter something like for example 99x9, it will not catch it. I know you can use cin.peek in the while statement to catch it, but i was wondering if there was another way just using #include<isotream> or #include<string>. i was told getline might work but i don't know to make strings into doubles yet.
Here's the code.

#include <iostream>
int main()
{
double num = 0;
std::cout << "Enter a number: " << std::endl;

while ( !(std::cin >> num))
{
std::cout << "That''s not valid input. Try again" << std::endl;
std::cin.clear();
std::cin.ignore(1000, '\n');
}

return(0);
}
1
2
3
4
5
6
int n;
cin >> n;
if(!cin) // or if(cin.fail())
{
    // user didn't input a number
}


From: http://stackoverflow.com/questions/5655142/how-to-check-if-input-is-numeric-in-c
thanks for the response Drakon. The code you put does the same thing as i code i made. if for example i input 1x, the program still takes it as a number. I wanting to make the program catch something like 1x and let the user know it's not a number.
sorry for my overly late reply. You could try using two for loops one hold numbers in a string like
1
2
3
4
5
6
string Numbers = "1234567890";
string Input = "";
std::cin >> Input;
for (int x = 0; x < Input.length();x++){
if (Input[x] = Numbers[x];
}

Or something some such like that, I can't work up the code now but work with something like that
Topic archived. No new replies allowed.