-
August 4th, 2010, 01:36 PM
#1
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
-
August 4th, 2010, 01:41 PM
#2
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.
-
August 4th, 2010, 01:42 PM
#3
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.
-
August 4th, 2010, 01:47 PM
#4
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
-
August 4th, 2010, 01:52 PM
#5
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)
-
August 4th, 2010, 01:53 PM
#6
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.
-
August 4th, 2010, 02:07 PM
#7
Re: Why is this program return 0 ?
Originally Posted by Lindley
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
-
August 4th, 2010, 02:11 PM
#8
Re: Why is this program return 0 ?
Originally Posted by Jrnoe
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).
-
August 4th, 2010, 02:20 PM
#9
Re: Why is this program return 0 ?
Thanks, Codeblocks looks cool, I am downloading it now OK ?
-
August 5th, 2010, 04:00 AM
#10
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.
-
August 5th, 2010, 08:06 AM
#11
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*
-
August 5th, 2010, 09:20 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|