2

I am making a maths project where the user answers questions based on the difficulty level they select!

But every time the question is a division question It returns the lower-bound.

For example, if the code asked what is "5/3" the answer should be 2 but the code thinks the correct answer is 1.

Here is my code:

using System;

namespace mathstester
{
    class Program
    {
        enum UserDifficulty
        {
            Easy,
            Normal,
            Hard
        }
        public static void Main(string[] args)
        {
            string userDifficulty = "";
            do
            {
                Console.WriteLine("What difficulty level would you like to do! Please type E for Easy, N for Normal and H for hard");
                userDifficulty = Console.ReadLine().ToUpper();
            } while (userDifficulty != "E" && userDifficulty != "N" && userDifficulty != "H");

            int numberOfQuestions = 0;
            do
            {
                Console.Write("How many questions would you like to answer? Please type a number divisible by 10!");
                int.TryParse(Console.ReadLine(), out numberOfQuestions);
            } while (numberOfQuestions % 10 != 0);

            int numberOfQuestionsLeft = numberOfQuestions;
            Random random = new Random();
            int score = 0;

            while (numberOfQuestionsLeft > 0)
            {
                var operation = random.Next(1, 7);
                int number1 = 0;
                int number2 = 0;
                switch (userDifficulty)
                {
                    case "E":
                        switch (operation)
                        {
                            case 1:
                                number1 = random.Next(1000);
                                number2 = random.Next(1000);
                                break;
                            case 2:
                                number1 = random.Next(1000);
                                number2 = random.Next(1000);
                                break;
                            case 3:
                                number1 = random.Next(13);
                                number2 = random.Next(13);
                                break;

                        }
                        break;
                    case "N":
                        switch (operation)
                        {
                            case 1:
                                number1 = random.Next(1000);
                                number2 = random.Next(1000);
                                break;
                            case 2:
                                number1 = random.Next(1000);
                                number2 = random.Next(1000);
                                break;
                            case 3:
                                number1 = random.Next(1000);
                                number2 = random.Next(1000);
                                break;
                            case 4:
                                number1 = random.Next(1, 10000);
                                number2 = random.Next(1, 1000);
                                break;

                        }
                        break;
                    case "H":
                        switch (operation)
                        {
                            case 3:
                                number1 = random.Next(1000);
                                number2 = random.Next(1000);
                                break;
                            case 4:
                                number1 = random.Next(1, 10000);
                                number2 = random.Next(1, 1000);
                                break;
                            case 5:
                                number1 = random.Next(13);
                                number2 = random.Next(5);
                                break;
                            case 6:
                                number1 = random.Next(1000);
                                break;

                        }
                        break;
                }

                if(operation == 1 && (userDifficulty == "E" || userDifficulty == "N"))
                {
                    Console.Write($"What is {number1} + {number2} =");
                    int correctAnswer = number1 + number2;
                    int userAnswer = Convert.ToInt32(Console.ReadLine());
                    if (correctAnswer == userAnswer)
                    {
                        Console.WriteLine("Well Done!");
                        score++;
                    }
                    else
                    {
                        Console.WriteLine("Your answer is incorrect!");
                    }
                    numberOfQuestionsLeft--;
                }
                else if (operation == 2 && (userDifficulty == "E" || userDifficulty == "N"))
                {
                    Console.Write($"What is {number1} - {number2} =");
                    int correctAnswer = number1 - number2;
                    int userAnswer = Convert.ToInt32(Console.ReadLine());
                    if (correctAnswer == userAnswer)
                    {
                        Console.WriteLine("Well Done!");
                        score++;
                    }
                    else
                    {
                        Console.WriteLine("Your answer is incorrect!");
                    }
                    numberOfQuestionsLeft--;
                }
                else if (operation == 3 && (userDifficulty == "E" || userDifficulty == "N" || userDifficulty == "H"))
                {
                    Console.Write($"What is {number1} * {number2} =");
                    int correctAnswer = number1 * number2;
                    int userAnswer = Convert.ToInt32(Console.ReadLine());
                    if (correctAnswer == userAnswer)
                    {
                        Console.WriteLine("Well Done!");
                        score++;
                    }
                    else
                    {
                        Console.WriteLine("Your answer is incorrect!");
                    }
                    numberOfQuestionsLeft--;
                }
                else if (operation == 4 && (userDifficulty == "N" || userDifficulty == "H") && (number1 > number2))
                {
                    Console.Write($"To the nearest integer, What is {number1} / {number2} =");
                    double correctAnswer = (number1 / number2);
                    double roundedCorrectAnswer = Math.Round(correctAnswer);
                    int userAnswer = Convert.ToInt32(Console.ReadLine());
                    if (roundedCorrectAnswer == userAnswer)
                    {
                        Console.WriteLine("Well Done!");
                        score++;
                    }
                    else
                    {
                        Console.WriteLine("Your answer is incorrect!");
                    }
                    numberOfQuestionsLeft--;
                }
                else if (operation == 5 && userDifficulty == "H")
                {
                    Console.Write($"What is {number1} ^ {number2} =");
                    double correctAnswer = Math.Pow(number1, number2);
                    int userAnswer = Convert.ToInt32(Console.ReadLine());
                    if (correctAnswer == userAnswer)
                    {
                        Console.WriteLine("Well Done!");
                        score++;
                    }
                    else
                    {
                        Console.WriteLine("Your answer is incorrect!");
                    }
                    numberOfQuestionsLeft--;
                }
                else if (operation == 6 && userDifficulty == "H")
                {
                    Console.Write($"To the nearest integer, What is √{number1} =");
                    double correctAnswer = Math.Sqrt(number1);
                    double roundedCorrectAnswer = Math.Round(correctAnswer);
                    int userAnswer = Convert.ToInt32(Console.ReadLine());
                    if (roundedCorrectAnswer == userAnswer)
                    {
                        Console.WriteLine("Well Done!");
                        score++;
                    }
                    else
                    {
                        Console.WriteLine("Your answer is incorrect!");
                    }
                    numberOfQuestionsLeft--;
                }
            }
            Console.WriteLine($"You got a score of {score} out of {numberOfQuestions}");
        }
    }
}

Here is what happens:

enter image description here

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
qqqqqkks
  • 62
  • 16
  • 1
    You shouldn't be doing `if (roundedCorrectAnswer == userAnswer)` between `double` and `int`. This can fail because binary floating point can't accurately represent all base 10 numbers. You should cast `roundedCorrectAnswer` to `int`. – Zer0 Mar 14 '20 at 00:07

2 Answers2

3

Since number1 and number2 are integers, dividing them would be done using integer division, and only retain the "whole" part of the result (i.e., the part left of the decimal point).

You could cast them (or at least of one them) to doubles before performing the division in order to get the "proper" result:

double correctAnswer = ((double) number1) / number2;
Mureinik
  • 297,002
  • 52
  • 306
  • 350
2

The issue is with the line double correctAnswer = (number1 / number2);, the expression on the right is a division of two integers and thus is done as an integer division, which rounds down. That integer value is then cast to a double, but the rounding has already taken place. To store the actual, probably non-integer value of number1 / number2, you'd need to cast one or both of them as a double before dividing:

double correctAnswer = ((double) number1) / ((double) number2);
The Zach Man
  • 738
  • 5
  • 15