Alexander Turner
Alexander Turner

Reputation: 21

C# variable changes value unexpectedly

The title basically says it all. The variable "answer" in my code randomly changes value to another random number when it is defined. The program is supposed to ask ten random maths questions and tell the user if they have got the correct answer or not, however this doesn't seem to be working at all. Once the variable has changed, the program then also asks about 3 more questions, and tells the user the answer to each is incorrect.

   static void Main(string[] args)
    {

        Random R = new Random();
        double solution = 0;
        string sign = "";

        int score = 0;
        for (int i = 0; i < 10; i++)
        {
            int X = R.Next(1, 5);
            int Y = R.Next(1,10);
            int Z = R.Next(1,10);

            switch (X)
            {
                case 1:
                    solution = Y + Z;
                    sign = "+";
                    break;
                case 2:                       
                    solution = Y - Z;
                    sign = "-";
                    break;
                case 3:                        
                    solution = Y / Z;
                    sign = "/";
                    break;
                case 4:                        
                    solution = Y * Z;
                    sign = "X";
                    break;
            }
            Console.WriteLine("What is " + Y + " " + sign + " " + Z + "?");
             double answer = Console.Read();
            if (answer == solution)
            {
                Console.WriteLine("Correct");
                score = score + 1;
                Console.Read();
            }
            else if (answer != solution)
            {
                Console.WriteLine("Incorrect. The correct answer is " + solution);
                Console.Read();
            }
            else
            {
                Console.WriteLine("Error");
                Console.Read();
            }
        }
    }
}
}

Upvotes: 0

Views: 511

Answers (4)

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

Reputation: 6251

The problem is that you are using Console.Read() which returns only the next character code. Instead, you should use Console.ReadLine(). Also after you check the answers you use some unnecessary Console.Read()(s). I have removed them and this code is working perfectly fine: -

static void Main(string[] args)
{

    Random R = new Random();
    double solution = 0;
    string sign = "";

    int score = 0;
    for (int i = 0; i < 10; i++)
    {
        int X = R.Next(1, 5);
        int Y = R.Next(1,10);
        int Z = R.Next(1,10);

        switch (X)
        {
            case 1:
                solution = Y + Z;
                sign = "+";
                break;
            case 2:                       
                solution = Y - Z;
                sign = "-";
                break;
            case 3:                        
                solution = Y / Z;
                sign = "/";
                break;
            case 4:                        
                solution = Y * Z;
                sign = "X";
                break;
        }
        Console.WriteLine("What is " + Y + " " + sign + " " + Z + "?");
        double answer = double.Parse(Console.ReadLine());
        if (answer == solution)
        {
            Console.WriteLine("Correct");
            score = score + 1;
        }
        else if (answer != solution)
        {
            Console.WriteLine("Incorrect. The correct answer is " + solution);
        }
    }
}

Another thing is that the division questions are actually integer divisions. If you want decimals instead, you should use something like solution = Math.Round((double)((decimal)Y / (decimal)Z), 3); to check a value rounded to 3 decimal places.

Upvotes: 1

kevintjuh93
kevintjuh93

Reputation: 1010

Your problem is in Console.Read(). This will return the characters number. Starting at 48 (0x30) for character 0. That's why all of your answers are incorrect.

So I suggest you use Console.ReadLine() and then parse the string to answer.

        double answer;
        if (!double.TryParse(Console.ReadLine(), out answer))
        {
            Console.WriteLine("Error");
            continue;
        }

        if (answer == solution)
        {
            Console.WriteLine("Correct");
            score = score + 1;
        }
        else if (answer != solution)
        {
            Console.WriteLine("Incorrect. The correct answer is " + solution);
        }

Upvotes: 1

Rahul Jha
Rahul Jha

Reputation: 1141

Console.Read();

returns an int value(The ASCII code) and you are storing this as answer which is of type double ! Use

Console.ReadLine();

instead ! Also you need to convert your answer to a double value to be able to compare your answer to the solution!

Upvotes: 0

Philip Johnson
Philip Johnson

Reputation: 1081

I think your problem lies in the fact that Console.Read returns an integer code for the last character pressed (see https://msdn.microsoft.com/en-us/library/system.console.read(v=vs.110).aspx for details).

You need code something like this:

double answer = double.Parse(Console.ReadLine());

This takes the entire input (which might be more than one character) and converts it to a double (Console.ReadLine returns a string) so you are comparing like with like.

Upvotes: 0

Related Questions