BleepingComputer.com: C++ Guessing game

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

C++ Guessing game My friend can't work out why he's program won't stop!

#1 User is offline   danbrownlow 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 99
  • Joined: 05-March 07
  • Gender:Male

Posted 09 October 2008 - 11:41 AM

//============================================================================


#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;
}



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!

#2 User is offline   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,402
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

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

#3 User is offline   Romeo29 

  • Learning To Bleep
  • PipPipPipPipPipPip
  • Find Topics
  • Group: BC Advisor
  • Posts: 2,834
  • Joined: 06-July 08
  • Gender:Not Telling
  • Location:127.0.0.1

Posted 09 October 2008 - 09:52 PM

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

#4 User is offline   jpshortstuff 

  • WhatTheTech Teacher
  • PipPipPipPipPip
  • Find Topics
  • Group: Malware Response Team
  • Posts: 660
  • Joined: 15-June 07
  • Gender:Male
  • Location:UK

Posted 10 October 2008 - 03:19 AM

View PostRomeo29, on Oct 10 2008, 03:52 AM, said:

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:

Quote

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:

Quote

guessRun(userNumber, chosenNumber, &numberGuessed);

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

Quote

*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 User is offline   Romeo29 

  • Learning To Bleep
  • PipPipPipPipPipPip
  • Find Topics
  • Group: BC Advisor
  • Posts: 2,834
  • Joined: 06-July 08
  • Gender:Not Telling
  • Location:127.0.0.1

Posted 10 October 2008 - 09:51 AM


#6 User is offline   jpshortstuff 

  • WhatTheTech Teacher
  • PipPipPipPipPip
  • Find Topics
  • Group: Malware Response Team
  • Posts: 660
  • Joined: 15-June 07
  • Gender:Male
  • Location:UK

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 User is offline   strangerdanger 

  • New Member
  • Pip
  • Find Topics
  • Group: Members
  • Posts: 2
  • Joined: 12-October 08

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.

This post has been edited by strangerdanger: 12 October 2008 - 10:20 PM


#8 User is offline   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,402
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users