URI problem 1061 - C #

0

I have a problem that the code below gives me the correct result, but when I send it to the site it informs the RUNTIME error (vector or array with less capacity than necessary for the problem, or when you try to access an invalid memory.) I can not see the reason for the error, could anyone help me?

exercise site for viewing: link

            Console.Write("Dia ");
        int diaI = int.Parse(Console.ReadLine());

        // vetor string dados ínicio da festa
        string line = Console.ReadLine();
        string[] tempoI = line.Split(':');
        int horaI = int.Parse(tempoI[0]);
        int minI = int.Parse(tempoI[1]);
        int segI = int.Parse(tempoI[2]);

        Console.Write("Dia ");
        int diaF = int.Parse(Console.ReadLine());

        // vetor string dados encerramento da festa
        string line2 = Console.ReadLine();
        string[] tempoF = line2.Split(':');
        int horaF = int.Parse(tempoF[0]);
        int minF = int.Parse(tempoF[1]);
        int segF = int.Parse(tempoF[2]);

        int diaT = 0, horaT = 0, minT = 0, segT = 0;

        //lógica para verificar tempo
        {
            //segundos
            if (segI > segF)
                segT = (60 - segI) + segF;
            else if (segI == segF)
                segT = 0;
            else
                segT = segF - segI;

            //minutos
            if (minI > minF)
                minT = (60 - minI) + minF;
            else if (minI == minF)
                minT = 0;
            else
                minT = minF - minI;

            //hora
            if (horaI > horaF)
            {
                horaT = (24 - horaI) + horaF;
                diaT = (diaF - diaI) - 1;
            }
            else if (horaI == horaF)
            {
                horaT = 0;
                diaT = (diaF - diaI) + 1;
            }
            else
            {
                horaT = horaF - horaI;
                diaT = (diaF - diaI);
            }

        }

        //imprimindo
        Console.WriteLine("{0} dia(s)", diaT);
        Console.WriteLine("{0} hora(s)", horaT);
        Console.WriteLine("{0} minuto(s)", minT);
        Console.WriteLine("{0} segundo(s)", segT);
    
asked by anonymous 30.04.2018 / 20:51

1 answer

0

The URI will not always send valid entries to your program, as far as I remember, so your code should wait for errors and treat them.

Example: // Caso 1 int horaI = int.Parse(tempoI[0]); int minI = int.Parse(tempoI[1]); int segI = int.Parse(tempoI[2]); // Caso 2 int horaF = int.Parse(tempoF[0]); int minF = int.Parse(tempoF[1]); int segF = int.Parse(tempoF[2]);

If there is any invalid line reading, it would result in an exception index out of bounds because it does not have an array of the size you are accessing. See if this is not the problem.

I recommend using TryParse instead of Parse. Another recommendation is to take a look at the DateTime class, I do not know if it's possible / allowed to use it but it does all the calculation you do on hand easily. The little I used the URI did using C.

    
12.11.2018 / 14:20