C++ - Code problem

Asked

Viewed 34 times

-2

most variables work, but "Guess" apparently must be modifiable and has many arguments in its function call. How do I fix it?

//header
#pragma once
class Game
{
public:
 Game();
 void reset();
 void run();
 int secretnum = rand() % 100 + 1;
 int guesscount = 5;
 int guess;
 int n;
 const int MAX_SECRET = 100;
 int Game::getNumber() {
  int guess;
  std::cin >> guess;
  while (std::cin.fail()) {
   std::cin.clear();
   std::cin.ignore(32767, '\n');
   std::cout << "Please enter a valid number: ";
   std::cin >> guess;
 }
private:

private:
 int  secretNumber;
};
//game.ccp
//initialize variables
#include "Game.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
//game
const int MAX_SECRET = 100;
Game::Game()
{
 srand(time(0));
}
void Game::reset()
{
 const int secretnum = rand() % MAX_SECRET + 1;
}
void Game::run()
{
 reset();
 std::cout << "secret number is: " << secretnum 
  << "\nTODO create game\n\n";
 int secretnum = rand() % 100 + 1;
 int guesscount = 5;
 int guess;
 char playGame;
 std::cout << "Guess a number between 1 and 100. " << "\n";
 std::cin >> guess;
 getNumber(guess);
 if (guess > 100 || guess < 1)
 {
  std::cout << "Guess must be between 1 and 100. " << "\n";
  std::cin >> guess;
 }
 else if (guess < secretnum && guesscount > 0)
 {
  guess = guess - 1;
  std::cout << "Higher. " << "\n";
  std::cout << "You have " << guesscount << " guess(es) left." << "\n";
  std::cin >> guess;
 }
 else if (guess > secretnum && guesscount > 0)
 {
  guess = guess - 1;
  std::cout << "Lower. " << "\n";
  std::cout << "You have " << guesscount << " guess(es) left." << "\n";
  std::cin >> guess;
 }
 else if (guess != secretnum && guesscount = 1)
 {
  std::cout << "GAME OVER " << "\n";
  playGame = false;
 }
 else if (guess = secretnum)
 {
  std::cout << "That's correct! Well done! " << "\n";
  playGame = false;
 }
}
int Game::getNumber() {
 int guess;
 std::cin >> guess;
 while (std::cin.fail()) {
  std::cin.clear();
  std::cin.ignore(32767, '\n');
  std::cout << "Please enter a valid number: ";
  std::cin >> guess;
 }
 return guess;
}
//source.ccp
#include <iostream>
#include <cstdlib>
#include "Game.h"
//main loop
int main() {
 Game game;

 std::cout << "Guess a number between 1 - 100\n" <<
  "Would you like to play (y/n): ";

 char playGame;
 std::cin >> playGame;
 while ('Y' == toupper(playGame))
 {
  game.run();
  std::cout << "play again (y/n): ";
  std::cin >> playGame;
  system("CLS");
 }
}
  • From what I could observe the variable guess, declared in more than one function, is a variable int and not a function.

1 answer

0

You are creating a class containing the variable guess. The problem is that within the class methods you declare a another variable place with the same name. That is, if you change the value of guess in this method you will change the local variable (which will be destroyed when leaving the function) and not the class attribute. Remove int guess; from within the methods.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.