Code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <conio.h>
int main(void)
{
int num;
int randnum;
int tries;
char choice = 'y';
randnum = 1 + rand() % 1000;
tries = 1;
printf("I have a number between 1 and 1000.\n");
printf("Can you guess it?\n");
printf("Please type your guess: ");
scanf("%d", &num);
printf("\n\n");
do {
while(num != randnum)
{
if(num < randnum)
{
printf("Too low. Try again.\n\n");
printf("Next guess:\t ");
scanf("%d", &num);
printf("\n");
}
if(num > randnum)
{
printf("Too high. Try again.\n");
printf("Next guess: \t");
scanf("%d", &num);
printf("\n"); }
tries ++;}
if(num == randnum)
{
printf("Excellent! You guessed the number with %d tries!\n\n", tries );
printf("Would you like to play again (y or n)? ");
scanf("%c", &choice);}}
while(choice == 'y');
getchar();
return 0;
}
ANOTHER OPTION I AM TRYING IS TO USE goto
Code:
if (choice == 'y') {
goto loop; }
where loop is at the begging of the code