CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2010
    Posts
    7

    Arrow Why is this program return 0 ?

    #include <iostream>
    int main()
    {
    int i=1;
    using namespace std;
    cout<<i/3<<endl;
    return 0;
    }

    g++ divide.cpp Enter
    ./a.out Enter
    0


    ?????
    Can you help me please urgent ?


    Thank you

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Why is this program return 0 ?

    It returns 0 because you told it to.

    It outputs 0 because and int divided by an int results in an int. If the division results in a remainder, it is discarded.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Why is this program return 0 ?

    "i" and "3" are both integers, therefore the division operation is integer division (rounding down). If you want to see something different, try dividing by 3.0 instead.

    That's why the program displays zero. It also returns zero because you have a big honkin' "return 0" statement, which is the normal way of indicating successful completion.

  4. #4
    Join Date
    Aug 2010
    Posts
    7

    Re: Why is this program return 0 ?

    Oh thanks I see it now,
    Could you show me how to debug the program line by line, I'd like to see how program runs insidely ?

    Thanks

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Why is this program return 0 ?

    You'll need to pass the -g flag when building with g++, and make sure you're *not* passing any optimization flags (if you don't know, then you probably aren't).

    You can then run the program within gdb. The command-line syntax for gdb will have a bit of a learning curve; the most important commands are
    break (create breakpoint on the current line)
    break <filename:linenumber> (create breakpoint on the indicated line)
    break <functionname> (create breakpoint at the indicated function)
    enable/disable <int> (turn breakpoints on or off)
    run (begin or restart the program)
    continue (resume the program after pausing at a breakpoint)
    next (move ahead one line while paused)
    step (attempt to move into a function on the current line)
    finish (attempt to move out of the current function to the next level up)
    print <expression> (display value of a variable, memory location, or expression)

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Why is this program return 0 ?

    There is not much "debug the program line by line" that you can do in such a tiny program. It is just a matter of learning the effects of integer division, and possibly changing i to a double if you want floating point division.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Aug 2010
    Posts
    7

    Re: Why is this program return 0 ?

    Quote Originally Posted by Lindley View Post
    You'll need to pass the -g flag when building with g++, and make sure you're *not* passing any optimization flags (if you don't know, then you probably aren't).

    You can then run the program within gdb. The command-line syntax for gdb will have a bit of a learning curve; the most important commands are
    break (create breakpoint on the current line)
    break <filename:linenumber> (create breakpoint on the indicated line)
    break <functionname> (create breakpoint at the indicated function)
    enable/disable <int> (turn breakpoints on or off)
    run (begin or restart the program)
    continue (resume the program after pausing at a breakpoint)
    next (move ahead one line while paused)
    step (attempt to move into a function on the current line)
    finish (attempt to move out of the current function to the next level up)
    print <expression> (display value of a variable, memory location, or expression)
    Oh noo, that much ?(if such was the money amount for me)

    I am just start learn C++ for a few weeks, I don't think I can memorize all of them, Can you recommend a visual debug tool that is free to download ? I'd love something with mouse clicks on buttons, it's better and easier to learn faster

    There is not much "debug the program line by line" that you can do in such a tiny program. It is just a matter of learning the effects of integer division, and possibly changing i to a double if you want floating point division.
    Thanks for you answer

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Why is this program return 0 ?

    Quote Originally Posted by Jrnoe View Post
    Oh noo, that much ?(if such was the money amount for me)

    I am just start learn C++ for a few weeks, I don't think I can memorize all of them, Can you recommend a visual debug tool that is free to download ? I'd love something with mouse clicks on buttons, it's better and easier to learn faster
    Code::Blocks has a GUI wrapper around GDB on Linux. XCode has one on OSX. On Windows, Visual Studio has its own debugger (not based on GDB).

  9. #9
    Join Date
    Aug 2010
    Posts
    7

    Re: Why is this program return 0 ?

    Thanks, Codeblocks looks cool, I am downloading it now OK ?

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

    Re: Why is this program return 0 ?

    I have never tried the others but Visual Studio has a very good debugger. The express version is free so you might to try it as well.

    A few things you probably need to know though. When creating a project in Visual Studio the default settings that create alot of CG questions are unicode character set and precompiled headers. As a beginner who probably test a lot of code from books and internet you probably should change project setting to multibyte character set and to not use precompiled headers to save yourself from getting a headache.
    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

  11. #11
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Why is this program return 0 ?

    Code::Block's debugger is nice and powerful, although when I first started using it, I didn't find it terribly intuitive. Perhaps because I was used to XCode's *shrug*

  12. #12
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Why is this program return 0 ?

    I like Code::Blocks under Linux, but, to be honest I've been using CodeLite more. I find the project workspace and things like intellisense easier to manage under CodeLite.

    However, the debugger interface on CodeLite, though good, is arguably not quite as good as Code::Blocks... that is when it works... I've had issues with getting the debugger in Code::Blocks to work, like, I think it went wrong, when I moved the file that contains main(). I told the IDE where I'd moved it in the solution workspace, but I couldn't get it to run in debug mode after it compiled. Anyway, I think the moral of the story is that I either need to not move the file that contains main(), or I need to read the Code::Blocks documentation on that one.

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