Random Number Guessing Game

I have to create a program that is essentially a guessing game which generates a number from 1-100. The user has 5 guesses to guess the correct number. If the user guesses the correct number then they win and a congratulations message is shown that mentions what the number is and how many guesses it took. If the user fails to guess the number within 5 guesses, then the program should output a message that says they lose and could not guess x number. With each incorrect guess, the program should output if the guess was too high or too low. We are only allowed to use <stdio.h> and <stdlib.h>. My issue is that the program only stops when I guess the number correctly. After 5 failed attempts it will continue unless I input the random number that it gives me after the 5th attempt. I have been working on this for a couple of days as this is my first programming class. The other programs we have had to make seemed simple up to now. Please help me...


#include <stdio.h>
#include <stdlib.h>

int main()

{

srand(time(NULL));
int r_number=rand()%100+1;
int guess, num=5, tries;

printf("Welcome to the Guessing Number Game\n");
printf("Can you guess my secret number from 1-100?\n");

scanf("%d", &guess);

while(guess!=r_number){
if(guess<r_number){
printf("Sorry your guess is too low. You have %d more guesses.\n", --num);
printf("Can you guess my secret number from 1-100?\n");
scanf("%d", &guess);
}

else if(guess>r_number){
printf("Sorry your guess is too high. You have %d more guesses.\n", --num);
printf("Can you guess my secret number from 1-100?\n");
scanf("%d", &guess);
}


if(guess == r_number){
tries=5-num;
printf("congratulations you win!\n");
printf("you have guessed my secret number %d, in %d guesses\n", r_number, tries);
}

else if((guess!=r_number) && (num<=1)){
printf("Sorry you lose! I win!\n");
printf("You could not guess my secret number %d.\n", r_number);
}
}
system("pause");
return 0;
}




Use break; to end the loop
wwwwwwwwwwwwwwwwwooooooooooooooooooooooooowwwwwwwwwwwwwwwwwwwwwwwww!!!!!!!!!!!!!!!!!!!!!
I completely forgot about "break;" shows how new I am to this. Thank you LendraDwi!!!! You are my hero.
Topic archived. No new replies allowed.