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');
}
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.