简体   繁体   中英

C++ Constant char

I built a simple calculator out of c++, it uses chars, so (+,-*,/) will works as math operators but when a user inputs an '=' it does't work.

#include <iostream>
#include <string>
#include <sstream>
#include <math.h>  
using namespace std;

#define PI 3.14159265359
#define NEWLINE '\n' 


void printf(string string)
{ 
  cout << string;
}

int main ()
{
char operation;
double a,b,c, value;
double answer = 0;
bool more = true;

cout << "Welcome to My Calculator\n\nInput a value: ";
cin >> answer;
operations:

cin >> operation;

if (operation != '=') {
    if (operation == '+') {
        cin >> value;
        cout << "\n" << answer << " " << operation << " " << value << "\n";
        answer += value;
        cout << "Equals " << answer << "\n";
        cout << answer << " - New Operation? ";
        goto operations;
    }
    if (operation == '-') {
            cin >> value;
            cout << "\n" << answer << " " << operation << " " << value << "\n";
            answer -= value;
            cout << "Equals " << answer << "\n";
            cout << answer << " - New Operation? ";
            goto operations;
        }
    if (operation == '*') {
            cin >> value;
            cout << "\n" << answer << " " << operation << " " << value << "\n";
            answer *= value;
            cout << "Equals " << answer << "\n\n";
            cout << answer << " - New Operation? ";
            goto operations;
        }
    if (operation == '/') {
                cin >> value;
                cout << "\n" << answer << " " << operation << " " << value << "\n";
                answer /= value;
                cout << "Equals " << answer << "\n\n";
                cout << answer << " - New Operation? ";
                goto operations;
            }
    if (operation == '^') {
                    cin >> value;
                    cout << "\n" << answer << " " << operation << " " << value << "\n";
                    answer = pow(answer, value);
                    cout << "Equals " << answer << "\n\n";
                    cout << answer << " - New Operation? ";
                    goto operations;
                }
    if (operation == '=') {
                    cout << "\nFinal Answer = " << answer << "\n\nNew operation [yes/no]: ";
                    string check;
                    cin >> check;
                    if (check == "yes") {
                        cout << "\nInput value: ";
                        cin >> answer; 
                        cout << "\n";
                        goto operations;
                    } else {
                        cout << "\nGoodbye for now...\n";
                        return 0;
                    }

                }
} else {
    cout << "Unknown Error! Program Closing...";
    return 0;
}


return 0;
}

When a user uses any operation besides = it works perfectly, but if I use and equals sign it doesn't work.

Example program out put:

Welcome to My Calculator

Input a value: 4
+4

4 + 4
Equals 8
8 - New Operation? - 3

8 - 3
Equals 5
5 - New Operation? * 5

5 * 5
Equals 25

25 - New Operation? /2

25 / 2
Equals 12.5

12.5 - New Operation? ^2

12.5 ^ 2
Equals 156.25

156.25 - New Operation? =
Unknown Error! Program Closing...
if (operation != '=') {
    ...
    if (operation == '=') {
    }
}

If operation isn't equal to "=" and if it's equal to "=". I think you planned to put a closure operator or something like that in the first outer if.

您的语句if (operation != '=')使控件转移到else语句

Because you have an if (operation != '=') outside the if checking for = . You don't want to do that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM