Certain infinity looping input numbers

Asked

Viewed 39 times

1

When I collect the number 14 in the input it goes into infinite looping, how can I solve this problem?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ex14
{
    class Program
{
    static void Main(string[] args)
    {
        int taxa_dada, total, s, qs;
        s = 5;
        qs = 0;

        Console.WriteLine("Qual é a taxa:");
        taxa_dada = int.Parse(Console.ReadLine());
        if (taxa_dada >= 8)
        {
            for (; ; )
            {
                if (taxa_dada >= s)
                {
                    taxa_dada -= s;
                    qs++;

                }
                else
                {
                    if (qs > 0)
                    {
                        Console.WriteLine("Total de {0}  selos: {1}", qs, s);
                    }
                    if (s == 5)
                    {
                        s = 3;
                        qs = 0;
                    }
                    if (taxa_dada == 0)
                    {
                        break;
                    }
                }
            }
        }

        else
        {
            Console.WriteLine("Taxa minima de 8 centavo!");

        }




        Console.ReadKey();

    }
}

}

  • 1

    The ideal in these simple cases is you do a table test that quickly finds the problem. Your logic inside the for is in fact not correct and does not allow the loop ends unless the input is multiples of 5.

  • yours is (; ; ) working ?

1 answer

1


I couldn’t quite understand what your code wants to do (want to count the amount of 5cent stamps given a fee?), but to remedy the problem of the infinite loop I recommend making the following approach:

  • An infinite loop (for(;;)) in this case is kind of prone to errors of this type that is happening with 14. I recommend in this case then use the while more or less as:

    while(taxa_dada >= s)
        {
            if (taxa_dada >= s)
            {
                taxa_dada -= s;
                qs++;
            }
        }
    

That way you would already eliminate the first if and no longer risk an infinite loop. All other conditions you can put down that while still inside the if, in the end would result in something like this:

    if (taxa_dada >= 8)
    {
        while(taxa_dada >= s)
        {
            if (taxa_dada >= s)
            {
                taxa_dada -= s;
                qs++;
            }
        }

        if (qs > 0)
            Console.WriteLine("Total de {0}  selos: {1}", qs, s);

        if (s == 5) {
            s = 3;
            qs = 0;
        }
    }
    else
    {
        Console.WriteLine("Taxa minima de 8 centavo!");
    }

As I said I didn’t quite understand what you want with your code, but I hope to have helped :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.