Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can i make the loop restart when i press enter?

Im learning C#, and i want to repeat part of my code, if person types not a digit. SO, if person types a letter, everything seems fine, and the loop repeats. But, if the person just click enter, program breaks. First loop works correct, if you click enter, it starts again, but the seond, idk why, but it recongises enter(nothing) as true. Because of this, my formula cant multiply nothing, and i get an error. So, if the bool think that nothing IS a digit, how can i fix this? ( the problem is in first case ).

using System;
using System.Text;

class Geeks
{

    // Main Method
    public static void Main()
    {
        //buy
        float kg = 1000;
        string bananas;
        float BananaKgPrice = 1.69f;
        float bananaWeight = 200;
        float WatermelonKgPrice = 3.29f;
        double price;
        string BuyChoice;

        Console.WriteLine("Welcome to our shop! We sell bananas and watermelons.");
        Console.WriteLine($"Kilo of bananas costs {BananaKgPrice} euros, kilo watermelons {WatermelonKgPrice} euros");
        bool loopBreakBuyChoice = true;
        while (loopBreakBuyChoice)
        {
            Console.WriteLine("What do you want to buy? 1 - banans, 2 - watermelons");
            BuyChoice = Console.ReadLine();
            switch (BuyChoice)
            {
                case "1":
                    loopBreakBuyChoice = false;
                    Console.WriteLine($"Price of bananas for kilo.: {BananaKgPrice} euro");
                    bool loopBreakBananaDigits = true;
                    while (loopBreakBananaDigits)
                    {
                        Console.WriteLine("How much bananas do you want to buy?");
                        bananas = Console.ReadLine();//press enter
                        bool DigitsBanana = bananas.All(char.IsDigit);
                        if (DigitsBanana != true)
                        {
                            loopBreakBananaDigits = true;
                            Console.WriteLine("Please insert AMOUNT of bananas.");
                        }

                        if (DigitsBanana == true)
                        {
                            loopBreakBananaDigits = false;
                            loopBreakBuyChoice = false;
                            price = Math.Round(Convert.ToDouble(bananas) * Convert.ToDouble(bananaWeight) / Convert.ToDouble(kg) * Convert.ToDouble(BananaKgPrice), 2);
                        }
                    }
               break;
            }

        }
    }
}

I alredy tried to fix my problem here, but i did some problems in my post, so it was closed. Re-trying to make this post, and fix my problem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you press enter without entering anything Console.ReadLine will return an empty string. Enumerable.All is implemented that it returns …

true if every element of the source sequence passes the test in the
specified predicate, or if the sequence is empty; otherwise, false.

So i think this fixes the issue:

bool DigitsBanana = !string.IsNullOrEmpty(bananas) && bananas.All(char.IsDigit);

Another approach would be to use int.TryParse because you are actually trying to parse the banana-count:

int bananaCount = 0;
while (true)
{
    Console.WriteLine("How much bananas do you want to buy?");
    if (int.TryParse(Console.ReadLine(), out int count))
    {
        bananaCount = count;
        break;
    }  

    Console.WriteLine("Please insert AMOUNT of bananas.");                 
}

price = bananaCount * Convert.ToDouble(bananaWeight) / Convert.ToDouble(kg) * Convert.ToDouble(BananaKgPrice), 2);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading