By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,499 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,499 IT Pros & Developers. It's quick & easy.

Another newbie question

P: 1
Hello yes I am a beginner at writing C++ and have been using Visual Studio C++ 2008, my problem is that when I enter this very simple code

#include <stdio.h>

int main( void )
{
int counter = 1;

do {
printf( "%d " , &counter );
} while ( ++counter <= 10 );

return 0;

}

this code is supposed to simply count from 1 to 10 but instead the results are this

1245024 1245024 1245024 1245024 1245024 1245024 1245024 1245024 1245024 1245024
Press any key to continue...

I have checked the code in the book to the code I wrote numerous times and cannot find anything different except the results they display in the book actually work

Any help would be appreciated,
Nov 5 '08 #1
Share this Question
Share on Google+
5 Replies


100+
P: 175
where you have "&counter", make it "counter"

what's printing out is the MEMORY ADDRESS of counter, that's the 124... whatever number you're getting. When you put "&" before a variable like you have, you are getting the ADDRESS of it, NOT the value. It's quite possible it's a typo in your book ... I've seen good books with code filled with typos.


...and by the way, your code there is C not C++.
Nov 5 '08 #2

Expert 10K+
P: 11,448
Hello yes I am a beginner at writing C++
Please don't hijack somebody else's thread; it is considered rude. I've split your
thread (and reply) to its own thread.

kind regards,

Jos (moderator)
Nov 5 '08 #3

Expert 100+
P: 2,415
You may also what some separator between each printed value, maybe a space or a newline.
Nov 5 '08 #4

boxfish
Expert 100+
P: 469
There is a separator; a space after the %d.
Nov 5 '08 #5

100+
P: 127
The perfect answer was given by "manontheedge" !! Remove the "&" and rerun the program. It will be fine!
Nov 7 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.