Certain infinite looping input numbers [closed]

1

When I paste the number 14 into 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();

    }
}

}

    
asked by anonymous 18.10.2018 / 05:25

1 answer

1

I could not quite understand what your code wants to do (do you count the number of 5-cent stamps given a fee?), but to remedy the infinite loop problem I recommend doing the following approach:

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

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

This way you would eliminate the first if and no longer risk an infinite loop. All the other conditions you can put under 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 did not quite understand what you want with your code, but I hope I have helped: D

    
18.10.2018 / 06:05