C Do/While Loop problem

Started by
19 comments, last by alvaro 11 years, 4 months ago
Hello, I am learning C at the moment and I just made a very simple program to get started with the basics, but I keep getting an error with my while statement and have no idea why! Can anyone please help me see what I am doing wrong?

[source]
#include <stdio.h>
#include <time.h>

int CheckNumbers(int, int);

int main()
{
// Generates random number between 1-10
srand(time(NULL));
int comp_num = (rand() % 10 +1);

// Gets the users number
int player_num = 0;

do
{
printf("Enter in a number (1-10): ");
scanf("%d", &player_num);

int result = CheckNumbers(player_num, comp_num);

// determines if the numbers are the same
if(result == 1)
{
printf("The number you entered is the same as the computers.\n");
getchar();
}
else
{
printf("You and the computers numbers are different.\n");
printf("computer number: %d & your number is: %d\n\n", comp_num, player_num);
printf("Try again!\n\n");
getchar();
}
}while(result != 1);


return 0;
}
///////////////////////////
//
// FUNCTION DEFINITIONS
//
///////////////////////////
int CheckNumbers(int num, int comp_num)
{
if(num == comp_num)
return 1;
else
return 0;
}[/source]
Advertisement
nevermind, I fixed it! int result was outside the scope
You'll have to move the declaration of "result" to outside the loop.

EDIT: Ah, I see you found the answer while I posted. My connection seems to be slow for some reason.
I tend to use a "manual break" pattern when I don't want to lift the variable out of the loop. Some people might argue that this is a code smell, but I prioritize "narrowest variable scoping" and "use the least number of variables that work" in this case.


for(;;) // this syntax loops indefinitely, so the only way out is a break, return, or exception.
{
// stuff
int result = whatever;

if (result == x)
break;
}

I tend to use a "manual break" pattern when I don't want to lift the variable out of the loop. Some people might argue that this is a code smell, but I prioritize "narrowest variable scoping" and "use the least number of variables that work" in this case.


for(;;) // this syntax loops indefinitely, so the only way out is a break, return, or exception.
{
// stuff
int result = whatever;

if (result == x)
break;
}



I sometime do the same thing, but without "for". I consider this a bit safer approach as it doesn't hit you so bad when you forget the break:


{
// stuff
int result = whatever;
if (result == x)
// other stuff
}

I tend to use a "manual break" pattern when I don't want to lift the variable out of the loop. Some people might argue that this is a code smell, but I prioritize "narrowest variable scoping" and "use the least number of variables that work" in this case.


for(;;) // this syntax loops indefinitely, so the only way out is a break, return, or exception.
{
// stuff
int result = whatever;

if (result == x)
break;
}




for(int result; result != x; ) // this syntax loops indefinitely, so the only way out is a break, return, or exception.
{
// stuff
result = whatever;
}


?
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.


for(int result; result != x; ) // this syntax loops indefinitely, so the only way out is a break, return, or exception.
{
// stuff
result = whatever;
}


?

That syntax uses `result' uninitialized. I am perfectly happy with the infinite-loop-with-break solution.
Result being uninitialized and as such potentially x, not even entering the loop is exactly the kind of bug I love so much. "We have a few customers reporting a really weird bug, but we are completely unable to reproduce it and have now wasted several weeks running test scenarios on a bunch of machines and staring at many thousands lines of related and semi-related code to figure it out. Turns out somebody ignored #1 in the coding guidelines: ALWAYS immediately initialize your variables and never justify laziness with 'better performance'."

So if x is unknown, you would have to initialized with something like "result = x+1" to be safe, resulting in awkward code that is more confusing than it has to be..
f@dzhttp://festini.device-zero.de

I sometime do the same thing, but without "for". I consider this a bit safer approach as it doesn't hit you so bad when you forget the break:


{
// stuff
int result = whatever;
if (result == x)
// other stuff
}


Am I missing something? Because the way I read that, there's no looping, and if there's no looping, it's purpose is entirely different than the looping version, in which case you don't do the same thing...
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

[quote name='fae' timestamp='1354526009' post='5006548']
I sometime do the same thing, but without "for". I consider this a bit safer approach as it doesn't hit you so bad when you forget the break:


{
// stuff
int result = whatever;
if (result == x)
// other stuff
}


Am I missing something? Because the way I read that, there's no looping, and if there's no looping, it's purpose is entirely different than the looping version, in which case you don't do the same thing...
[/quote]

No you're absolutely correct. I was just focusing on the scoping part of Nypyren's post. However now that you mentioned I probably misinterpreted Nypyren's reason for the block as the topic of the discussion is loops after all.. :)

This topic is closed to new replies.

Advertisement