Jump to content

  • Log In with Google      Sign In   
  • Create Account


C Do/While Loop problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 NUCLEAR RABBIT   Members   -  Reputation: 247

Like
0Likes
Like

Posted 30 November 2012 - 02:30 PM

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?


#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;

}

Attached Thumbnails

  • Screen Shot 2012-11-30 at 12.28.48 PM.png

------------------------My band: RISE OVER ME!

Sponsor:

#2 NUCLEAR RABBIT   Members   -  Reputation: 247

Like
0Likes
Like

Posted 30 November 2012 - 02:37 PM

nevermind, I fixed it! int result was outside the scope
------------------------My band: RISE OVER ME!

#3 shuma-gorath   Members   -  Reputation: 887

Like
0Likes
Like

Posted 30 November 2012 - 02:39 PM

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.

Edited by shuma-gorath, 30 November 2012 - 02:43 PM.


#4 Nypyren   Crossbones+   -  Reputation: 3737

Like
0Likes
Like

Posted 30 November 2012 - 02:41 PM

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;
}

Edited by Nypyren, 30 November 2012 - 02:42 PM.


#5 fae   Members   -  Reputation: 214

Like
0Likes
Like

Posted 03 December 2012 - 03:13 AM

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
}


#6 Khatharr   Crossbones+   -  Reputation: 2870

Like
0Likes
Like

Posted 03 December 2012 - 06:30 PM

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.

#7 Álvaro   Crossbones+   -  Reputation: 11966

Like
0Likes
Like

Posted 03 December 2012 - 09:47 PM

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.

#8 Trienco   Crossbones+   -  Reputation: 2056

Like
2Likes
Like

Posted 03 December 2012 - 11:37 PM

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

#9 Cornstalks   Crossbones+   -  Reputation: 6966

Like
2Likes
Like

Posted 03 December 2012 - 11:43 PM

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...
[ 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 ]

#10 fae   Members   -  Reputation: 214

Like
0Likes
Like

Posted 04 December 2012 - 01:11 AM


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...


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.. :)

#11 Khatharr   Crossbones+   -  Reputation: 2870

Like
0Likes
Like

Posted 04 December 2012 - 01:46 AM

Yeah, wasn't paying attention to the body when I did that. (derp) The var should have an initial value, but I just figure if you're gonna use a for loop that breaks when a var is at a specific value then why not use the for-loop syntax?

Here's an upgraded version:
[source lang="cpp"]void operator,() { for(int result = initial_val; result != x; ) { //do stuff result = whatever; }}[/source]

Posted Image

Anyway, I'll use 'while(true)' sometimes, although MSVC has an annoying warning about it. Suppose it's a matter of preference.

Edited by Khatharr, 04 December 2012 - 01:54 AM.

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.

#12 Bacterius   Crossbones+   -  Reputation: 8187

Like
0Likes
Like

Posted 04 December 2012 - 04:28 AM

Anyway, I'll use 'while(true)' sometimes, although MSVC has an annoying warning about it. Suppose it's a matter of preference.

What does the warning say? If it's because of "true", you can just use 1 (or any value different from zero, really). Or does it literally babysit you saying "this is an infinite loop"?

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#13 rip-off   Moderators   -  Reputation: 7701

Like
2Likes
Like

Posted 04 December 2012 - 04:29 AM

You lost me when you overloaded the comma operator.

#14 mhagain   Crossbones+   -  Reputation: 7466

Like
0Likes
Like

Posted 04 December 2012 - 05:46 AM


Anyway, I'll use 'while(true)' sometimes, although MSVC has an annoying warning about it. Suppose it's a matter of preference.

What does the warning say? If it's because of "true", you can just use 1 (or any value different from zero, really). Or does it literally babysit you saying "this is an infinite loop"?


"while (1)" throws the warning as well - conditional expression is constant. Personally I prefer to have the warning and just use "for (;;)" instead - the warning is far more useful than being able to do "while (1)".

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#15 Khatharr   Crossbones+   -  Reputation: 2870

Like
0Likes
Like

Posted 04 December 2012 - 03:37 PM

I agree that the warning could potentially be useful, but I've probably triggered it a million times and it was always an intentional unconditional loop.

Pity there's no explicit unconditional loop.

"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?

Do we have an ASCII expert in here?
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.

#16 Cornstalks   Crossbones+   -  Reputation: 6966

Like
0Likes
Like

Posted 04 December 2012 - 04:43 PM

Do we have an ASCII expert in here?

What do you need an ASCII expert for (not claiming to be one)?
[ 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 ]

#17 Álvaro   Crossbones+   -  Reputation: 11966

Like
0Likes
Like

Posted 04 December 2012 - 05:07 PM

Is there such a thing as an ASCII expert? There isn't all that much to know about it...

#18 Bacterius   Crossbones+   -  Reputation: 8187

Like
0Likes
Like

Posted 04 December 2012 - 05:51 PM

"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?

Well, I agree that for(;;) looks horrible and is much less readable at a glance than while(true), but if you can just remember that it means "loop indefinitely" then I guess it's fine.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#19 Paradigm Shifter   Crossbones+   -  Reputation: 5152

Like
1Likes
Like

Posted 04 December 2012 - 05:58 PM

Just #define ever (;;)

then you can do

for ever

;)
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#20 mhagain   Crossbones+   -  Reputation: 7466

Like
0Likes
Like

Posted 04 December 2012 - 07:12 PM

I agree that the warning could potentially be useful, but I've probably triggered it a million times and it was always an intentional unconditional loop.


Have a read: http://stackoverflow.com/questions/3490823/why-msvc-generates-warning-c4127-whan-constant-is-used-in-while-c - it outlines the general usefulness of the warning. My general philosophy on this is that if it saves your ass even just once then it's probably worth it.

The one case where I do agree that it's a pain is "do { ... } while (1)".

Pity there's no explicit unconditional loop.


According to the link above "for (;;)" is actually what is explicitly defined to be an infinite/unconditional loop, but I haven't cross-checked with the standard so take it with the appropriately sized grain of salt.

"for(;;)" looks like the arachnid version of a revolutionary's wall art. Or maybe it's a walrus?


Definitely a walrus.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS