variables - C++ Constant char -
i built simple calculator out of c++, uses chars, (+,-*,/) works math operators when user inputs '=' 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 calculator\n\ninput 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 now...\n"; return 0; } } } else { cout << "unknown error! program closing..."; return 0; } return 0; }
when user uses operation besides = works perfectly, if use , equals sign doesn't work.
example program out put:
welcome calculator input 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 "=" , if it's equal "=". think planned put closure operator or in first outer if.
Comments
Post a Comment