Adding Conditions for the CIN function

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;
    

    
    
}
Sample output looks like :

Please select one of the operations from the list below:
1. Addition
2. Subtraction
3. Multiplication
4. Division
4
Please enter two numbers:
300
10
300 / 10 = 30
I am not sure if this helps but this generally how I handle user input (for assignments)

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
#include<iostream>


using namespace std;

int menu() {

    int userinput = 0;
    cout << "Please select one of the operations from the list below: " << endl;
    cout << "1. Addition" << endl;
    cout << "2. Subtraction" << endl;
    cout << "3. Multiplication" << endl;
    cout << "4. Division" << endl;
    cout << "5. Quit" << endl;
    cin >> userinput;
    return userinput;
    
}

int main()
{
    bool runMenu = true;



    while (runMenu) {
        switch (menu()) {
        case 1:
            cout << "you pressed 1" << endl;
            break;
        case 2:
            cout << "you pressed 2" << endl;
            break;
        case 3:
            cout << "you pressed 3" << endl;
            break;
        case 4:
            cout << "you pressed 4" << endl;
            break;
        case 5:
            cout << "Goodbye!" << endl;
            runMenu = false;
            break;
        default:
            break;
        }

    }
    return 0;
}


You would have to work you program logic into the the switch statement.
I would most likely write a function for each operation.
Or just manipulate the operands to print to standard out.

You could also use isalpha

http://www.cplusplus.com/reference/cctype/isalpha/
Last edited on
I don't think you need to worry about letters or symbols. Because you declared option as an int data type. An int data type can only hold 1 integer value.

So think about what you're trying to do.
The only valid integers for option are 1, 2,3 or 4.
You just need to include one more logic conition which says that
if option is less than 0 OR greater than 4
then cout a "invalid option entered" statement.



Last edited on
Topic archived. No new replies allowed.