A program that only lets the user enter a number

I am just learning C++ and I am trying to make a program that displays an error message when the user enters something other than a number.
This is my code:

#include <iostream>
#include <string>
#include "conio.h"
using namespace std;

void main()
{
double num1;
num1 = 0;

cout << "Please enter a number" << endl;
cin >> num1;

while(num1 == 0)
{
cout << "That is not a number, please enter a number" << endl;
cin >> num1;
}

cout << "The number you entered is: " << num1 << endl;

_getch();
}

When I run this and I type in a number it does what it should and says what number I have typed in.
If I run it and type in letters it says "that is not a number please enter a number" as it should except it doesn't wait for me to enter another number, it just keeps on writing that message over and over until I close the console.
What am I doing wrong?
Wow weird one.

If I enter 0, it works, if I enter 21, it works, if I enter 'l', it goes nuts.

Try reading http://www.cplusplus.com/forum/articles/6046/. Maybe this will help. I know there are other ways to determine if an input is a letter or a number. The trick is finding them. I think the problem is with the WHILE conditional.
I don't understand what your thought process is here. Your while conditional doesn't make sense. Whether or not 0 is a number is debatable, but for the purposes of this program I would assume it is. So why would you check if the user enters 0 to see if they entered a number? Because letters are represented as numbers in a computer, if I was to input the letter 'a' into your program, it would output the number 97. A better way to go about this would be to read the input into either a character array or an std::string and then iterate through each character and use the isdigit() function from cctype to determine if the character is a digit. If it a character is found, tell the user that they didn't enter a number. This does prohibit users from entering hexadecimal values, however.
actually the problem is 'I' is a character and your expecting an integer. Since I can enter a multi-byte character anymore c++ takes the approach to not to convert characters to integers like it use to. so if you enter a character on an integer input you have to trap for it like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#include <iostream>
#include <string>
#include "conio.h"
using namespace std;

void main()
{
       double num1;
       num1 = 0;

       cout << "Please enter a number" << endl;
       cin >> num1;

       while(cin.fail()) // the test for the correct input.
       {
                cout << "You entered a invalid number" << endl;
                cout << "Please try again: ";
                cin >> num1;
       }

       cout << "The number you entered is: " << num1 << endl;

       _getch();
}
If istream fails to extract a value, it sets the error bit and does not extract the characters from the stream. You need to clear the error flag and skip the confusing characters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
include <iostream>
#include <string>
#include <limits>
#include "conio.h"
using namespace std;

void main()
{
       double num1;
       num1 = 0;

       cout << "Please enter a number" << endl;
       cin >> num1;

       while(cin.fail()) // the test for the correct input.
       {
                cin.clear(); // reset stream flags
                cin.ignore(std::numeric_limits<int>::max(),'\n'); // skip confusing chars

                cout << "You entered a invalid number" << endl;
                cout << "Please try again: ";
                cin >> num1;
       }

       cout << "The number you entered is: " << num1 << endl;

       _getch();
}
Last edited on
I've been coding in C for far too long now... damn :o
Thanks for the help, I have tried azagaros's code and it didnt work it did the same as my code but andywestken's code worked perfectly thankyou.
@ ascii sorry if my code was bad, I have only been learning code for a few weeks and c++ for 1 week.
Last edited on
Thinking about it a bit more, the code I posted above isn't safe.

If you enter "10 green bottles", the cin >> num1; will extract the 10 leaving "green bottles" alone. So the next extraction from cin will get "green", which might not be what you want.

You really need to check that the stream is empty after getting the number successfully (to check it's a real a real success), or use ascii's approach (read a line from cin into a string, using getline, then check that all the chars are digits (or a decimal point). Then convert to a int (or double) if that's the case.)
Last edited on
Topic archived. No new replies allowed.