Jump to content

How do I end a do/while loop early in C?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
zacharyrs

zacharyrs

    Newbie

  • Members
  • Pip
  • 1 posts
I was able to set a variable and increase it each time to show the number of tries. Yay for me. Anyone know best way of stopping the loop when the user has guessed the number, thus ending the loop before it has reached 10? Any help would be greatly appreciated.


#include<stdlib.h>

#include<stdio.h>

#include<time.h>


int main(void)

{

	int x = 10;

	int i = 0;

	int target, guess;

	int numGuess = 0;


	/*create a random number*/

	//create random function

	srand(time(NULL));//this creates new number based on time which changes every second :)

	target = rand() % 99; //create a random number using the rand() function, from 0 -99

	

	

	

	do{

		//increase the loop until it meets the x variable

		i++;

		numGuess++;

		//allow user to input a number for guess

		scanf("%d", &guess);

		if (guess == target)

		{

			printf("You win! \n\n");

			

		}

		else if (guess > target)

		{

			printf("You are too high. Guess a number:\n\n");

		}

	    else if (guess < target)

		{

			printf("You are too low. Guess a number:\n\n");

		}

		

	}while(i < x);

		printf("You lose, the number was %d. \n", target);


	printf("Number of tries %d\n", numGuess);

	printf("Enter any key to exit...");

	getchar();

	getchar();

	

	return 0;


}


#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
break;


#3
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
In your example, you would put the break keyword right after your printf() so it would look like so...
printf("You win! \n\n");
break;

When a break command is encountered, it'll immediately leave the loop and end up at
printf("You lose, the number was %d. \n", target);


#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
If you're unfortunate enough to have to have a switch statement inside a loop, the break statement won't work, though. Moral: don't use switch statements inside loops.
sudo rm -rf /

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Alternatively, just add that condition to the exit condition.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
I'm not entirely sure what you mean by that. Do you mean call exit()?
sudo rm -rf /

#7
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
He's referring to

}while(i < x);

which could have been
}while(i < x || guess == target);


#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Oooh. I thought WP was referring to my (somewhat) random comment.
sudo rm -rf /

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
nah, just got in with my 2 cents kinda late.

tk's right. I find that people tend to be focussed on half of their real exit condition for a loop, and when the detect the other condition don't consider applying it directly where it can be tested.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog