Char value == 0? Non-numerical values interpreted as zero

I am testing my Calculator program's error message by inputting random letters and punctuation, hoping to see the error message "Error, please make your selection again". However, every non-integer key that I press is read as zero and is diverted to my final if statement:
1
2
3
4
    if (selection == 0)
    {
        exit(1);
    }


That is, the program exits with a value of 1. Why does this happen, and how can I fix my code. Thank you.
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

int AddNum(int, int);
int SubNum(int, int);
int MultNum(int, int);
double DiviNum(int,int);
int ModNum(int, int);
int PowNum(int, int);
double ResultSin(int);
double ResultCos(int);

const float PI = 3.14159265;

int main ()
{
    int selection;
    int num1;
    int num2;
    
    for(;;){
        cout << "********************CALCULATOR*********************" << endl;
        cout << "1. Addition" << endl;
        cout << "2. Subtraction" << endl;
        cout << "3. Multiplication" << endl;
        cout << "4. Division" << endl;
        cout << "5. Modular Division" << endl;
        cout << "6. Raise x to the power of y" << endl;
        cout << "7. Sine(x)" << endl;
        cout << "8. Cosine(x)" << endl;
        cout << " " << endl;
        cout << "0 - Quit" << endl;
        cout << "***************************************************" << endl;
        cout << "Enter a selection please" << endl;
        cin >> selection;
        
        
        if (selection == 1)
        {
            cout << "Enter first number:" << endl;
            cin >> num1;
            cout << "Enter next number:" << endl;
            cin >> num2;
            cout << "The sum of " << num1 << " and " << num2 << " is " << AddNum(num1, num2) << endl;
        }
        else if (selection == 2)
        {
            cout << "Enter first number:" << endl;
            cin >> num1;
            cout << "Enter next number:" << endl;
            cin >> num2;
            cout << "The difference of " << num1 << " and " << num2 << " is " << SubNum(num1, num2) << endl;
        }
        else if (selection == 3)
        {
            cout << "Enter first number:" << endl;
            cin >> num1;
            cout << "Enter next number:" << endl;
            cin >> num2;
            cout << "The product of " << num1 << " and " << num2 << " is " << MultNum(num1, num2) << endl;
        }
        else if (selection == 4)
        {
            cout << "Enter first number:" << endl;
            cin >> num1;
            cout << "Enter next number:" << endl;
            cin >> num2;
            cout << "The quotient of " << num1 << " and " << num2 << " is " << DiviNum(num1, num2) << endl;
        }
        else if (selection == 5)
        {
            cout << "Enter first number:" << endl;
            cin >> num1;
            cout << "Enter next number:" << endl;
            cin >> num2;
            cout << "The remainder of " << num1 << " / " << num2 << " is " << ModNum(num1, num2) << endl;
        }
        else if (selection == 6)
        {
            cout << "Enter first number:" << endl;
            cin >> num1;
            cout << "Enter next number:" << endl;
            cin >> num2;
            cout << num1 << " to the power of " << num2 << " is " << PowNum(num1, num2) << endl;
        }
        else if (selection == 7)
        {
            cout << "Enter number:" << endl;
            cin >> num1;
            cout << "Sine(" << num1 << ")" << " is " << ResultSin(num1) << endl;
        }
        else if (selection == 8)
        {
            cout << "Enter number:" << endl;
            cin >> num1;
            cout << "Cos(" << num1 << ")" << " is " << ResultCos(num1) << endl;
        }
        else if (selection == 0)
        {
            break;
        }
        else
        {
            cerr << "Error, please make your selection again" << endl;
        }
        
        cout << "Press any key to continue" << endl;
        system("read");
        
    };
    
    if (selection == 0)
    {
        exit(1);
    }
    
    return 0;
}

int AddNum(int num1, int num2)
{
    return num1+num2;
}
int SubNum(int num1, int num2)
{
    return num1-num2;
}
int MultNum(int num1, int num2)
{
    return num1*num2;
}
double DiviNum(int num1, int num2)
{
    return num1/num2;
}
int ModNum(int num1, int num2)
{
    return fmod (num1, num2);
}
int PowNum(int num1, int num2)
{
    return pow(num1,num2);
}
double ResultSin(int num1)
{
    return sin(num1*PI/180);
}
double ResultCos(int num1)
{
    return cos(num1*PI/180);
}
Last edited on
If cin >> operator fails, that means that selection will still have its previous value, which in this case is a junk value (which I suppose happens to be 0 in this case, but in reality could be undefined).

You should instead first check to see if the cin >> operation was successful.
1
2
3
4
5
6
7
8
9
if (cin >> selection)
{
    // Success, now you can properly check the value of selection
}
else
{
    cerr << "Error, please make your selection again" << endl;
    cin.clear();
}
Last edited on
That would work, however if I use "cin >> selection" twice it produces an error where I have to enter the input value twice into the prompt before it will register. Is there a way to do this without using "cin >> selection" twice?
I figured out a way to make that work however it is still throwing out related errors. I'm going to mark this as complete though because my question was essentially resolved.
Topic archived. No new replies allowed.