Jump to content

Stumped. The nested if statement stops.(begginer) C++

Icycookies
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.

#include <iostream>#include <cmath>using namespace std;int main(){   int addition, subtraction, division, multiplication;    double quadractic,pythagorean, numb1,numb2, numb3;    string operation;    cout << "Please choose and operation:";    cin >> operation;    if (operation == "sqrt" || operation == "fabs" || operation== "addition" || operation == "subtraction" || operation== "multiplication" || operation== "division"|| operation== "quadractic" || operation== "quadractic")        cout << "Please enter your first number: ";        cin >> numb1;        if  (operation=="sqrt")        cout << "sqrt" << sqrt(numb1)<< ")";        else if (operation=="fabs")        cout << "fabs" << fabs(numb1) << ")";        {                        cout << "Please enter your second number: ";            cin >> numb2;            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) << ")";            {                                cout << "Please enter third number: ";                cin >> numb3;                operation == "quadractic";                cout << "quadractic(" << (-numb2 + sqrt(numb2 * numb2 - 4 * numb1 * numb3)) / (2 *numb1)<< "," << (-numb2 - sqrt(numb2 * numb2 - 4 * numb1 * numb3)) / (2 *numb1) << ")";                            }        }    return 0;}

I had cout << statement; it worked fine but i am not using that. So how did it allow it to do the other ifs? I noted where it stops.

Link to comment
Share on other sites

Link to post
Share on other sites

They way you have your if's arranged is really odd.

It should be just a bunch of separate if's, not sure why you have them nested.

 

Maybe explain in pseudo code what you want your program to do.

 

I would just print out a menu of options with numbers and then give the user a prompt to enter the number of their choice, then use a switch statement with that number.

 

A switch statment looks like this:

 

switch (x) {  case 1:  case 2:  case 3:    cout << "x is 1, 2 or 3";    break;  default:    cout << "x is not 1, 2 nor 3";  }

 

where x would be the number you ask for and each case would execute the code that is under the case for the number that was input.

Default would be for if there is no case for the value given.

Link to comment
Share on other sites

Link to post
Share on other sites

They way you have your if's arranged is really odd.

It should be just a bunch of separate if's, not sure why you have them nested.

 

Maybe explain in pseudo code what you want your program to do.

 

I would just print out a menu of options with numbers and then give the user a prompt to enter the number of their choice, then use a switch statement with that number.

program gives option to choose operation.

if it is sqrt or fabs

the user to enter a number.

if not sqrt or fabs go to other if statement.

if addition,subtraction, division, multiplication or quadratic.

user asked to enter a second number

if pyth

user asked to enter a third number

Link to comment
Share on other sites

Link to post
Share on other sites

program gives option to choose operation.

if it is sqrt or fabs

the user to enter a number.

if not sqrt or fabs go to other if statement.

if addition,subtraction, division, multiplication or quadratic.

user asked to enter a second number

if pyth

user asked to enter a third number

 

Oh, well you need to not put the ifs inside of each other.

The way you have it set up right now, if you write anything other than sqrt or fabs, I bet it doesnt work. You need to get all of your ifs separated.

 

EX:

if (ans == "sqrt"){//do sqrt math}if (ans == "fabs"){//do fabs math}

etc....

Link to comment
Share on other sites

Link to post
Share on other sites

Well I changed your code slightly, and arranged it so it should all work. All you need to do is fix the math and stuff 

 

#include <iostream>#include <cmath> using namespace std; int main(){     double numb1,numb2, numb3;    string operation;     cout << "Please choose and operation:";    cin >> operation;     if (operation == "sqrt" || operation == "fabs" ){        cout << "Please enter your number: ";        cin >> numb1; if  (operation=="sqrt")cout << "sqrt(" << sqrt(numb1)<< ")";         if (operation=="fabs")        cout << "fabs(" << fabs(numb1) << ")"; //runs fine here but it does not go to the other ifs}     if (operation== "addition" || operation == "subtraction" || operation== "multiplication" || operation== "division"|| operation== "pythagorean"){         cout << "Please enter your 1st number: ";        cin >> numb1;        cout << "Please enter your 2nd number: ";        cin >> numb2;         if (operation=="addition")            cout << "addition("<< numb1+numb2 << ")";         if (operation== "subtraction")            cout << "subtraction(" << numb1-numb2 << ")";         if  (operation=="multiplication")            cout << "multiplication(" << numb1*numb2<< ")";         if (operation== "division")            cout << "division(" << numb1/numb2 << ")";         if (operation=="pythagorean")            cout << "pythagorean(" << sqrt(numb1*numb1+numb2*numb2) << ")";     } if (operation== "quadratic"){                        cout << "Please enter your 1st number: ";            cin >> numb1;            cout << "Please enter your 2nd number: ";            cin >> numb2;cout << "Please enter your 3rd number: ";            cin >> numb3;             cout << "quadratic(" << (-numb2 + sqrt(numb2 * numb2 - 4 * numb1 * numb3)) / (2 *numb1)<< ", " << (-numb2 - sqrt(numb2 * numb2 - 4 * numb1 * numb3)) / (2 *numb1) << ")";}     return 0; }
Link to comment
Share on other sites

Link to post
Share on other sites

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.

Link to comment
Share on other sites

Link to post
Share on other sites

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.

Link to comment
Share on other sites

Link to post
Share on other sites

If this were me, I would use a switch case where the choice is a char.

When the program runs, output the options for the user (e.g. "a. multiplication")

Normally for programs like this I do a "do-while" statement so that the user can keep on running the program until they decide to stop. Unfortunately switch statements in c++ do not support strings (I hear that swift supports strings in switch statements), thus is why I always use characters (e.g. a-f) to represent choices. 

 

The best thing about using switch cases is that you have a default case (if case the user enters an incorrect choice), so that you can do a cout<<"Incorrect choice, please try again\n";

A switch case is basically 

if

else if

else if 

....

 

 

The main problem with your program is that if a user makes a spelling typo or enters an unsupported operation, your program will not inform the user of said error. Anyway I have to learn front end development for a project , good luck.

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×