c++ - My program will loop regardless of whatever char I enter -
i have been working on very basic calculation program in c++. calculates square root of number, , squares if user wants. have far (i know it's rubbish code, i'm beginner experimenting see how works. suggestions appreciated though):
#include <iostream> #include <stdlib.h> #include <math.h> using namespace std; int number; // global variables used in void functions main. int squarednumber; double sqrtresult; char input; char useagain; void squarenum(); // prototypes void functions void sqrtnum(); void useagainquery(); int main() { retry: // establishing somewhere send user if input invalid. system("cls"); cout << "square calcualtions" << endl; cout << "******************" << endl; cout << endl; cout << "do want square number or find square root of number?" << endl; cout << "please select 1 or 2 respectively." << endl; cout << endl; cin >> input; if (input == '1') { cout << "please press enter continue." << endl; cin.ignore().get(); squarenum(); // if input 1, run void square number. } else if (input == '2') { cout << "please press enter continue." << endl; cin.ignore().get(); sqrtnum(); // if input 2, run void sqrt number. } else if (input != '1' || '2') { system("cls"); cout << "square calcualtions" << endl; cout << "******************" << endl; cout << endl; cout << "your selection invalid, please enter 1 or 2." << endl; cin.ignore().get(); goto retry; // if input isn't either 1 or 2, send start of program. } return 0; } void squarenum() // function square inputted number. { system("cls"); cout << "square calcualtions" << endl; cout << "******************" << endl; cout << endl; cout << "enter number want square." << endl; cin >> number; cout << "you have chosen: " << number << endl; cout << "press enter calculate." << endl; cin.ignore().get(); system("cls"); squarednumber = number * number; // simple maths find square number cout << "you have squared " << number << "." << endl; cout << "the result " << squarednumber << "." << endl; cout << "press enter continue." << endl; cin.get(); useagainquery(); return; } void sqrtnum() { system("cls"); cout << "square calcualtions" << endl; cout << "******************" << endl; cout << endl; cout << "enter number square root of." << endl; cin >> number; cout << "you have chosen: " << number << "." << endl; cout << "press enter calculate." << endl; cin.ignore().get(); system("cls"); sqrtresult = sqrt(number); cout << "you have found square root of " << number << "." << endl; cout << "the result was: " << sqrtresult << "." << endl; cout << "press enter continue." << endl; cin.get(); useagainquery(); return; } void useagainquery() { system("cls"); cout << "square calcualtions" << endl; cout << "******************" << endl; cout << endl; cout << "would make calculation?" << endl; cout << "y yes , n no." << endl; cout << endl; cin >> useagain; if (useagain == 'y' || 'y') { retry2: // establishing somewhere send user if input invalid. system("cls"); cout << "square calcualtions" << endl; cout << "******************" << endl; cout << endl; cout << "do want square number or find square root of number?" << endl; cout << "please select 1 or 2 respectively." << endl; cout << endl; cin >> input; if (input == '1') { cout << "please press enter continue." << endl; cin.ignore().get(); squarenum(); // if input 1, run void square number. } else if (input == '2') { cout << "please press enter continue." << endl; cin.ignore().get(); sqrtnum(); // if input 2, run void sqrt number. } else if (input != '1' || '2') { system("cls"); cout << "square calcualtions" << endl; cout << "******************" << endl; cout << endl; cout << "your selection invalid, please enter 1 or 2." << endl; cin.ignore().get(); goto retry2; } } else if (useagain != 'y' || 'y') return; return; } so yeah, when go through , asks "would play again", goes through on , over. doesn't matter key press loops. appreciated!
change condition here:
if (useagain == 'y' || 'y') to
if (useagain == 'y' || useagain=='y') also, change this:
else if (useagain != 'y' || 'y') { return; } to this:
else if (useagain != 'y' && useagain!='y') { return; }
Comments
Post a Comment