Stumped. The nested if statement stops.(begginer) C++
Go to solution
Solved by KuroSetsuna29,
Thank you for the help but i am only supposed to have 4 cin.
so i changed it up a bit.
However with this change i cannot stop it.
Example: if i want sqrt it will ask me for the rest of the numbers which i do not want.
Well if your constraint is limited to 4 cin, then I have restructured xshockz answer:
#include <iostream>#include <cmath> using namespace std; int main(){ double numb1,numb2, numb3; string operation; cout << "Please choose and operation:"; cin >> operation; cout << "Please enter your " << ((operation == "sqrt" || operation == "fabs") ? "" : "first ") << "number: "; cin >> numb1; if (operation == "addition" || operation == "subtraction" || operation == "multiplication" || operation == "division" || operation == "pythagorean" || operation == "quadratic") { cout << "Please enter your second number: "; cin >> numb2; } if (operation == "quadratic") { cout << "Please enter your third number: "; cin >> numb3; } if (operation == "sqrt") cout << "sqrt(" << sqrt(numb1) << ")"; else if (operation == "fabs") cout << "fabs(" << fabs(numb1) << ")"; else if (operation == "addition") cout << "addition(" << numb1+numb2 << ")"; else if (operation == "subtraction") cout << "subtraction(" << numb1-numb2 << ")"; else if (operation == "multiplication") cout << "multiplication(" << numb1*numb2 << ")"; else if (operation == "division") cout << "division(" << numb1/numb2 << ")"; else if (operation =="pythagorean") cout << "pythagorean(" << sqrt(numb1*numb1+numb2*numb2) << ")"; else if (operation == "quadratic") cout << "quadratic(" << (-numb2 + sqrt(numb2 * numb2 - 4 * numb1 * numb3)) / (2 *numb1)<< ", " << (-numb2 - sqrt(numb2 * numb2 - 4 * numb1 * numb3)) / (2 *numb1) << ")"; return 0;}
I didn't bother checking the math, so you may want to verify. Also forgive any syntax error as I don't work in C++.
You may also note that no nested if statements were required. If it was a requirement in your constraints, do as you will with it.
And if you wanted to post your modified code, we could take a look and see why it doesn't stop asking for the next number.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now