if and else problems

Hi. I writed a simple calculator and i dont know why when i type q, the loop doesnt end. Also when i write an unknown operator, the error message doesnt show up. Any ideas how to fix this?

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

float result;
char oper_char;
float value1;
float value2;

int main()
{
    cout<<"addition -> +"<<endl;
    cout<<"subtraction -> -"<<endl;
    cout<<"multiplication -> *"<<endl;
    cout<<"division -> /"<<endl;
    cout<<" "<<endl;
    cout<<"To exit the calculator press q + Enter"<<endl;
    cout<<"Ctrl (Control) + C to abort."<<endl;
    cout<<" "<<endl;
    result = 0;
    while (1) {
          cout<<"Enter operator and the two numbers respectively."<<endl;
          cin>>oper_char;
          
          if (oper_char == 'q') 
          break;
                        
          cin>>value1;
          cin>>value2;
          if (oper_char == '+') {
                        result = value1 + value2;
                        cout<<"Result: "<<result<<endl;
                        } else if  (oper_char == '-') {
                             result = value1 - value2;
                             cout<<"Result: "<<result<<endl;
                             } else if (oper_char == '/') {
                                           if ((value1 == 0) || (value2 == 0)) {
                                                      cout<<"Error: Cannot divive by zero!"<<endl;
                                                      cout<<"Operation ignored!"<<endl;
                                                      } else {
                                                              result = value1 / value2;
                                                              cout<<"Result: "<<result<<endl;
                                                              }
                                                      } else if (oper_char == '*') {
                                                         result = value1 * value2;
                                                         cout<<"Result: "<<result<<endl;
                                                         } else {
                                                                 cout<<"Unknown operator "<<oper_char<<endl;
                                                                 }
                                                         }
    return 0;
}
So if you press Q and then Enter the program doesn't end?
Using break there doesn't do anything. Break is used for switch statements and not if statements. Replace it with return 0; Secondly, you need to learn to properly indent your code because it's harder to understand when it's indented like you have it now.
@Niven: break can also be used to break out of loops, which is the case here.
@Peter87 Yeah thats right, the program doesn't end. Also the "else" in the end doesnt show the message when the requirements are met.
So when you input q you can still give input and use the program like normal? Strange ...

Note that when you input an unknown operator you will have to first input the two numbers before it prints "Unknown operator".
@Peter87 it is very strange indeed... is that it, that i have to put the operator after the two numbers? because as you can see up there, i have made the operator the first one to be entered...
No the operator goes first. I just meant you need to also input the two numbers to reach the line that prints "Unknown operator".

Inputting q works for me. I can't understand why it doesn't work for you.
@Peter87 alright, i got that about the operator, but it still doesnt quit when i input q... i shall rewrite it on a new file and recompile it. Maybe will it work?
Topic archived. No new replies allowed.