473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to repeat the game?

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. }
Oct 10 '19 #1
2 1624
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.  
Oct 10 '19 #2
dev7060
636 Expert 512MB
Use Goto statement to transfer the control of program.
Oct 12 '19 #3

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

Similar topics

6
by: Mike | last post by:
Hello, This game will not loop. It compiles, builds, and executes fine, but the game will not loop around no matter what I do. What is wrong with the code? What do I have to add to make it loop...
0
by: dickie999 | last post by:
i can get visual basics to send a key, but how can i get it to send keys into a game window and repeat the process thanks in advance Richard
14
by: harbintyler | last post by:
I was wondering if anyone knows how to write a tic tac toe game in c++?
2
by: rockchicx23 | last post by:
im a beginner and i have to write a program that makes a guessing game. For example this should be my output. How many games would you like to play JW? (1 – 10): 22 That’s too many! How many...
2
by: pythonnoob83 | last post by:
I got my game to work but I when I run it and answer the first question it says Could not save. Why is my game not saving. It does create the save game file but it is always empty. Please Help!!!...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.