Error in c# - Input string was not in a correct format, using an if statement with four ints

Viewed 48

Problem: In Chefland, a tennis game involves 4 referees. Each referee has to point out whether he considers the ball to be inside limits or outside limits. The ball is considered to be IN if and only if all the referees agree that it was inside limits.

Given the decision of the 4 referees, help Chef determine whether the ball is considered inside limits or not.

Input Format: The first line of input will contain a single integer TT, denoting the number of test cases. Each test case consists of a single line of input containing 4 integers R1, R2, R3, R4.

R can be either 0 or 1. If the input is 0, the ball is inside limits, and the output should be "IN". If the input is 1, the ball is outside limits, and output should be "OUT".

My Code:

using System;

public class Test
{
public static void Main()
{
    
    int t = Convert.ToInt32(Console.ReadLine());
    
    int r1 = Convert.ToInt32(Console.ReadLine());
    int r2 = Convert.ToInt32(Console.ReadLine());
    int r3 = Convert.ToInt32(Console.ReadLine());
    int r4 = Convert.ToInt32(Console.ReadLine());
    
    for(int i = 0; i < t; i++)
    {
        if(r1 == 0 && r2 == 0 && r3 == 0 && r4 == 0)
        {
            Console.WriteLine("IN");
        }
        else
        {
            Console.WriteLine("OUT");
        }
    }
    
    
}
}

The Error: Unhandled Exception: System.FormatException: Input string was not in a correct format.

Disclaimer: I've already checked other solutions, which I understand, for example solving this using an array,but I don't get why this solution doesn't work. Can someone help me understand please?

1 Answers

You need to read each line, and then parse the 4 referees from each line.

Each test case consists of a single line of input containing 4 integers R1, R2, R3, R4.

for(int i = 0; i < t; i++)
{
    var str = Console.ReadLine();
    var referees = l1.Split(',');
    var r1 = int.Parse(referees[0]);
    var r2 = int.Parse(referees[1]);
    var r3 = int.Parse(referees[2]);
    var r4 = int.Parse(referees[3]);

    if(r1 == 0 && r2 == 0 && r3 == 0 && r4 == 0)
    {
        Console.WriteLine("IN");
    }
    else
    {
        Console.WriteLine("OUT");
    }
}
Related