Jump to content
  • Advertisement
Sign in to follow this  
ruddj16

loop - printing out value of i

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all I've created a loop (counter) and trying to output the value of I , instead of using an if and test statement like below
for (int i = 0; i < 5; i++)
{
	if (i == 0)
	{
	MessageBox(NULL,"I equals 0","SHUTDOWN ",MB_OK | MB_ICONINFORMATION);
	}
	else
	{
	MessageBox(NULL,"Error.","SHUTDOWN ",MB_OK | MB_ICONINFORMATION);
	}
}
WM_QUIT;



}
as you can see it works but if i do this: MessageBox(NULL, i ,"SHUTDOWN ",MB_OK | MB_ICONINFORMATION); where i should = 0,1,2,3,4 hence the counter but it doesnt even print i ... it errors MessageBox(NULL,i ,"SHUTDOWN",MB_OK | MB_ICONINFORMATION); so it should print out i?? cout << i; works but trying to print the value of i out doesnt Thanks John

Share this post


Link to post
Share on other sites
Advertisement
Three primary answers:

std::stringstream
sprintf (or it's related function snprintf)
boost::lexical_cast

A nice page talking about all of the above

Essentially, MessageBox() is expecting a pointer to a bunch of characters in memory, thus it's expecting a memory address. You're passing it a integer. This integer is a number, and gets interpretted as a memory address. It's certainly not pointing to anything that you mean for it to point to. It's almost definitely pointing at something that you don't have access to.

So the solution is to convert your integer into a series of characters, a string, and then tell MessageBox() where this string is. The above three methods will do that for you. The std::stringstream and boost::lexical_cast would be preferable to sprintf, though. The article should explain things more thoroughly.

Share this post


Link to post
Share on other sites
ok sorry for been dump but this gives a wacky error

char buf[4];
int i = 1 ;
sprintf( buf, "%4d", i );

my understanding of c is somewhat limited, but buf is a buffer of chars so i've created an array of 4 elements and named it buf.
then for i ive given it and int and stored 1 in its memory location.

but it fails big time.....

Share this post


Link to post
Share on other sites
char buf[50];
int n, a=5, b=3;
n=sprintf (buf, "%d a, b, );
printf ("[%s] is a %d chars string\n",buf,n);

why is n needed?

Share this post


Link to post
Share on other sites
n contains the number of letters that were successfully written to the buffer (which is what sprintf returns), so basically it holds the length of the string

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!