Loop test

Why the program exit before I can choose the mathematical operation?

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
#include <iostream>
using namespace std;

int main(){
    int num1 = 0;
    double num;
    double numTotal = 0;
    double numEntered = 0;
    char O;
    while (num != O){
        numTotal = numTotal + num;
        numEntered++;
        
        cout << "Enter a number or 'O' to quit:  ";
        cin >> num;
    
    }
    cout << "Numbers you enterd are: " << numEntered << endl;
    cout << " Press 1 if you want the 'Sum' or\n 2 if you want the diffrance or\n 3 if you want the average or\n 4 if you want the multiplication";
    
    cin >> num1;
    
    if ( num1 == 1 ){
    cout << " The Sum is: " << numTotal + numEntered << endl;
    }
    else if ( num1 == 2 ){
        cout << " The Diffrence is: " << numTotal - numEntered << endl;
    }
    
    else if ( num1 == 3 ){
        cout << " The Average is: " << numTotal / numEntered << endl;
    }
    else if ( num1 == 4 ){
        cout << " The Multiplication is: " << numTotal * numEntered << endl;
    }
    else { cout << "Error";}
    
    return 0;
}
This does not even compile for me.


while (num != O){

You have not initialised either of these variables.
This is "O" not "zero"..
And it compiles for me
And also I didnt understand your word "these variables" can you discuss more please?
while (num != O){

at this exact point in time, what do you think the value of 'num' is? (and your 'O' as well).
Last edited on
You mean it print the "Error" string as I entered it(the charachter"O") , so it should be like what or the code itself is wrong?
Last edited on
No, i mean that when it evaluates that condition you have NOT set 'num' to be anything. It could be ANY value. You need to initialise it like you've done with your other variables.

And you need to initialise 'O' as well (which is a terrible name for a variable).
Love you Got it now.. sorry for bothering you
I was too dumb.. it worked now..
I wanted to thank you again..
Thanks SIR!
nice one :)

sorry for bothering you

You aren't bothering me :)
Topic archived. No new replies allowed.