Continue looping between classes after incorrect input

I am trying to create a class that collects the sum of an integer that is collected from a console window. First, the class asks how many numbers to sum, and then reads that there are a lot of integers from the console.

If the user enters a number with a decimal point, which is unacceptable, I have another class called Input.cs that sends back a message that asks them to try again. the class has 4 methods, WriteProgamInfo, ReadInput, SumNumbers and ShowResults. WriteProgamInfo just prints some information with Console.WriteLine, ReadInput asks how many numbers the user wants to add,

private void ReadInput()
{
    Console.Write("Number of values to sum? ");
    numOfInput = Input.ReadIntegerConsole(); //This here is from the Input class that sends an error message if a decimal is being used. This works fine.
    Console.WriteLine();
}

But now the question that I have comes up, in SumNumbers, he asks the user to specify the value of the number that he / she wants to add using the for loop, which depends on what the user enters into ReadInput.

// asks for which numbers to be added, based on how many times
// is given by user in ReadInput
private void SumNumbers()
{
    int index;
    int num = 0;
    for (index = 1; index <= numOfInput; index++) // Loop that asks the quesion d
    {
        Console.Write("Please give the value no. " + index + " (whole number):");
        //This here is from the Input class that sends an error message if a decimal
        //is being used. This is where I have a problem. 
        num = Input.ReadIntegerConsole2();
        sum += num;
    }
}

I have a problem in that I want the error message to continue the for loop after sending the message. For example, I want to sum 2 numbers, so first I add 1, and then prints the console: give the value no.2 (integer): but, let's say I then type 1.6, which is not allowed, then the Imprints console: wrong input. Try again. Now I want this to continue with the same question as before: "Please indicate value No. 2 (integer):"

How can I do it? The input class is as follows:

public static int ReadIntegerConsole2()
{
    int input;
    if (int.TryParse(Console.ReadLine(), out input))
        return input;
    else
        Console.WriteLine("Wrong input. Please try again: ");
    Console.Write("Please give the value no. " + /*index from SumNumbers + */" (whole number):");
    return ReadIntegerConsole2();
}
+4
source share
3

, Int32.TryParse. , , out, false, .

public static bool ReadIntegerConsole2(out int number)
{
    int input;
    bool ok = true;

    number = 0;
    if (int.TryParse(Console.ReadLine(), out input))
        number = input;
    else
    {
        ok = false;
        Console.WriteLine("Wrong input. Please try again: ");
    }
    return ok;
}

, ReadInteger2

private void SumNumbers() 
{
    int index;
    int num = 0;
    for (index = 1; index <= numOfInput; index++) 
    {
        Console.Write("Please give the value no. " + index + " (whole number):");
        if(Input.ReadIntegerConsole2(out num))
            sum += num;
        else 
            // Decrement the counter to avoid skipping an input
            index--;
    }
}
+2

. , , int.TryParse(), . ReadIntegerConsole2(), :

public static bool TryReadIntegerConsole2(out int input)
{
    if (int.TryParse(Console.ReadLine(), out input))
        return true;

    Console.WriteLine("Wrong input. Please try again: ");
    return false;
}

:

private void SumNumbers()
{
    int index;
    int num = 0;
    for (index = 1; index <= numOfInput; index++) // Loop that asks the quesion d
    {
        do
        {
            Console.Write("Please give the value no. " + index + " (whole number):");
        } while (!Input.TryReadIntegerConsole2(out num));
        sum += num;
    }
}
+2

, , , , .

private int GetUserValue(int index)
{
    var secondValueValid = false;
    int secondValue;
    do
    {
        Console.WriteLine("Please enter the value at index {0}: ", index);
        if(int.TryParse(Console.ReadLine(), out secondValue))
        {
            secondValueValid = true;
        }
        else
        {
            Console.WriteLine("Value entered is not a whole integer, please try again.");
        }
    }
    while(!secondValueValid)

    return secondValue;
}

, , , SumNumbers , , :

private void SumNumbers()
{
    int index;
    int num = 0;
    for (index = 1; index <= numOfInput; index++)
    {
        num = GetUserValue(index);
        sum += num;
    }
}
+1
source

Source: https://habr.com/ru/post/1610616/


All Articles