Letter crashes math problem

I am very new to coding, and my first project for class is to code a project that adds and multiples two numbers together, I added a third number just to take it a step further for more possible points. I am not sure what to expect for the first grading, but I assume they will try to make my program crash. When I input a letter the program goes haywire and runs the whole way through and spits out random numbers. I was wondering if there was a way to code it, so when you put in a letter it would say Error or make the program restart. I have googled, and tried many things but can't seem to make it to work. Thanks in advance.
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
#include <iostream>
using namespace std;

int main ()
{
        int number_one, number_two, number_three, sum_of_numbers, product_of_nu$

        cout << "Welcome to addition and multiplcation simulator.\n";
        cout << "This program will give you the sum and product of two numbers.$
        cout << "Press return after entering a number.\n";
        cout << "Enter your first number:\n";
        cin >> number_one;
        cout << "Enter your second number:\n";
        cin >> number_two;
        sum_of_numbers = number_one + number_two;
        product_of_numbers = number_one * number_two;
        cout << sum_of_numbers;
        cout << " is the sum of your two numbers.\n";
        cout << product_of_numbers;
        cout << " is the product of your two numbers.\n";
        cout << "Let's take this a step further.\n";
        cout << "Enter a third number:\n";
        cin >> number_three;
        sum_of_all = number_one + number_two + number_three;
        product_of_all = number_one * number_two *number_three;
        cout << sum_of_all;
        cout << " is the sum of all of your numbers.\n";
        cout << product_of_all;
        cout << " is the product of all of your numbers.\n";
        cout << "This is the end of the program.\n";
} 
This problem was discussed in a recent thread:

how to deny non-numeric input?
http://www.cplusplus.com/forum/beginner/109874/#msg600468

It's working with double values there, but you use the same approach with int values.

Andy
Last edited on
Topic archived. No new replies allowed.