CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2012
    Posts
    5

    Talking int cannot equal

    So this is my code and apparently somethings going on at around the if statement. It makes my calculations show up as 0. I know it's some bizarre coding, but Im sure you guys get what's going on. Please let me know of what I can do for the mistake and also what I can do to improve or clean up my code. Thanks.

    Jeremy

  2. #2
    Join Date
    Feb 2012
    Posts
    5

    Re: int cannot equal

    Code:
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    int frank;
    int john;
    int userage;
    
    //the reason I declared one as 1 was because I wasnt able to figure out how to divide a number by a variable in the If statement.
    //Only a variable by a variable.
    
    int one=1;
    
        //Fun
    
        cout << "My name is Jeremy Shawaf \n";
        cout << "Some things i like are desert parties and computers. \n";
        cout << "I like techno music, and I like computer science, hence it being my major :D \n\n";
        cout << "now that we got that out of the way....well frank is 27 years old, and john's age is unknown, but somehow....... \n";
        cout << "We seem to always get the same answer...why dont you enter in your age...\n";
    
    
        cin >> userage;
    
    frank=27;
    john=one/userage;
    
    int totalages=(frank*userage*john);
    
        cout << "now if we multiply your age by john and frank's then we should get " << totalages << " which is frank's age. \n";
    
        if(totalages>=frank){
                cout << "yours, frank's and john's age multiplied together equals " << userage << " which is your age.";
        }
    
        //back to HW
    
    
        return 0;
    
    }

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: int cannot equal

    An int can't hold a decimal value so your john=one/userage; division will make john equal to zero for all values of userage except 1.

    Switch to double if you want to do decimal calculations.

    Edit: See also this thread regarding floating point precision http://www.codeguru.com/forum/showthread.php?t=518005
    Last edited by S_M_A; February 19th, 2012 at 04:07 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Feb 2012
    Posts
    5

    Re: int cannot equal

    Ohhhhhh I see, that completely makes sense. Yeah you cant tell it's a decimal unless you know what the cin is going to be........okay, well, thanks for the heads up Ill try changing it into a double. Thanks for the floating point reference also.

    Jeremy

    Also, I wanted to know......how can I make it so I can multiply numbers with my ints inside of my if statement.

    Such as

    if(john*4*frank>=27*one)
    {
    cout ect ect....;
    }

    How would I go about that?
    Last edited by eyekantbeme; February 19th, 2012 at 09:56 PM.

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: int cannot equal

    That's a perfectly alright statement.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  6. #6
    Join Date
    Feb 2012
    Posts
    5

    Re: int cannot equal

    Okay, that's what I found out after I turned that int into a double, that was the reason why it wasnt working at first, great, thanks for that you guys, I cant wait til I start getting into more complicated code hehe but it's a great approach to think about all the ints and doubles and other values as such.

    I will bring up anything else once it comes up, but with C++ is there much of a reason to clean up code like there is with html and CSS which I know arent languages, but they have their similarities.

    Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured