Find the answer to your Linux question: Entire Site Articles Downloads Forums Linux Hosting

# Thread: Simple C++ guessing game.

Enjoy an ad free experience by logging in. Not a member yet? Register.
1. ## Simple C++ guessing game.

Hello all, I have been learning C++ and my last project that I needed help with was a calculator but we got that figured out. I have been working on a simple guessing game. I have to game done but I wanted to add a points system and a number of guesses you can guess. I haven't added the loops yet because something is messing me up. The random number generator is working fine but the guess isn't. It says correct even when wrong. Here is my code. I have tried all that I know which is not that much so maybe someone with more experience than me can help out :

Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

class GuessingGame{
private:
int NumberOfGuesses = 5;
int correctGuesses = 0;
int NumberToGuess;
int Guess1;
int loop = 0;
public:
int Guess()
{
cout << "Please enter a number to guess: ";
cin >> Guess1;

if(Guess1 == NumberOfGuesses){
cout << "you have guessed correctly!";
cout << "You have " << correctGuesses << " points.";
cout << " " << NumberToGuess;//this is to test the random number generator.
}else{
cout << "your guess was incorrect!";
cout << "You have " << NumberOfGuesses << " left";
}
}
int randomNumber()
{
srand(time(0));
NumberToGuess = rand() %6;
}
int guesses()
{
if(Guess1 == NumberToGuess){
correctGuesses = correctGuesses + 1;
}else{
NumberOfGuesses++;
}

}

};

int main (void)
{
GuessingGame GameObj;
GameObj.randomNumber();
GameObj.Guess();
GameObj.guesses();
return 0;
}```

2. It might help is the compare in the method "Guess", actually compared the correct variable to determine that you guessed correctly rather to the number of times you have guessed.

3. Don't use cin on its own to get console input.

There be dragons down that path and I've seen many weird things happen whereby the program ends up apparently having no clue as to what was typed in last.

Why did you not use getline(cin, TARGET VARIABLE); ?

Cheers - VP

4. \$spacer_open \$spacer_close
5. I would suggest adding the loops and then fixing the algorithm, because the loops may help you do just that. When you can see the consistency of the incorrect output it may help.

One question. Are you learning in school? or on your own?

If in school, then you can't help how they teach you to write your program, but if you are learning on your own, I would recommend to separate the different parts of the program into different files (since you are using objects, go ahead and have a main.cpp, other.cpp, and a .h header file. It will clean the code up and probably help a lot. You should also be using a constructor to initialize the data instead of initializing it in the would be header file (and putting the methods in the separate .cpp file, because the only methods you really want in a header is one liners).

But yeah, adding the loops would help me figure it out, because I could see a pattern of inconsistency or consistency and reason out what was causing it.

I didn't look at it much, but shouldn't this
Code:
`if(Guess1 == NumberOfGuesses)`
be this?
Code:
`if (Guess1 == NumberToGuess)`
If not, I apologize.

6. Ok, I don't know if it was homework or not, but I coded this for you with the loop and I think I may have fixed it. I also separated it and added a makefile. This was nice practice for me, so thank you as well. I was coding anyway and this was a nice distraction. It may be a little sloppy as well, as I threw this together in about 15 minutes.

If you don't want to use the make file (just type make) then you would compile it as so:
Code:
```g++ -c main.cpp guess.cpp
g++ -o guess main.o guess.o

Then run:
./guess```
main.cpp
Code:
```#include <iostream>
#include "guess.h"
int main (void)
{
GuessingGame GameObj;
GameObj.randomNumber();
GameObj.Guess();
GameObj.guesses();
return 0;
}```
guess.h
Code:
```#ifndef GUESSINGGAME_H
#define GUESSINGGAME_H

class GuessingGame
{
private:
int NumberOfGuesses;
int correctGuesses;;
int NumberToGuess;
int Guess1;
public:
GuessingGame (void);
void Guess (void);
void randomNumber (void);
void guesses (void);
};

#endif```
guess.cpp
Code:
```#include <iostream>
#include <cstdlib>
#include <ctime>
#include "guess.h"
using std::cout;
using std::cin;
using std::endl;

GuessingGame::GuessingGame (void)
{
NumberOfGuesses = 5;
correctGuesses = 0;
}

void GuessingGame::Guess (void)
{
while (NumberOfGuesses > 0)
{
cout << "Please enter a number to guess: ";
cin >> Guess1;

if(Guess1 == NumberToGuess)
{
cout << "you have guessed correctly!" << endl;
cout << "You have " << correctGuesses << " points." << endl;
correctGuesses++;
cout << " " << NumberToGuess;//this is to test the random number generator.
}
else
{
cout << "your guess was incorrect!" << endl;
cout << "You have " << NumberOfGuesses -1 << " left" << endl;
}
NumberOfGuesses--;
}
}

void GuessingGame::randomNumber (void)
{
srand(time(0));
NumberToGuess = rand() %6;
}

void GuessingGame::guesses (void)
{
if(Guess1 == NumberToGuess)
{
correctGuesses = correctGuesses + 1;
}
else
{
NumberOfGuesses++;
}
}```
Makefile
Code:
```CC=gcc

guess : main.o guess.o
g++ -o guess main.o guess.o
main.o : main.cpp
g++ -c main.cpp
guess.o : guess.cpp
g++ -c guess.cpp```

7. Oh awesome thanks man sorry for the late reply I have been very busy with college first year lol but thanks

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•