13 Replies - 1905 Views - Last Post: 23 May 2011 - 09:00 AM Rate Topic: -----

#1 thedudeky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 30-January 11

Guessing game array not loading/displaying correct info.

Posted 22 May 2011 - 11:18 AM

I have a homework assignment in which we are supposed to create a random number guessing game. Each week we have built additional functionality. This week we are supposed to build an array that stores user guesses and then displays them when the user guesses incorrectly. I've created the array and it displays, but the array is not loading the correct info and gives me back garbage. Any help appreciated. Here's the code.

#include <iostream>
#include <ctime>
#include <cstdlib>

/* Raley Week 4 Assignement
This program generates a random number and then asks the user to attempt to guess it. 
It then gives hints if the guess is incorrect.  The program then asks if the user wishes to play again. 
If so, the program generates a new number to play with.  If not, the program terminates.
May 20, 2011 */

using namespace std;



int reviewGuess(int secretNumber, int guessedNumber)//Function to compare the guess with the random number.
{
	if (guessedNumber<1||guessedNumber>100)
		return -2;
   else if (secretNumber==guessedNumber)
		return 0;
   else if (secretNumber<guessedNumber)
		return 1;
	else if (secretNumber>guessedNumber)
		return -1;
}


int main ()
{

int numberoftries = 0;
const int maxnumberoftries = 5;
int userguesses[maxnumberoftries];
for (numberoftries = 0; numberoftries < maxnumberoftries; numberoftries++);

	bool playGame=true;//Boolean expression to allow the user to play again.
	while (playGame)
	{
    int secretNumber = 0, guessedNumber = 0;
    const int MAX = 100, MIN = 1;

    // Get system time
	unsigned seed = time(0);

	// Seed the number generator
    srand(seed);

	// Generate the random number
    secretNumber = (rand() % (MAX - MIN + 1)) + MIN;

    cout<<"Welcome to the Secret Number Game!"<<endl;
	cout<<"Please try to guess a number 1 through 100!"<<endl;

    
    // Conditional loop that only ends when number is guessed
    while (guessedNumber != secretNumber)
    {
        cout<<"Pick a number : ";
        cin >> guessedNumber;
		userguesses[numberoftries] = guessedNumber;
        int userInput;
		
			userInput=reviewGuess(secretNumber, guessedNumber);//Calls the function to evaluate the guess.

	switch (userInput)//Switch statement generates a response according to function evaluation.
	{
    case 0:
            cout << "You guessed the secret number!"<<endl<<endl;
			break;
	case 1:
            cout << "Guess lower please!"<<endl<<endl;
				cout << "Your guesses are:";
for (numberoftries = 0; numberoftries < maxnumberoftries; numberoftries++);
				cout << "" << userguesses;
				cout << endl;
			break;
    case -1:
            cout << "Guess higher please!"<<endl<<endl;
			cout << "Your guesses are:";
for (numberoftries = 0; numberoftries < maxnumberoftries; numberoftries++);
				cout << "" << userguesses;
				cout << endl;
			break;
	case -2: 
		    cout << "Please make a guess between 1 and 100!"<<endl<<endl;
			break;
	default:
			cout <<"Your guess is not within the paramaters"<<endl<<endl;
	}}
	char userChoice;//Allows user to play again
		cout <<"Would you like to play again?  Enter y or n."<<endl;
		cin >> userChoice;

		if (userChoice!='y')//Starts the game over
		{
			playGame=false;//Generates termination prompt and ends loop.
		   cout<<"Thanks for playing the Guessing Game!"<<endl<<endl;
		}
	}
return 0;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Guessing game array not loading/displaying correct info.

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Guessing game array not loading/displaying correct info.

Posted 22 May 2011 - 11:26 AM

Your game loop is inside of the for loop that iterates through the array. Therefore, you never change past the first element, and the other values in the array are undefined.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Guessing game array not loading/displaying correct info.

Posted 22 May 2011 - 11:39 AM

The indentation of your code is a mess, making it almost impossible to tell which code is in which block (loop, if statement, switch, etc), and that muddies-up your thinking and makes your code prone to errors.

You may think that neatness is just for appearance or that you'll take care of it "later", but if you make an effort to keep your code indented properly as you go along you'll find that it actually makes programming easier.
Was This Post Helpful? 1
  • +
  • -

#4 thedudeky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 30-January 11

Re: Guessing game array not loading/displaying correct info.

Posted 22 May 2011 - 01:44 PM

The code looks a mess because I've been trying to find the right place to put the initialization, so with the moving around, I haven't had time to make it look pretty. Where would you recommend the for loop being placed. That seems to be the biggest problem I'm having.
Was This Post Helpful? 0
  • +
  • -

#5 thedudeky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 30-January 11

Re: Guessing game array not loading/displaying correct info.

Posted 22 May 2011 - 03:11 PM

I've tried putting it in other places now, but it is still giving me a hexadecimal output.
Was This Post Helpful? 0
  • +
  • -

#6 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Guessing game array not loading/displaying correct info.

Posted 23 May 2011 - 06:07 AM

Problem fixed.
Tidied up a bit too.

Expect an A+! :bananaman:

+rep me if this helped. :whistling:

#include <iostream>
#include <ctime>
#include <cstdlib>

/* Raley Week 4 Assignement
This program generates a random number and then asks the user to attempt to guess it. 
It then gives hints if the guess is incorrect.  The program then asks if the user wishes to play again. 
If so, the program generates a new number to play with.  If not, the program terminates.
May 20, 2011 */

using namespace std;

int guessedNumber;
int numberoftries(0);
const int maxnumberoftries(5);
int userguesses[maxnumberoftries];

//Function to compare the guess with the random number.
int reviewGuess(int secretNumber, int guessedNumber)
{
	if (guessedNumber<1||guessedNumber>100)
        return -2;
    else if (secretNumber==guessedNumber)
		return 0;
    else if (secretNumber<guessedNumber)
		return 1;
	else if (secretNumber>guessedNumber)
		return -1;
}
//Prints the array values based on length of array
void printarray (int arg[], int length) {
    for (int n=0; n<length; n++)
        cout << arg[n] << ", ";
}

int main ()
{

    
    //Boolean expression to allow the user to play again.
	bool playGame=true;
	while (playGame)
	{
        int secretNumber, guessedNumber(0);
        const int MAX(100), MIN(1);

        // Get system time
    	unsigned seed = time(0);

	    // Seed the number generator
        srand(seed);

	    // Generate the random number
        secretNumber = (rand() % (MAX - MIN + 1)) + MIN;

        cout<<"Welcome to the Secret Number Game!"<<endl;
	    cout<<"Please try to guess a number 1 through 100!"<<endl;

    
        // Conditional loop that only ends when number is guessed
        while (guessedNumber != secretNumber)
        {
            
            cout<<"Pick a number : ";
            cin >> guessedNumber;
		    
            int userInput;
		    //Calls the function to evaluate the guess.
		    userInput=reviewGuess(secretNumber, guessedNumber);
		    //Breaks the loop if you go over 5 tries
            if(numberoftries < maxnumberoftries){numberoftries++;}
            else break; 
            //Write your quessed number to the userguesses array
            userguesses[numberoftries-1] = guessedNumber; 
            //Switch statement generates a response according to function evaluation.
	        switch (userInput)
    	    {
                case 0:
                    cout << "You guessed the secret number!"<<endl<<endl;
		    	    break;
        	    case 1:                
                    cout << "Guess lower please!"<<endl<<endl;
				    cout << "Your guesses are: ";
				    printarray(userguesses, numberoftries);
				    cout << endl; 
                    break;
                case -1:
                    cout << "Guess higher please!"<<endl<<endl;
			        cout << "Your guesses are: ";
				    printarray(userguesses, numberoftries);
				    cout << endl;
			        break;
	            case -2: 
		            cout << "Please make a guess between 1 and 100!"<<endl<<endl;
		    	    break;
      	        default:
		    	    cout <<"Your guess is not within the paramaters"<<endl<<endl;
           
	     }
         
          
    }
    //Allows user to play again
    char userChoice;
        cout <<"Would you like to play again?  Enter y or n."<<endl;
        cin >> userChoice;
        //Starts the game over
        if (userChoice!='y')
        {
           //Generates termination prompt and ends loop.                            
           playGame=false;
           cout<<"Thanks for playing the Guessing Game!"<<endl<<endl;
        }
    }
	
return 0;
}


Was This Post Helpful? -4
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Guessing game array not loading/displaying correct info.

Posted 23 May 2011 - 06:14 AM

DIC isn't a code-fixing service, and it's contrary to the forum rules to do people's homework for them.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4225
  • View blog
  • Posts: 13,253
  • Joined: 25-December 09

Re: Guessing game array not loading/displaying correct info.

Posted 23 May 2011 - 06:25 AM

Not only is it contrary to forum rules, your use of global variables is a very bad habit especially since you have globals that conflict with local variables.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Guessing game array not loading/displaying correct info.

Posted 23 May 2011 - 07:13 AM

:offtopic:
Sorry guys, but I have to disagree;

Quote

If you are answering someone's question, only do so if they have made an attempt.

Quote

We can't help you if you don't post your code. We will not do your homework for you. You MUST show some effort.


He posted a fully operational program, I fixed his code. I didn't post entirely independent code, and thus was operating within the rules. He did show substantial effort in providing the code himself.

I gave this man an hour of my time reviewing his work, and for that I got -repped and caught in the middle of a WBM-ragefest that wasn't merited at all. This is the internet I suppose.
____________________________________________________________

Quote

your use of global variables is a very bad habit especially since you have globals that conflict with local variables.


I'm sorry that my variables have offended you, jimblumberg, but as stated, I did very little to his program.

Regards,
Gungnir.
Was This Post Helpful? 0
  • +
  • -

#10 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Guessing game array not loading/displaying correct info.

Posted 23 May 2011 - 07:20 AM

@Gungir
A person will never learn if you fix their problems for them. If you tell them where they have gone wrong and let them fix it themselves they will understand their mistake.
Also if I were a teacher and my student handed in code half-written by someone else I would be peeved. <_<
Was This Post Helpful? 0
  • +
  • -

#11 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Guessing game array not loading/displaying correct info.

Posted 23 May 2011 - 07:28 AM

Alright, that's a given but, can you please stop -repping me?

If I broke the rules, let a moderator deal with it.
It's not your place to police my posts.

This is bordering on the ridiculous.
I suggest getting a glass of water and some fresh air, come back, and remember that my posts don't affect you personally.

There's no reason to go out of your way to bully me into finding another forum.

My code was clearly commented and learning is at the OP's discretion.
Was This Post Helpful? 0
  • +
  • -

#12 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Guessing game array not loading/displaying correct info.

Posted 23 May 2011 - 07:50 AM

View PostGungnir, on 23 May 2011 - 08:28 AM, said:

Alright, that's a given but, can you please stop -repping me?

If I broke the rules, let a moderator deal with it.
It's not your place to police my posts.


one point of that reputation system is that you there is no need for a moderator to police you all the time. power to the people :gunsmilie:

View PostGungnir, on 23 May 2011 - 08:28 AM, said:

This is bordering on the ridiculous.
I suggest getting a glass of water and some fresh air, come back, and remember that my posts don't affect you personally.


Gungnir said:

I'm sorry that my variables have offended you, jimblumberg, but as stated, I did very little to his program.personally.


to me it seems that you are taking things personally. And yes your post will not affect us personally, but breaking the rules, posting bad practices (like here) or erroneous advice (like another topic) affects the OP and as I recall giving correct advice is important not a freaking number beneath your avatar.

This post has been edited by muballitmitte: 23 May 2011 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,094
  • Joined: 27-December 08

Re: Guessing game array not loading/displaying correct info.

Posted 23 May 2011 - 07:53 AM

Let's get things back on topic please. If you all do not have anything relevant to the OP's question, please leave the debate about the reputation system alone. :)
Was This Post Helpful? 0
  • +
  • -

#14 thedudeky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 30-January 11

Re: Guessing game array not loading/displaying correct info.

Posted 23 May 2011 - 09:00 AM

Hey guys, I didn't mean to start a brawl. Thanks to everyone for helping out. I figured it out late last night.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1