c++ - save while loop information after y/n response -
i having problem code can't figure out how fix it.
i have made calculator in c++ using dev-c++. made while loop user doesn't have restart program use again. trying add ability calculated answer used in next calculation code being skipped.
#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { string username; float num1, num2, answer; string berekening; string again; float oldanswer; string oldanswerq; oldanswer = answer; again = "y"; cout << "hello you?" << endl; cout << "" << endl; cin >> username; cout << "" << endl; cout << "well hello " << username << endl; cout << "" << endl; while (again == "y"){ if (oldanswer == 0) { cout << "what first number wanna put in " << username << endl; cout << "" << endl; cin >> num1; } else { cout << "do wanna use old answer? y/n" << endl; cout << "" << endl; cin >> oldanswerq; } cout << "" << endl; cout << "+, -, x or ÷(u can use / instead of ÷" << endl; cout << "" << endl; cin >> berekening; cout << "" << endl; cout << "and second number " << username << endl; cout << "" << endl; cin >> num2; cout << "" << endl; if (berekening == "+"){ answer = num1 + num2; } else if (berekening == "-"){ answer = num1 - num2; } else if (berekening == "x"){ answer = num1 * num2; } else if (berekening == "/"){ answer = num1 / num2; } else if (berekening == "÷"){ answer = num1 / num2; } cout << username << ", choosed " << berekening << " did was: " << num1 << berekening << num2 << "=" << answer << endl; cout << "" << endl; cout << username << ", wanna go again? y/n" << endl; cout << "" << endl; cin >> again; cout << "" << endl; } }
i new c++ , welcome suggested improvements.
you set oldanswer
equal newanswer
outside loop, should done inside loop.
you should not use ==
when comparing float
equal (decimals can accurate number of places on computer)
Comments
Post a Comment