Help conditioning CIN stream

I am working on a simple calculator program, and would like to support conditioned CIN input streams, i.e. only allow whole integer submissions and no letters etc. Below is the code, which works fine as long as everything is entered appropriately.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
 #include <iostream>
#include <iomanip>



using namespace std;

int main()
{
    
    double result, numOne, numTwo;
    int option;
    
    cout << "Please select one of the operations from the list below:  " << endl;
    cout << "1. Addition\n" << "2. Subtraction\n" << "3. Multiplication\n" << "4. Division" << endl;
    
    cin >> option;
    
    
    if(option < 2){
        
        cout << "Please enter two numbers: " << endl;
        cin >> setw(8) >> numOne >> setw(8) >> numTwo;
        
        result = numOne + numTwo;
        
        cout << numOne << " + " << numTwo << " = " << result << endl;
       
        
    }
    else if(option > 1 && option < 3){
        
        cout << "Please enter two numbers: " << endl;
        cin >> setw(8) >> numOne >> setw(8) >> numTwo;
        
        result = numOne - numTwo;
        
        cout << numOne << " - " << numTwo << " = " << result << endl;
        
    }
    else if(option > 2 && option < 4){
        
        cout << "Please enter two numbers: " << endl;
        cin >> setw(8) >> numOne >> setw(8) >> numTwo;
        
        result = numOne * numTwo;
        
        cout << numOne << " * " << numTwo << " = " << result << endl;
    }
    else if(option >3 && option < 5){
        
        cout << "Please enter two numbers: " << endl;
        cin >> setw(8) >> numOne >> setw(8) >> numTwo;
        
        result = numOne / numTwo;
        
        cout << numOne << " / " << numTwo << " = " << result << endl;
    }
    
    
    return 0;
    

    
    
}
Topic archived. No new replies allowed.