Don't type letters!

Hello, I am really new to C++ and I made a program that does addition (It's my first program). The problem, is, it glitches when you input a letter. I can't seem to figure out how to fix it. Can anyone help? here's the program so far:
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
29
30
31
32
33
34
35
36
#include <cstdlib>
#include <iostream>

using namespace std;

float Addition(float num1, float num2)
{
    return num1 + num2;
}

int main()
{
    begining:
    float number1;
    float number2;
    //addition
    cout << "Insert your first number to add: ";
    cin >> number1;
    cout << "Now your second: ";
    cin >> number2;
    if(number1 + number2 > 1000000000 || number1 + number2 < -1000000000)
    {
               cout << "The answer must be equal to or between -1000000000 and 1000000000\n" <<endl;
               system("pause");
               cout << endl;
               goto begining;
    }
    else
    {             cout << "The answer is: " << Addition(number1, number2);
                  cout <<endl <<endl;
                  system ("pause");
                  cout << endl;
                  goto begining;  
    }
}
 
This is where new users get bit. User input is not easy.

You must read everything the user typed in before pressing ENTER, and then
attempt to validate that as an integer (or whatever you are looking for). Complain if the user goofs up and continue otherwise.

Here is my standard example of how to do it.
http://www.cplusplus.com/forum/beginner/13044/#msg62827
It uses a fancy templated class to make the code to get input pretty, but that isn't the point. The important stuff is on lines 28 through 41 and the loop in main().

Hope this helps.
You could try
1
2
3
4
5
    while ( !(cin >> number1) )
    {
        cin.clear();
        cin.ignore(100, '\n');
    }

instead of just
 
    cin >> number1;
or use getline...
Topic archived. No new replies allowed.