Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

if else statement in C++

Options
  • 08-04-2008 4:34pm
    #1
    Registered Users Posts: 4,502 ✭✭✭


    Hi in a code i am writing i need to say that if numbers numbers (lets call them fx and fxx) are not equal to zero then it will perform the equation below.But if the if condition is not true (i.e. one of them is equal to zero) then it will display to the user "This problem cannot be solved".

    xnew=xold-(fx/fxx)

    I have tried to do this but it has not worked. not matching if and else!!

    if ((fx=0)or(fxx=0));
    {
    xnew=xold-(fx/fxx);
    }
    else (cout<<"This problem cannot be solved.");


    Some help would be great


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    = is assaignment.
    == is equality comparison.


  • Registered Users Posts: 83,307 ✭✭✭✭Overheal


    Try some coding standard, too: its easier to read for example:
    if ( a == 0 || b == 0 );
    {
    newX = oldX - ( a / b );
    }
    else { 
    cout << "This problem cannot be solved." << endl;
    }
    

    You used the wrong brackets on the else statement. Also, for a one line else statement it is often acceptable (and compiler-acceptable) to not require brackets at all (as above)

    But if you wanted to say else cout error message and return 0, you would need

    else { // curly bracket required as the else covers more than one line of execution
    cout << "cannot compute." << endl;
    return 0;
    }

    so in your case you could write it like:
    if ( a == 0 || b == 0 ) newX = oldX - ( a / b );
    else cout << "This problem cannot be solved." << endl;
    


  • Registered Users Posts: 3,492 ✭✭✭RosieJoe


    chris85 wrote: »
    Hi in a code i am writing i need to say that if numbers numbers (lets call them fx and fxx) are not equal to zero then it will perform the equation below.But if the if condition is not true (i.e. one of them is equal to zero) then it will display to the user "This problem cannot be solved".

    xnew=xold-(fx/fxx)

    I have tried to do this but it has not worked. not matching if and else!!

    if ((fx=0)or(fxx=0));
    {
    xnew=xold-(fx/fxx);
    }
    else (cout<<"This problem cannot be solved.");


    Some help would be great

    Looking at this it looks like you have your logic backwards. From your free text it looks like you should be raising an error when either value is 0 not trying to evaluate the equation. If not, then you could have a divide by zero.

    Try:

    if ( a == 0 || b == 0)
    {
    cout << "This problem cannot be solved." << endl;
    }
    else
    {
    xnew = xold - (a/b);
    }


  • Registered Users Posts: 4,502 ✭✭✭chris85


    RosieJoe wrote: »
    Looking at this it looks like you have your logic backwards. From your free text it looks like you should be raising an error when either value is 0 not trying to evaluate the equation. If not, then you could have a divide by zero.

    Try:

    if ( a == 0 || b == 0)
    {
    cout << "This problem cannot be solved." << endl;
    }
    else
    {
    xnew = xold - (a/b);
    }

    Sorry meant this

    if ((f<>0)and(fxx<>0))
    {
    xnew=xold-(fx/fxx)
    }
    else (cout<<"This problem cannot be solved.");


    Anyway thanks for the help it is now working using the code given This is for engineering so dont know much about programming but the program is now working :)


  • Registered Users Posts: 83,307 ✭✭✭✭Overheal


    ....................................................what.

    nobody in their right mind would attempt using <> or >< as an operator and im not even sure its valid.

    use != // not equal to


  • Advertisement
  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    Don't forget the && for the AND operator:)


  • Registered Users Posts: 4,502 ✭✭✭chris85


    Overheal wrote: »
    ....................................................what.

    nobody in their right mind would attempt using <> or >< as an operator and im not even sure its valid.

    use != // not equal to

    Well the help menu says to use is as a not equal to operator but it doesnt work in the program so not valid.

    Dont use this stuff much so sory if its stupid question


  • Registered Users Posts: 4,502 ✭✭✭chris85


    Also one more quick question guys. I output an answer to the user and it gives correct to say five decimal places. How can i get the program to give it to seven or eight decimal plaes. I think its something to do with adding in a line to set percision but aint working for me


  • Registered Users Posts: 17,727 ✭✭✭✭Sherifu


    chris85 wrote: »
    Also one more quick question guys. I output an answer to the user and it gives correct to say five decimal places. How can i get the program to give it to seven or eight decimal plaes. I think its something to do with adding in a line to set percision but aint working for me
    Something like << setprecision(8) << to specify decimal places afair.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    chris85 wrote: »
    Also one more quick question guys. I output an answer to the user and it gives correct to say five decimal places. How can i get the program to give it to seven or eight decimal plaes. I think its something to do with adding in a line to set percision but aint working for me
    what help menu, what IDE you using to write your C++?

    double f =12.345678;
    cout << setprecision (5) << f << endl; //prints 12.34568 rounds up only rounds up for the cout statement, the value in the variable is untouched.
    cout << setprecision (9) << f << endl; //prints 12.345678 precision is bigger than actual double, precision is set set but the original double number is still the same.


  • Advertisement
  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    Overheal wrote: »
    ....................................................what.

    nobody in their right mind would attempt using <> or >< as an operator and im not even sure its valid.

    use != // not equal to

    Maybe he's thinking of VB where the not equal to operator is <>


  • Registered Users Posts: 4,502 ✭✭✭chris85


    Sorry yes writing it in VB.


Advertisement