[RESOLVED] C# Help: I can't find my coding error... :(
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] C# Help: I can't find my coding error... :(

  1. #1
    Join Date
    Oct 2011
    Posts
    13

    [RESOLVED] C# Help: I can't find my coding error... :(

    The issues Ive noticed is that the score is not increasing properly... it goes up by 3, when it should be increasing by 10.

    Computers have been widely used to test students on their ability to solve arithmetic problems. Write a program that will test a student on addition, subtraction, or multiplication using random integers between 1 and 100. The student begins by choosing the type of problem and then is asked 10 problems with 3 chances to answer each correctly. If after 3 chances the answer is still incorrect, the correct answer is displayed. A score is calculated by awarding 10 points for a correct answer on the first try, 5 points on the second try, 3 points on the third try, and 0 points if all three attempts are wrong. The program output may look similar to:

    Enter the number for the problem type desired.
    1. Addition
    2. Subtraction
    3. Multiplication

    Enter choice: 1

    25 + 73 = 79
    WRONG Try again.
    25 + 73 = 98
    CORRECT

    12 + 94 = 106
    CORRECT

    15 + 6 = 19
    WRONG Try again.
    15 + 6 = 18
    WRONG Try again.
    15 + 6 = 20
    You have missed 3 times. The answer is 21.

    36 + 49 = 85
    CORRECT


    17 + 65 = 82
    CORRECT

    Your score = 70



    class Program
    {
    static Random randy = new Random();

    static int score;
    static int chances;

    static void ScoreSolver(int guesses)
    {
    if (guesses == 1)
    {
    score += 10;
    Console.WriteLine("+10 ... Your score is " + score);
    }
    if(guesses == 2)
    {
    score += 5;
    Console.WriteLine("+5 ... Your score is " + score);
    }
    if (guesses == 3)
    {
    score += 3;
    Console.WriteLine("+3 ... Your score is " + score);
    }
    if (guesses > 3)
    {
    Console.WriteLine("+0 ... Your score is " + score);
    }

    }

    static void Addition(int number1, int number2)
    {
    string question = "";
    int answer = 0;


    for (int loopy = 1; loopy <= 10; loopy++)
    {

    chances = 1;
    number1 = randy.Next(1, 101);
    number2 = randy.Next(1, 101);

    question = "\n" + number1 + " + " + number2 + " = ";
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());


    do
    {
    if (answer != number1 + number2)
    {
    Console.WriteLine("Wrong Try Again.");
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());
    }

    chances++;

    } while (chances < 3);

    if (answer == number1 + number2)
    {
    Console.WriteLine("\nCORRECT!");
    }
    else if (chances == 3)
    {
    Console.WriteLine("You missed 3 Times. The answer is " + (number1 + number2));
    chances++;
    }

    ScoreSolver(chances);
    }
    }


    static void Subtraction(int number1, int number2)
    {
    string question = "";
    int answer = 0;

    for (int loopy = 1; loopy <= 10; loopy++)
    {
    chances = 1;
    number1 = randy.Next(1, 101);
    number2 = randy.Next(1, 101);
    question = "\n" + number1 + " - " + number2 + " = ";
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());

    do
    {
    chances++;

    if (answer != number1 - number2)
    {
    Console.WriteLine("Wrong Try Again.");
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());
    }

    } while (chances < 3);

    if (answer == number1 - number2)
    {
    Console.WriteLine("\nCORRECT!");
    }
    else if (chances == 3)
    {
    Console.WriteLine("You missed 3 Times. The answer is " + (number1 - number2));
    chances++;
    }

    ScoreSolver(chances);
    }
    }


    static void Multiplication(int number1, int number2)
    {
    string question = "";
    int answer = 0;


    for (int loopy = 1; loopy <= 10; loopy++)
    {
    chances = 1;
    number1 = randy.Next(1, 101);
    number2 = randy.Next(1, 101);
    question = "\n" + number1 + " x " + number2 + " = ";
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());

    do
    {
    if (answer != number1 * number2)
    {
    Console.WriteLine("Wrong Try Again.");
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());
    }

    chances++;

    } while (chances < 3);

    if (answer == number1 * number2)
    {
    Console.WriteLine("\nCORRECT!");
    }
    else if (chances == 3)
    {
    Console.WriteLine("You missed 3 Times. The answer is " + number1 * number2);
    chances++;
    }

    ScoreSolver(chances);
    }
    }

    static void StartUp()
    {
    int problemType = -1;

    do
    {
    Console.Write("\nEnter the number for the problem type desired.");
    Console.Write("\n\t1. Addition \n\t2. Subtraction \n\t3. Multiplication \nDesired Type: ");
    //problemType = int.Parse(Console.ReadLine());
    do
    {
    try
    {
    problemType = int.Parse(Console.ReadLine());

    }
    catch (Exception e)
    {
    Console.Write("\nProgram threw an exception: ");
    Console.Write(e.Message);
    Console.Write("\nChoose Again: ");
    }
    } while (problemType == -1);

    } while (problemType < 1 || problemType > 4);

    if (problemType == 1)
    {
    Addition(randy.Next(1, 101), randy.Next(1, 101));
    }
    if (problemType == 2)
    {
    Subtraction(randy.Next(1, 101), randy.Next(1, 101));
    }
    if (problemType == 3)
    {
    Multiplication(randy.Next(1, 101), randy.Next(1, 101));
    }
    }

    static void Main(string[] args)
    {
    StartUp();

    Console.WriteLine("Thanks for playing");

    Console.ReadKey(true);



    }
    }
    }

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    71

    Re: C# Help: I can't find my coding error... :(

    I am looking at this through my phone so I am not completely sure but in your do-while loops how are you breaking out if the answer matches num 1 + num 2? From the looks of it you are making the loop run 3 times regardless of right or qrong answer and thats why in the end the score will always be giving you 3 points. You could break the loop or in your while condition set it to run while chances < 3 AND answer != num 1 + num2. That way when either condition is met it breaks out of the loop.

  3. #3
    Join Date
    Oct 2011
    Posts
    13

    Re: C# Help: I can't find my coding error... :(

    Quote Originally Posted by Ubiquitous View Post
    You could break the loop or in your while condition set it to run while chances < 3 AND answer != num 1 + num2. That way when either condition is met it breaks out of the loop.
    Okay, so I changed it to "while (chances < 3 && answer != number1 + number2);" and instead of getting 3 points each time. I get 5, unless I get it wrong 2 times and the third time is correct. So... Its better, but Im still unable to obtain +10 points.

  4. #4
    Join Date
    Oct 2011
    Posts
    13

    Re: C# Help: I can't find my coding error... :(

    Ive made some changes based on the last comment.
    And new problems have been noticed, such as: sometimes the message "you missed 3 times. the answer...." will appear to soon.


    Quote Originally Posted by Nin9tySe7en
    class Program
    {
    static Random randy = new Random();

    static int score = 0;
    static int chances = 0;

    static void ScoreSolver(int guesses)
    {
    if (guesses == 1)
    {
    score += 10;
    Console.WriteLine("+10 ... Your score is " + score);
    }
    else if(guesses == 2)
    {
    score += 5;
    Console.WriteLine("+5 ... Your score is " + score);
    }
    else if (guesses == 3)
    {
    score += 3;
    Console.WriteLine("+3 ... Your score is " + score);
    }
    if (guesses > 3)
    {
    Console.WriteLine("+0 ... Your score is " + score);
    }

    }

    static void Addition(int number1, int number2)
    {
    string question = "";
    int answer = 0;


    for (int loopy = 1; loopy <= 10; loopy++)
    {
    number1 = randy.Next(1, 101);
    number2 = randy.Next(1, 101);

    question = "\n" + number1 + " + " + number2 + " = ";
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());


    do
    {
    if (answer != number1 + number2)
    {
    Console.WriteLine("Wrong Try Again.");
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());
    chances++;
    }
    else
    {
    chances++;
    }



    } while (chances < 3 && answer != number1 + number2);

    if (answer == number1 + number2)
    {
    Console.WriteLine("\nCORRECT!");
    }
    else if (chances == 3)
    {
    Console.WriteLine("You missed 3 Times. The answer is " + (number1 + number2));
    }

    ScoreSolver(chances);
    }
    }


    static void Subtraction(int number1, int number2)
    {
    string question = "";
    int answer = 0;

    for (int loopy = 1; loopy <= 10; loopy++)
    {
    chances = 1;
    number1 = randy.Next(1, 101);
    number2 = randy.Next(1, 101);
    question = "\n" + number1 + " - " + number2 + " = ";
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());

    do
    {
    chances++;

    if (answer != number1 - number2)
    {
    Console.WriteLine("Wrong Try Again.");
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());
    }

    } while (chances < 3);

    if (answer == number1 - number2)
    {
    Console.WriteLine("\nCORRECT!");
    }
    else if (chances == 3)
    {
    Console.WriteLine("You missed 3 Times. The answer is " + (number1 - number2));
    chances++;
    }

    ScoreSolver(chances);
    }
    }


    static void Multiplication(int number1, int number2)
    {
    string question = "";
    int answer = 0;


    for (int loopy = 1; loopy <= 10; loopy++)
    {
    chances = 1;
    number1 = randy.Next(1, 101);
    number2 = randy.Next(1, 101);
    question = "\n" + number1 + " x " + number2 + " = ";
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());

    do
    {
    if (answer != number1 * number2)
    {
    Console.WriteLine("Wrong Try Again.");
    Console.Write(question);
    answer = int.Parse(Console.ReadLine());
    }

    chances++;

    } while (chances < 3);

    if (answer == number1 * number2)
    {
    Console.WriteLine("\nCORRECT!");
    }
    else if (chances == 3)
    {
    Console.WriteLine("You missed 3 Times. The answer is " + number1 * number2);
    chances++;
    }

    ScoreSolver(chances);
    }
    }

    static void StartUp()
    {
    int problemType = -1;

    do
    {
    Console.Write("\nEnter the number for the problem type desired.");
    Console.Write("\n\t1. Addition \n\t2. Subtraction \n\t3. Multiplication \nDesired Type: ");
    //problemType = int.Parse(Console.ReadLine());
    do
    {
    try
    {
    problemType = int.Parse(Console.ReadLine());

    }
    catch (Exception e)
    {
    Console.Write("\nProgram threw an exception: ");
    Console.Write(e.Message);
    Console.Write("\nChoose Again: ");
    }
    } while (problemType == -1);

    } while (problemType < 1 || problemType > 4);

    if (problemType == 1)
    {
    Addition(randy.Next(1, 101), randy.Next(1, 101));
    }
    if (problemType == 2)
    {
    Subtraction(randy.Next(1, 101), randy.Next(1, 101));
    }
    if (problemType == 3)
    {
    Multiplication(randy.Next(1, 101), randy.Next(1, 101));
    }
    }

    static void Main(string[] args)
    {
    StartUp();

    Console.WriteLine("Thanks for playing");

    Console.ReadKey(true);
    }
    }
    }
    I've forgotten how to quote coding.. so i just did this ^

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,000

    Re: C# Help: I can't find my coding error... :(

    Quote Originally Posted by Nin9tySe7en View Post
    I've forgotten how to quote coding
    Use [code] and [/code] tags.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    71

    Re: C# Help: I can't find my coding error... :(

    [code]Code goes here[/code]

    Ok I am on an actual computer so I will try to help you out better now that I can read the full code.

    The new issues that arose are from you adding to chances right at the beginning of the loop and your chances value starting at 1.

    Code:
    static void Subtraction(int number1, int number2)
    {
       string question = "";
       int answer = 0;
    
       // Run loop 10 times
       for (int loopy = 1; loopy <= 10; loopy++)
       {
          chances = 1;
          number1 = randy.Next(1, 101);
          number2 = randy.Next(1, 101);
          question = "\n" + number1 + " - " + number2 + " = ";
          Console.Write(question);
          answer = int.Parse(Console.ReadLine());
    
          // Run loop 3 times or until answer is found
          do
          {
             // Adds to chances value before giving the user a chance to get right answer
             chances++;
    
             // Checks for wrong answer
             if (answer != number1 - number2)
             {
                Console.WriteLine("Wrong Try Again.");
                Console.Write(question);
                answer = int.Parse(Console.ReadLine());
             }
    
          } while (chances < 3);
    
          // Code checks for write answer after loop conditions are met
          if (answer == number1 - number2)
          {
             Console.WriteLine("\nCORRECT!");
          }
          else if (chances == 3)
          {
             Console.WriteLine("You missed 3 Times. The answer is " + (number1 - number2));
             chances++;
          }
    
          // Sends value to ScoreSolver to figure out points to award
          ScoreSolver(chances);
       }
    }

    Ok so I've commented whats going on in your code in hopes it will help you understand what is happening.


    Code:
            static void Addition()
            {
                // Run loop 10 times
                for (int i = 0; i < 10; i++)
                {
                    // Initialize Variables
                    int num1 = rand.Next(1, 100);
                    int num2 = rand.Next(1, 100);
                    int answer = num1 + num2;
                    int attempts = 0;
                    int userGuess = 0;
    
                    string mathProblem = num1 + " + " + num2 + " = ";
    
                    // Write out problem and ask user for answer
                    while (attempts < 3 && userGuess != answer)
                    {
                        Console.Write(mathProblem);
                        userGuess = int.Parse(Console.ReadLine());
    
                        // Check answer and run code accordingly
                        if (userGuess != answer)
                        {
                            Console.WriteLine("Wrong!\n");
                            attempts++;
                        }
                        else
                        {
                            Console.WriteLine("Correct!\n");
                        }
                    }
    
                    //Run Score evaluator once loop is completed
                    ScoreSolver(attempts);
                }
            }
    The code I posted should work but I cannot be to sure since I did not get to test it out.

    In the code I gave you the reason the values are initiated inside the loop is so you get fresh values at the beginning of each loop iteration. By doing this you prevent having to reset attempts somewhere down the code structure. If you do not reset attempts each time a new problem is created you will keep your previous attempts value and it will cause issues. The while loop would not run for the rest of the answers once 3 answers were wrong.

    You may also want to keep in mind that 0 is a number.

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,074

    Re: C# Help: I can't find my coding error... :(

    When posting questions, please also include details. Things such as waht is to be achieved and what is happening is crucial to us to know what you want

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center