Click here to Skip to main content
16,016,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i get error under 1st Rep he said Use of unassigned local variable 'Rep' for this code

"while (Rep == 'Y' || Rep == 'y') ;"

What I have tried:

using System;

namespace Loop
{
    class Program
    {
        static void Main(string[] args)
        {

            int Num = 4;
            char Rep;
           do
            {
                Console.WriteLine("Guess The No.");
                int GNum = int.Parse(Console.ReadLine());
                if (GNum == Num) Console.WriteLine("Great You Win");
                else
                {
                    Console.WriteLine("Good Luck second time");
                    Console.WriteLine("Do you want to try again ? Y or N");
                    Rep = char.Parse(Console.ReadLine());
                }
           } while (Rep == 'Y' || Rep == 'y') ;
           
        }
    }
}
Posted
Updated 11-Sep-21 21:59pm

You get the error because there is a path through the code where the first use of the variable can occur without a value being assigned to it: if the if test passes then no value is ever assigned.

The simplest solution is to assign a value when you create the variable:
C#
char Rep = 'n';

But you should stick to naming conventions: local variables should be named in "camelCase" so they start with a lowercase letter:
C#
int num = 4;
int rep = 'n';
Classes, Enums, Properties, and Methods start with uppercase letters but not "local variables"
 
Share this answer
 
You can try this way too:
C++
using System;

namespace Loop
{
    class Program
    {
        static void Main(string[] args)
        {

            int Num = 4;
            char Rep;
           do
            {
                Console.WriteLine("Guess The No.");
                int GNum = int.Parse(Console.ReadLine());
                if (GNum == Num)
                    Console.WriteLine("Great You Win");
                else
                {
                    Console.WriteLine("Good Luck second time");
                }
                Console.WriteLine("Do you want to try again ? Y or N");
                Rep = char.Parse(Console.ReadLine());
           } while (Rep == 'Y' || Rep == 'y') ;
           
        }
    }
}

or:
C++
using System;

namespace Loop
{
    class Program
    {
        static void Main(string[] args)
        {

            int Num = 4;
            char Rep;
           do
            {
                Console.WriteLine("Guess The No.");
                int GNum = int.Parse(Console.ReadLine());
                if (GNum == Num) {
                    Console.WriteLine("Great You Win");
                    break;
                }
                else
                {
                    Console.WriteLine("Good Luck second time");
                    Console.WriteLine("Do you want to try again ? Y or N");
                    Rep = char.Parse(Console.ReadLine());
                }
           } while (Rep == 'Y' || Rep == 'y') ;
           
        }
    }
}

Advice: Do not try to save a keystroke by packing lines.
On long term, it makes your code more diffcult to read and understand. See the change I made for the 'you win'.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900