By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

How to repeat the game?

P: 1
how do i go back to "Please enter a number from 0 to 100" after i guessed correctly?

this should be my output:
Please enter a number from 0 to 100:
50
You guessed correctly in 1 try!Congratulations!
Play again?
Y //Yes
Please enter a number from 0 to 100:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main()
  6. {
  7.     int guessed_number, random_number, counter=0;
  8.     char symbol='*',choice,choice2;
  9.  
  10.     srand(time(NULL));
  11.     random_number= rand()%100 + 1;
  12.  
  13.     printf("*************************************************");
  14.     printf("\n%-14cWELCOME TO THE GAME!%15c",symbol,symbol);
  15.     printf("\n%-24c%25c",symbol,symbol);
  16.     printf("\n%-24c%25c",symbol,symbol);
  17.     printf("\n%-22cSTART%22c",symbol,symbol);
  18.     printf("\n%-22cQUIT%23c",symbol,symbol);
  19.     printf("\n*************************************************\n");
  20.     scanf("%c",&choice);
  21.  
  22.     if(choice=='s')
  23.     {
  24.         printf("Please enter a number from 0 to 100:");
  25.         while(1)
  26.         {
  27.             counter ++;
  28.             scanf("%d",&guessed_number);
  29.  
  30.             if(guessed_number<random_number)
  31.             printf("Your guess is too low.Guess again.\n");
  32.  
  33.             if(guessed_number>random_number)
  34.             printf("Your guess is too high. Guess again.\n");
  35.  
  36.             if(guessed_number==random_number)
  37.             {
  38.                 printf("You guessed correctly in %dtries! Congratulations!",counter);
  39.                 break;
  40.             }    
  41.         }
  42.     }
  43.  
  44.     else 
  45.     {
  46.         printf("Bye! See you next time!");
  47.     }
  48. }
1 Week Ago #1
Share this Question
Share on Google+
2 Replies


P: 1
SORRY FOR WRITING IN JS, BUT THE IDEA IS THE SAME

You could try setting a variable that says whether the game should repeat.
Expand|Select|Wrap|Line Numbers
  1. let gameReapeting = true  
  2.  
and then run the game in a while loop

Expand|Select|Wrap|Line Numbers
  1. while (gameReapeting) {
  2. //Game here
  3. }
And then if the player chooses not to loop, the var is set to false

Expand|Select|Wrap|Line Numbers
  1. if (input !== Y || input !== y) {
  2.   gameReapeting = false
  3. }



The final code:

Expand|Select|Wrap|Line Numbers
  1. let gameReapeting = true  
  2.  
  3. while (gameReapeting) {
  4. //Game here
  5.  
  6. if (input !== Y || input !== y) {
  7.   gameReapeting = false
  8. }
  9. }
  10.  
1 Week Ago #2

dev7060
100+
P: 117
Use Goto statement to transfer the control of program.
4 Days Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.