Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Why am i getting an error in C#? Please check the code and email me!

+4 votes
asked May 6, 2021 by Костадин Клемов (360 points)
closed 5 days ago by Admin
using System.IO;
using System;

class Program
{
    static void Main(string[] args)
    {
        int guess_count = 0;
        int guess_max = 3;
        string word = "kostadin";
        
        while (guess_count != guess_max)
        {
            Console.Write("Enter your guess: ");
            string guess_word = Console.ReadLine();
            
            if (guess_word == word)
            {
                Console.WriteLine("You win!");
                break;
            }
            guess_count++;
            
        }
        if (guess_word != word)
        {
            Console.WriteLine("You lose!");
        }
    }
}
closed with the note: answered

1 Answer

+2 votes
answered May 7, 2021 by Peter Minarik (87,810 points)
selected 5 days ago by Admin
 
Best answer

The scope of you variable guess_word was limited to within the while loop, hence outside of it in the if it is now known.

Your code fixed is below:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        int guess_count = 0;
        int guess_max = 3;
        string word = "kostadin";
        string guess_word;
        
        while (guess_count != guess_max)
        {
            Console.Write("Enter your guess: ");
            guess_word = Console.ReadLine();
            
            if (guess_word == word)
            {
                Console.WriteLine("You win!");
                break;
            }
            guess_count++;
            
        }
        if (guess_word != word)
        {
            Console.WriteLine("You lose!");
        }
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...