473,413 Members | 1,679 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,413 software developers and data experts.

number guessing game

I have to do the following:

For this project, you are to create a simple number guessing game. The program should
begin by asking the user for a range (i.e. a minimum number and a maximum number) in
which the random number should fall. Next, the program should ask for a maximum
number of guesses to allow. The program should then allow the player to attempt to
guess the number giving the maximum allowable guesses. The program should give the
player hints along the way, telling them if their current guess is too big or too small.

• Write a function that asks the player for the maximum number of guesses.
• Write a function that asks the player for the range (the minimum and maximum
values).
• Write a function that takes the player's current guess and the actual number and
tells the player if they are too high or too low.
• Write a function that takes the minimum and maximum values from the user and
returns a random number in that range.

My main problem is, I'm not sure how to do the range function... since you can only return one value I don't know how to take two numbers and return them to use in the randomNum function. Please help
Feb 25 '07 #1
4 4121
willakawill
1,646 1GB
If I have to return more than one value from a function I can either send references to my variables as parameters or I can change the values of global variables. This is often just a matter of style.
Feb 25 '07 #2
Unfortunately I don't have a compiler at home so I can't test this, but this is what I got so far.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int randomNumber(int x, int y);
  5. int maxGuesses(int x);
  6. int main()
  7. {
  8.    int minNum, maxNum, maxGuesses;
  9.    char playAgain;
  10.    do
  11.    {
  12.       cout << "Enter a minimum number: ";
  13.       cin >> minNum;
  14.       cout << "\nEnter a maximum number: ";
  15.       cin >> maxNum;
  16.       maxGuesses();
  17.       randomNumber(minNum, maxNum);
  18.       compareNum(realNumber, guess);
  19.       cout << "Play again?"
  20.       cin >> playAgain;
  21.    while (playAgain != "N")
  22.  
  23.    cout << "Bye!";
  24.  
  25.    return 0;
  26. }
  27.  
  28. compareNum(int realNumber, int guess)
  29. {
  30.    if (realNumber > guess)
  31.       cout << "too high!"
  32.    else if
  33.       cout << "too low!"
  34.    else
  35.       cout << "You win!";
  36.    return 0;
  37. }
  38.  
  39. maxGuesses()
  40. {
  41.    int guesses = 0;
  42.    cout << "Enter maximum guesses: "
  43.    cin >> guesses;
  44.    return guesses;
  45. }
  46.  
  47. randomNumber(minNum, maxNum);
  48. {
  49.    int realNumber = rand() % maxNum + minNum;
  50.    return realNumber;
  51. }
Feb 25 '07 #3
willakawill
1,646 1GB
Thank you for sharing :)
Feb 25 '07 #4
DeMan
1,806 1GB
At a quick glance, I think your randomNumber function may not be doing quite what you want.

I'm not quite sure on operator precedence but either (i think the first)
if I put in minimum number as 10 and maximyum as 50, then your method could
1) give me number between 10 and 59
OR
2)give me an answer as high as 59 (I think), and as low as 0.

I think what you want to do is
Expand|Select|Wrap|Line Numbers
  1.  
  2. (rand() %(maxNum- minNum+1)) + minNum;
  3.  
This would make the span mof numbers the difference between min and max (inclusive-> hence the +1) and the lowest number min.
Feb 25 '07 #5

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

Similar topics

8
by: EAS | last post by:
Hey, I'm new to python (and programming in general) so I'll prolly be around here a lot... Anyways, I've found out how to make a "guess my number game" where the player guesses a number between...
12
by: xeys_00 | last post by:
#include<iostream> #include<cstdlib> //This program will be a number guessing game int main() { //variables for the game
27
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
13
by: artev | last post by:
If sort this work: var myarray= new Array(10,16,35,"0.1",8,4,22,19,1,22,35,9,26,38,40); with code function function1(a,b) {return a - b} var order02=new Array();...
5
by: Kraken | last post by:
Hi, i have a bit of a problem here. I have an assignment to do an animal guessing game using an original database and updating it as the user enters new animals in it. The program enters the file...
2
by: antonyfran | last post by:
hey can some please tell me how to run this class a specific number of times. maybe by using a for loop?. i am making a game called memory game where you match the cards which are the same. i want...
9
KoreyAusTex
by: KoreyAusTex | last post by:
I have written a program using method calls as opposed to nested while loops, or rather a while within a while, but for some reason the only way I can get the program to terminate is to type break in...
7
by: dseto200 | last post by:
I'm able to create a guessing number game, but I can limit the amount of guesses to less than 3. Can someone tell me what i'm doing wrong? # Guess My Number # The computer picks a random number ...
3
by: sbdalecia | last post by:
In C++ I have to create a guessing game that has three loops and and I have to make a flowchart. Now mind you I am a beginner at all of this. Someone help!
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
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...

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.