Thread: if statements

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    8
    Rep Power
    0

    if statements


    hello:

    i am having some trouble trying to inserting if statements in the right place in order for my program to work. i left out the if statement if B % A ==0. i found out that % means modulo and not divide. my question is how to i fit this if statement into my program below?

    #include<iostream.h>

    int main()

    {
    int num1;

    int num2;

    int result= num2/num1;

    cout<<"please enter two positive integers and press <ENTER>"<<endl;
    cin>>num2>>num1;

    if(num2/num1)

    cout<<"a is"<<num1<<endl;

    if(num2/num1)

    cout<<"b is "<<num2<<endl;

    result = num2/num1;
    if (num2/num1)
    cout<<"the result of b divided by a is"<<result<<endl;



    return 0;



    }
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    what does this program do (or is supposed to do)?

    that is please re-phrase your question. Don't know what you want.



    >> if(num2/num1)

    this is meaningless as it will always be non zero and so true.....


    ie same as

    if(1==1)
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  4. #3
  5. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    >> if(num2/num1)

    this is meaningless as it will always be non zero and so true.....
    How's that? Remember, num1 and num2 are of type int. If you have num1 larger than num2, the result will be 0.
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  6. #4
  7. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    Sorry my bad.....

    did not think about the complier casting the result back to an int before the test.


    I was considering these type of traps for new coders.

    if(!iNum1/iNum2)//this is an error

    if(!(iNum1/iNum2))//this is not an error
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    The initial declaration:
    Code:
    int result= num2/num1;
    is dangerous and unnecessary. num1 is uninitialised, if it happens to be zero, you will get a runtime error.

    Also why are you performing the same test and calculation three times?
    Code:
    result = num2 / num1 ;
    if( result ) 
    {
        cout << "a is " << num1 << endl ; 
        cout << "b is " << num2 << endl ; 
        cout << "the result of b divided by a is " << result << endl; 
    }
    In the end what are you trying to do?

    This code displays a result if num1 is larger than num2, otherwise it does nothing.

    The following would seem more useful:
    Code:
    int quotient = num2 / num1 ;
    int remainder =  = num2 % num1 ;
    cout << "a is " << num1 << endl ; 
    cout << "b is " << num2 << endl ; 
    cout << "the result of b divided by a is " << quotient << "remainder " << remainder << endl ;
    Also it seems counter intuitive to enter the denominator before the numerator. If I enter a then b I would expect a calculation a/b not b/a.

    Finally, post code in code tags so it is readable.

    Clifford.

IMN logo majestic logo threadwatch logo seochat tools logo