Jump to content

A simple number guessing game...If Else statements...help please?

- - - - -

  • Please log in to reply
31 replies to this topic

#1
RuneNova91

RuneNova91

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
I'm trying to make a simple number guessing game in Visual C++ using If Else statements.
Basically you guess a number, if you guessed the number correctly then a message will pop up saying you guessed right, and if not wrong...however im trying to make it so that if you guess to high or too low, it tells you to guess the opposite (lower or higher and vice versa).

Here's the code I have so far, there's errors on it obviously because it's not done haha:

#include "stdafx.h"

#include <iostream>

#include <Windows.h>


using namespace std;


int main()

{

	int theNumber = 76;

	int playersGuess;


	cout << "Hello and welcome! \n";

	Sleep(3000);

	cout << "Lets play guess my number! \n";

	Sleep(3000);

	cout << "Please enter a number between 0 and 100: ";


	cin >> playersGuess; //cin >> is the extraction operator, it waits for the user to type something in

	bool alive = true;


	if ( playersGuess == theNumber )

{

			cout << "They match... \n";

			Sleep(2000);

			cout << "Good job! \n";

}

		else

	if ( playersGuess > theNumber )

{

			cout << "They don't match... \n";

			Sleep(2000);

			cout << "I'm sorry, your guess is too high. \n";

}

		else

	if ( playersGuess < theNumber )

{

			cout << "They don't match... \n";

			Sleep(3000);

			cout << "I'm sorry, your guess is too low. \n";

}

		else


	char f;

	cin >> f;

	return 0;

}

So I guess my question is am I on the right track? what do I need to add or watch for? I'd like some help please

Edited by TkTech, 28 July 2010 - 08:55 PM.


#2
Groogy

Groogy

    Programmer

  • Members
  • PipPipPipPip
  • 183 posts
Kinda hard to read the code as you don't have it in the [ CODE ] tags and thus not properly indented. But you seem on the right track except that you only get one chance to guess the number then the application exists. You should implement so that they can guess several times before it quits. Also making the number random would be nice.
My Code Blog - My Github - Ascension Project - Madness Script Project - Simple-Garbage-Collector Project
There is bound to be something useful somewhere.

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
For rand add an include of stdlib.h and be sure to seed it:
srand( (unsigned)time( NULL ) );
cout << "Between 1 and 10: " << (rand() % 10 + 1);

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
RuneNova91

RuneNova91

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
yes the random number had occured to me haha and also I was trying to figure out how to keep it opened until you wanted to quit...I will figure that one out. but the above code for the random number aspect...can you explain it to me so I understand it? I can understand/decipher what most of it means but not the whole thing.

and yea im sorry about the tags...its more confusing here than on my compiler for some reason haha

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
The srand() function seeds the pseudorandom number generator, as they are quite sloppy at providing good results. You're feeding the time to the srand() function, as time always moves forwards, and never repeats it's a good random seed to provide. Time() returns an unsigned number so it's casted as so, accepting no arguments (time(NULL)).

As rand() returns a number between 0 and RAND_MAX (usually 32767 on most systems (in hex: 0x7FFF)) so you need to apply the modulus operator (%) to basically wrap the number.

120 % 100 = 20 and 72 % 10 = 2, so essentially rand() % 10 + 1 will force rand() to return a number between 1 and 10.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
RuneNova91

RuneNova91

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
Ok. that was clear...unfortunately Im pretty new to programming so its kinda hard to understand all the concepts :( sorry guys I hope its not frustrating but my code should look like what after its finished? if you guys dont mind...

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Maybe add tries first before we polish the code?

Here's a really simple example using a while loop, note it checks if tries = 5, and also breaks out of the loop if you win.
int tries = 0;
bool flag = false;

while(!flag) {
   //Example:
   if(playersGuess == theNumber) {
       cout << "You won!";
       flag = true;
   }  else if (playerGuess < theNumber) {
       cout << "Too low!";
       tries++;
   } else if(playerGuess > theNumber) {
       tries++; 
       cout << "Too high!";
   }
   if(tries == 5) {
       cout << "You lose! Answer is: " << theNumber;
       flag = true;
   }
}
EDIT: EDIT: Man I need coffee.. Fixed up a bit
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
RuneNova91

RuneNova91

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
Ok let me type this out and break it down and let you know what I understand and dont about the code you just posted and yes I would love to try and find out myself so I learn rather than the answers be just given to me so thank you for the patience and wonderful help :)

int tries = 0;

If I understand correctly we are assigning "tries" as the integer and the number 0 simply meaning that is the value of the integer "tries"

while(tries < 5) {

If I understand correctly "while" is an ongoing statement while the rest of the code is being carried out and "tries" or 0 is less than 5 which is a true statement. "{" is the start of a braces function where the code up until the closing brace is contained and pertains to the statement before the braces were opened.

if(playersGuess == theNumber) {

is an if else statement saying that if the integer "playersGuess" equals the integer "theNumber" then the following line "cout << "You won!"; will appear stating that you entered the correct number

break;

the break means that if you guessed or entered the correct number it will end the loop, being part of the if else statement.

} else {

closing brace closes the opening brace on line 6, else states if the above if statement isn't carried out it will do the following loop on the lines following it. then an opening brace

tries++;

simply means to add another try to the loop seeing as ++ means +1 I believe? then the next line after is a closing brace to line 9

if(tries == 5) {

is the start of an if statement meaning if the amount of times you enter a number is 5 then the following lines happen. then an opening brace to the following lines:

cout << "You lose! Answer is: " << theNumber;

the following message is printed and the integer "theNumber" is printed in the message to give the answer. the line after is a break stating that if the program gets to that point, it will print the losing message with the answer inside and will then break out of that loop and end the program. then theres a closing brace to line 12 and then another closing brace to line 3.

how did I do? haha

sorry its so long

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Sounds right , although look at my edit up there; I simply use flags (much cleaner message IMO) and I integrated a better example.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#10
RuneNova91

RuneNova91

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
unfortunately I havent gotten into flags YET in the tutorials I am learning from. Bool from what I remember is either true or false right? so what is a flag then?

I posted my last message with the explanations and then saw your edit and was like ah shoot lol

#11
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Sorry about that. I was using a flag just as a method to break out of the while loop. It's simple to use:
while(!flag) {
! means not true (such as !=), so while it is false, it will continue the loop. If it becomes true, then it won't be false, and will break out naturally. Much more clean code like that.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#12
RuneNova91

RuneNova91

    Learning Programmer

  • Members
  • PipPipPip
  • 74 posts
oh! ok yea that makes perfect sense...cant flag be used to remember a piece of info and can be referred to later? I suppose kind of like when you name an integer and refer to it later in the coding?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users