-4

Hello i'm trying to create a calculator game but it loops only 2 times and (idk why)not til the user finds the result with input. And i'll later see how to make a random generator of numbers instead of writing them by myself in the code. Ty for helping.

Yassine

using System;

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

            Calcul(2, 2);
        }
        static void Calcul(int nombre1, int nombre2)
        {

            int result = nombre1 + nombre2;
            
            Console.WriteLine("Combien font " + nombre1 + " + " + nombre2);

            int supposed = int.Parse(Console.ReadLine());

            if(result != supposed)
            {
                Console.WriteLine("Try again!");
                supposed = int.Parse(Console.ReadLine());
              
            }
            else 
            {
                Console.Clear();
                Console.WriteLine("Correct!");
            }



        }
    }
}
Nippa
  • 1
  • 1
    "it loops only 2 times" - No it doesn't... do you know how to use *actual* loops? – Broots Waymb Sep 02 '20 at 14:11
  • (I tried the while loop but i didn't found how to deal with it.) – Nippa Sep 02 '20 at 14:11
  • 1
    What troubles are you having with a `while` loop? They are pretty basic features. Perhaps start out with a simple tutorial like [this one](https://www.tutorialsteacher.com/csharp/csharp-while-loop)? – Code Stranger Sep 02 '20 at 14:12
  • `while(true) { // put code inside loop here }` – Novaterata Sep 02 '20 at 14:13
  • Hint: `do{/* code here */}while(/* condition */);` – Zohar Peled Sep 02 '20 at 14:14
  • Change the `if` to `while (result != supposed)` if you want to loop until they get the correct answer. – Rufus L Sep 02 '20 at 14:18
  • `while (result != supposed) { Console.WriteLine("Try again!"); supposed = int.Parse(Console.ReadLine()); }` This is what i did but, how to put a "Correct!" message when the while is at his end? – Nippa Sep 02 '20 at 14:18
  • 2
    Duplicate of [While loop to confirm user input](https://stackoverflow.com/questions/33111978/while-loop-to-confirm-user-input), [loop until Console.ReadLine = 'y' or 'n'](https://stackoverflow.com/q/41609470/8967612), [How to loop user input until the datatype of the input is correct?](https://stackoverflow.com/q/4996793/8967612), and many others. – 41686d6564 stands w. Palestine Sep 02 '20 at 14:25

1 Answers1

1

You can change your if statement to a while loop whose condition is that the input does not match the result. We can also use int.TryParse with Console.ReadLine to handle cases where the user doesn't enter a valid number. This works by taking an out parameter that gets set to the parsed value if it's successful, and it returns a bool that indicates success. Perfect for a loop condition!

Then you can put the "success" message after the body of the while loop (because the only way to exit the loop is to get the correct answer). It would look something like:

static void Calcul(int nombre1, int nombre2)
{
    int result = nombre1 + nombre2;
    int input;

    Console.Write($"Combien font {nombre1} + {nombre2} = ");

    while (!int.TryParse(Console.ReadLine(), out input) || input != result)
    {
        Console.WriteLine("Incorrect, please try again.");
        Console.Write($"Combien font {nombre1} + {nombre2} = ");
    }

    Console.WriteLine("Correct! Press any key to exit.");
    Console.ReadKey();
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43