0

I am fairly new to OO programming, I have created a simple "Guess the number" game, it functions correctly. However, when attempting to implement a loop errors occur. More specifically, I want the user to restart the program through a console [not compiling and running the game again].

I need help with the static void ExitGame() method. At this moment in time, I am working on "RepL.it" and the generated errors are as followed:

main.cs (10,10): warning CS0649: Field GuessTheNumber.Game.replay' is never assigned to, and will always have its default valuenull' Compilation succeeded - 1 warning (s) Welcome to the guessing game. Please press enter.

using System;

namespace GuessTheNumber
{
  class Game
  {

  static int UserGuess;
  static int Answer;
  string replay;
  static string EndProg = "No";

    static void Main (string[] args)
    {
      Console.Title = "Guess the number.";

      EntryMessage();
      GenerateRandom();

      while (EndProg == "No") {
        askData();
      }
    }

    static void EntryMessage()
    {
      Console.WriteLine("Welcome to the guessing game. Please press enter.");
      Console.ReadLine();
    }

    public static void askData()
    {
      while (EndProg == "No")
      {
        Console.WriteLine(Answer);
        Console.WriteLine("Guess a number between 1 and 100.");
        UserGuess = Convert.ToInt32(Console.ReadLine());
        WinLose();
      }
      askData();
    }

    public void askData(bool endProg)
    {
      Console.WriteLine("Does you want to play again");
      if (replay == "y")//; Remove this semicolon
      {
      Console.WriteLine("\nOkay, guess again");
      askData(EndProg == "No");
      }
      else if (replay == "n")//; Remove this semicolon
      {
        askData(EndProg == "Yes");
      }
      else
      {
        Console.ReadLine();
      }
    }

    static void GenerateRandom()
    {
      Random random = new Random();
      Answer = random.Next(0,101);
    }
      static void WinLose() 
      {
        if (UserGuess == Answer)
        {
          Console.WriteLine("Correct number!");
          EndProg="Yes";
        }
        else if (UserGuess > Answer)
        {
          Console.WriteLine("Too high.");
          EndProg="No";
        }
        else if (UserGuess < Answer)
        {
          Console.WriteLine("Too Low.");
          EndProg="No";
        }
        else 
        {
          Console.WriteLine("Invalid answer.");
          EndProg="No";
        }
      }
  }
}
K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
  • There are too many typos in your code. You need to learn to read the code you write. You can notice that all `if` statements are wrong since there is no indentation after the braces – Camilo Terevinto Dec 15 '18 at 23:14

2 Answers2

0

There's a number of syntactical errors in your code:

  1. If statement parentheses shouldn't be suffixed with a semicolon

    else if (replay == "n");
    

    should be

    else if (replay == "n")
    
  2. Your askData() method is sometimes being called with a bool argument but doesn't take one

    void askData()
    

    should be

    void askData(bool endProg)
    
  3. You are mixing and matching static and non-static methods and need to decide whether to implement this logic in a static or instance context

Creyke
  • 1,887
  • 2
  • 12
  • 16
  • FYI: we don't answer typo questions, we close and delete them. – Camilo Terevinto Dec 15 '18 at 23:15
  • Okay, thanks for your help :) But I am still struggling to correct my errors :P exit status 1 main.cs(40,7): error CS0103: The name `ExitGame' does not exist in the current context Compilation failed: 1 error(s), 0 warnings –  Dec 15 '18 at 23:25
  • This will be point 3. You need to make all your methods static for now, as well as your replay variable. This should then compile. You should then consider moving to using class instances, i.e. var game = new Game(). Best to do some reading: https://stackoverflow.com/questions/4124102/whats-a-static-method-in-c – Creyke Dec 15 '18 at 23:30
-1

I believe the culprit here is the semicolons you have after your if/else statements:

    static void ExitGame()
{
  Console.WriteLine("Does you want to play again");
  if (replay == "y")//; Remove this semicolon
  {
  Console.WriteLine("\nOkay, guess again");
  askData(EndProg == "No");
  }
  else if (replay == "n")//; Remove this semicolon
  {
    askData(EndProg == "Yes");
  }
  else
  {
    Console.ReadLine();
  }
}

Those semicolons are terminating the statement, which would then make the Else If/Else blocks not follow an IF block, which is syntactically invalid.

This also explains the empty statement warnings the compiler is generating.

Chris Thompson
  • 490
  • 5
  • 17