Public Group

# Guess the Number Game

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I made a guess the number game and I'd like you guys to see if there is anything you would change. As far as I know it works properly. If you know how to put the code into that little box with the highlightng and everything please tell me. Edit: Thanks for the responses, I put it into the code box.
// Guess the Number - Program generates random number
// and user tries to guess what it is

#include <iostream>
#include <stdlib.h>
#include <time.h>

bool gameloop();  // main game loop
void debug();  // prints values of variables for debugging

int guess, lives, num; // guess for what rand num is, lives left, random number
char input; // input for if user wants to play again
bool win;  // checks to see if user won

int main()
{
srand( (unsigned)time( NULL ) );

while (gameloop() == true);

return 0;
}

bool gameloop()
{
num = int((rand()/(double)(RAND_MAX+1)) * 6) + 1;
win = 0;
lives = 3;
bool playagain;

while (lives != 0)
{
debug();
std::cout << "Guess number: ";
std::cin >> guess;
std::cout << "\n";

// checks if the users guess equals the random number
// if not tells if its too high or too low and decrements lives
if (guess == num)
{
win = 1;
break;
}
else
{
std::cout << "Wrong number\n";
lives -= 1;

if (guess < num)
std::cout << "Too low.\n";
else
std::cout << "Too high.\n";
}

}
if (win == 1)
std::cout << "You win!\n";
else
std::cout << "You lose.\n";

std::cout << "Would you like to play again? (y/n)\n";
std::cin >> input;
if (input == 'y')
playagain = true;
else
playagain = false;
return playagain;
}

void debug()
{
std::cout << num << "\n" << win << "\n" << lives << "\n";
}



##### Share on other sites
[source] code here [/source] will make a code box. It's in the FAQ.

##### Share on other sites
Looks neat; as far as a I can tell [grin]
To put it in a source box, do this:
[ source lange="cpp" ]
***source in here***
[ /source ]

Without the spaces around after and before the [ ]

EDIT: Well; if you wrote a funky one there, how about one I wrote specially for those who don't want to compile the source? Guess the number game in a link!

If you want the source;
javascript:var r=Math.floor(Math.random()*10)+1;var g='Enter your guess here.';window.alert('Welcome to Ben\'s awesome guess the number game!');while(g!=r){g=prompt('Guess the number!\nIt\'s between 1 and 10',g);if(g==r){window.alert('Well done! You win a cake!');window.location='http://www.gerryscakes.com/cafe-mocha-cake.html'}else if(g<r){window.alert('Too low!');}else if(g>r){window.alert('Too high!');}}

##### Share on other sites
Its just a stylistic thing, but I would take the break out of the if statement and change the while to a do while, with conditions while (lives > 0 && win == 0). This makes the exit conditions clearer for anyone who is reading your code; they know the loop will be executed at least once, because its a do while, and they can see all the exit conditions in one place.

##### Share on other sites
Are you the same DaemonMagus on CGTalk.com?

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013707
×