473,654 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help Please in making Code replay the game.

1 New Member
Hi guys I'm new to C++ Programming and I am having trouble in making my Guessing game code replay. I am suppose to give the user that is playing my game the opportunity to play again with options of 'Yes' and 'No' and if Yes then the game starts up again and if No then the game tells the user bye and quits.

"Do you want to play again? (y/n)"

Please Help if possible thanks. ^^


-----------------------------------------

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() 
  6. {
  7.     cout << "\t\tWelcome to the Number Guessing Game!\n\n";
  8.  
  9.     cout << "Think of a number between 1 and 1000 and I'll try to guess it.\n"; 
  10.  
  11.     cout << "After guess, respond with 1 for higher, 2 for lower, or 3 for correct";
  12.  
  13.  
  14.     int tries = 0;
  15.     int low = 0;
  16.     int high = 1001;
  17.     char response = 1;
  18.  
  19.  
  20.     do
  21.     {
  22.          int guess = (low + high) / 2;
  23.          cout << "\nMy guess is " << guess << endl;
  24.          tries += 1;
  25.  
  26.  
  27.     cout << "Is your number (1)higher, (2) lower or am I correct?(3): ";
  28.     cin >> response;
  29.  
  30.         switch (response)
  31.         {
  32.         case '1':
  33.             low = guess;
  34.             break;
  35.         case '2':
  36.              high = guess;
  37.              break;
  38.         case '3':
  39.              cout << "Yeah! I guessed your number in only " << tries << " tries.";
  40.              break;
  41.         default:
  42.             cout << "Uh, error. You made an illegal choice. " << response << endl;
  43.         } 
  44.     } while (response != '3');
  45.  
  46.  
  47.     system ("pause");
  48.  
  49.     return 0;
  50. }
Feb 24 '08 #1
1 3088
weaknessforcats
9,208 Recognized Expert Moderator Expert
You need a loop in main() that tests for the choice to continue or not.

If the choice is to continue, then call the code you have already written. Your main() is really the game. So change the name to Game().

If the choice is to quit, then break out of the loop.
Feb 24 '08 #2

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

Similar topics

6
2087
by: Kartik | last post by:
Hello, I was trying to develop a game in c++ ( text mode ), but I couldn't figure out how to make rest of the elements moving in the game while waiting for the player's input, or, how to accept the player's input in the game. I had visited many sites having c++ tutorials but no one gave enough explanation about this. an anyone please help me?
1
1663
by: rainbowii7 | last post by:
Calling all programmers for helllllllllllllllppppp!!! i am currently doing a uni degree and our lecturers have set us the task of making a game in JavaScript. i chose to do a hangman game and have completed the actual game. I have recently been in hospital as I am disabled and so have not been in the lessons where my lecturers have been teaching us how to create scoring and incorporating different difficulty levels into the game. it...
7
2842
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I found a bug that I can't figure out (believe me; I've tried everything and asked for help several times on the IRC-channel). The updateMsg() and iMsg() takes a list of message-lines as argument (or process a string to a list), and it'll output...
1
4408
hpbutterbeer
by: hpbutterbeer | last post by:
We have a Machine Project and my brain is currently in a clouded state. Sorry, I'm just a beginner in C Programming... Text twist is a windows game whose main objective is to form words out of the letters shown on the screen, you move on to the next level when you are able to form a word with all the letters given. In the actual game you click on the letters of the word, in this text version you simply type the words. To be able to...
2
1553
by: shirin.no87 | last post by:
how to make solitaire game of win xp with c++? the game should be in this way: 1:deal:to begin the game 28 cards are dealt into seven piles the leftmost pile has one card the next tow cards and so forth up tp seven cards in the rightmost pile only the upermost card of each of the seven piles is turnd face up.
17
1652
keyvanrahmadi
by: keyvanrahmadi | last post by:
Hello there guys, i think i am at the end of this, which i have to edmit has been a huge learning curve for me, but i still have couple of problems, which i was hoping you can help me. I think tbh its a very simple thing which i am over looking and just require a pair of fresh eyes to look at. I greatly appriciate if you could have a look at the script and let me know what you think. if you have any solution, or advise just email me or pm...
45
3918
nemisis
by: nemisis | last post by:
Hi Everyone I have am doing an object oriented C++ program and I have no idea as to how start it................. I am not that good at coding so i tried and made an overview of what the main driver should look like main{
2
5307
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I managed to write a code for a single player (human -vs- computer) game. My problem is, now we want to merge the two games with options to pick which game you would like to play. However, the games were written totally different styles and I can't...
0
8814
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8706
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7304
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.