Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

C++ Guessing game


  • Please log in to reply
7 replies to this topic

#1 Guest_danbrownlow_*

Guest_danbrownlow_*

  • Guests
  • OFFLINE
  •  

Posted 09 October 2008 - 11:41 AM

[codebox]//============================================================================


#include <iostream>
using namespace std;

bool numberGuessed = false;

int guessRun(int user, int computer, bool numberGuessed){

if (user > computer){
cout << "Number too large!\n";
}
else if (user < computer){
cout << "Number too small!\n";
}
else if (user == computer){
cout << "You Have Entered The Correct Value\n";
numberGuessed = true;


}
return 0;
}

int main() {
int chosenNumber = 22;
int userNumber;
int userIncrement;

while (numberGuessed == false ){
cout << "Guess The Number: \n";
cout << "Please Enter A Number That You Think Is Valid \n";
cin >> userNumber;
cout << numberGuessed+"\n";
guessRun(userNumber, chosenNumber, numberGuessed);
userIncrement++;
}
return 0;
}

[/codebox]

Can anyone help me him work out why the boolean numberGuessed isn't changed when the user guesses the right number? I at least think this is the program, because if the bool is set to true at the beginning, then the program never executes. However, when the user has guessed the right number, the bool isn't assigned true and the program always contines.

Any help would be appreciated.

Thanks!

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington

Posted 09 October 2008 - 06:30 PM

Hello, danbrownlow :thumbsup:

You are passing the boolean value by value, while you need to pass it by reference. Pass a pointer to the function so that it may modify the item you want, rather than the value of the item you want.
Syntax is here:
http://publib.boulder.ibm.com/infocenter/c...ref/cplr233.htm

Hope that helps,

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:06:29 AM

Posted 09 October 2008 - 09:52 PM

What is the difference in passing a reference and passing a pointer and passing by reference?

#4 jpshortstuff

jpshortstuff

    WhatTheTech Teacher


  • Members
  • 660 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:08:29 AM

Posted 10 October 2008 - 03:19 AM

What is the difference in passing a reference and passing a pointer and passing by reference?

I'm not sure I quite understand what you mean but I will explain the difference between passing by value and passing by reference.

When you pass variables to functions by value, they are copied onto the stack, so that the function may access them. This means that in the above code:

guessRun(userNumber, chosenNumber, numberGuessed);

A copy of numberGuessed is pushed onto the stack. The function may modify this copy, but the original stays the same. Hence, numberGuessed is always false.

When you pass by reference:

guessRun(userNumber, chosenNumber, &numberGuessed);

You are passing the address of the original. This means that you can access the original by using that address:

*numberGuessed = true;

And this will actually modify the original, causing the effect that you want.

Hope this helps, any more questions just ask.
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Posted Image

#5 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:02:29 PM

Posted 10 October 2008 - 09:51 AM

Thanks for your explanation ipshortdtuff. :thumbsup:

#6 jpshortstuff

jpshortstuff

    WhatTheTech Teacher


  • Members
  • 660 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:08:29 AM

Posted 10 October 2008 - 10:10 AM

No probs :thumbsup:
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Posted Image

#7 strangerdanger

strangerdanger

  • Members
  • 2 posts
  • OFFLINE
  •  
  • Local time:02:29 PM

Posted 12 October 2008 - 08:26 PM

It's been a while since I've used c++, but I noticed your function returns 0. If memory serves, 0 = false, meaning your function will always return false causing your program to continue even though the correct number was guessed. Have your function return the variable numberguessed instead.

On a side note, you are incrementing the variable userIncrement, but have not given it an initial value, nor are you using this variable for anything. Unless you are counting the number of guesses the user makes, you may want to drop this variable.

Edited by strangerdanger, 12 October 2008 - 10:20 PM.


#8 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:08:29 AM

Posted 13 October 2008 - 03:49 PM

StrangerDanger, he's not using the return value of the funcion, he's trying to pass values back and forth via arguments.

But the uninitialized variables do present a problem ;)

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users