•Generate a random number from 1 to 100 inclusive.
•Input a guess. Use a while loop to check the guess to ensure that it is within the range of 1 to 100, If the guess is outside that range, continue to input guesses until it is within the desired range. Display an error message in yellow and honk the speaker (use a low frequency) if the guess was outside the range. Invalid guesses will not be counted.
•Compare a valid guess with the secret number. If the guess was correct, honk the speaker (use a higher frequency) and display the secret number and the number of guesses required in green.
•If the guess was lower than the secret number, display a message that the guess was too low.
•If the guess was higher than the secret number, display a message that the guess was too high.
•Continue to input guesses by using a while loop until the secret number has been guessed, or the number of guesses reaches 6.
•If the number of guesses reaches 6 and the secret number was not guessed, display a message in yellow indicating that the player has lost the game, and show the secret number.

so far i have this...

{
        static void Main(string[] args)
        {       
            Console.Title = "ICA14 - Guess the Number";
            Random random = new Random();
            int iGuess = 0;
            int iCount = 0;
            int iRandom = random.Next(1, 100);


            Console.WriteLine("\t\t\tICA14 - Guess the Number");

            Console.WriteLine("\nTry to guess a secret number from 1 to 100 is 6 guesses.");

            Console.Write("\nEnter guess #1: ");
            iGuess = int.Parse(Console.ReadLine());
            
            while (iCount < 5)
            {

                while ((iGuess <= 0) || (iGuess > 100))
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("You have entered an invalid guess.");
                    Console.ResetColor();
                    Console.Write("Enter guess #{0}: ", (iCount + 1));
                    iGuess = int.Parse(Console.ReadLine());

                    if (iGuess > iRandom)
                        Console.WriteLine("Your guess was too high");
                    Console.Write("Enter guess #{0}: ", (iCount + 1));
                    iGuess = int.Parse(Console.ReadLine());

                    else if (iGuess < iRandom)
                        Console.WriteLine("Your guess was too low");
                    Console.Write("Enter guess #{0}: ",(iCount + 1));
                    iGuess = int.Parse(Console.ReadLine());

		
                }

            }

            Console.WriteLine("Press the <Enter> key to exit:");
            Console.ReadLine();
        }

    }

Dani AI

Generated

The snippet posted by has the right idea but the input/loop structure makes the flow fragile: it reads input multiple times, can throw on non-numeric input, and mixes validation with game logic so invalid guesses can accidentally be counted (and the prompt numbering gets out of sync). rightly flagged parsing and loop/count issues. The pattern below separates concerns: validate input (numeric and in-range) first, only then increment the valid-guess counter and compare to the secret; use int.TryParse to avoid exceptions; and keep Random seeded once.

using System;

class Program
{
    static void Main()
    {
        var rnd = new Random();
        int secret = rnd.Next(1, 101);
        const int maxAttempts = 6;
        int attempts = 0;
        bool guessed = false;

        Console.WriteLine("\t\t\tICA14 - Guess the Number");
        Console.WriteLine("\nTry to guess a secret number from 1 to 100 in 6 guesses.");

        while (attempts < maxAttempts && !guessed)
        {
            Console.Write("Enter guess #{0}: ", attempts + 1);
            string input = Console.ReadLine();
            if (!int.TryParse(input, out int guess) || guess < 1 || guess > 100)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("You have entered an invalid guess.");
                Console.ResetColor();
                try { Console.Beep(300, 200); } catch { }
                continue;
            }

            attempts++;

            if (guess == secret)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Correct! The secret number was {0}. Guesses: {1}", secret, attempts);
                Console.ResetColor();
                try { Console.Beep(1000, 300); } catch { }
                guessed = true;
            }
            else if (guess < secret) Console.WriteLine("Your guess was too low.");
            else Console.WriteLine("Your guess was too high.");
        }

        if (!guessed)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("You've used all {0} guesses. The secret number was {1}.", maxAttempts, secret);
            Console.ResetColor();
        }

        Console.WriteLine("Press <Enter> to exit:");
        Console.ReadLine();
    }
}

Notes: wrap Console.Beep in try/catch since it may not be supported on all platforms; always ResetColor() after colored output; increment the attempt counter only after a valid guess so invalid inputs are not counted. This addresses the prompts, coloring and beeps required while keeping the logic clear and robust.

Line 8: Returns a number from 1 to 99, not 1 to 100. The upper bound is exclusive in Random.Next

Line 16: What happens when the user types "I want to quit this game" instead of a number?

Line 18 only lets you enter 5 guesses. The requirements say 6.

You only check to see if they guessed the correct number if they enter an incorrect number. You need to rethink the logic of this whole section (Lines 21-42)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.