how to make the code not break when the enter key is pressed?

максим гурко

So, im learing C# right now, and i was making my console program, which sells you some fruits. with the help of loop, I made it so that if a person enters a non-digit, then the loop starts over. But it turns out, if i type nothing and just press enter, my code gets an error. What should I do in such a situation? How can I change the code?

// 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 - bananas, 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 +digit
                    ConsoleKeyInfo key;
                    key = Console.ReadKey(true);
                    if (key.Key == ConsoleKey.Enter)
                    {

                    }
                    else
                    {
                        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;
         }         
    }
}
Selaka Nanayakkara

I pasted your code into mine, but it only works if enter is pressed twice. If you press it 1 time, and then any letter / number, the program will break

   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
                    ConsoleKeyInfo key;
                    key = Console.ReadKey(true);
                    if (key.Key == ConsoleKey.Enter)
                    {

                    }
                    else
                    {
                        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;
         }         
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make a function do something when the enter key is pressed

How to trigger code if Enter key is pressed in a column

How to show alert when enter key pressed

Perl Code required to break loop when key pressed

Trying to make send a message when the enter key is pressed

Execute code in objective-c when enter key is pressed?

How to do something when Enter-key is pressed on NSTextField

How can I fire the Tab keypress when the Enter key is pressed?

How to change focus to another component when Enter key is pressed

How can I know when the Enter key was pressed on QTextEdit

How to move to next input field when enter key is pressed?

How to have a function called when Enter key is pressed in a guizero application?

How to disable the <KeyRelease> event only when the enter key is pressed

how to make asp:LinkButton work in safari when enter pressed

Why this code is not printing Hi when the Enter key is pressed after stopping the code using F1 key?

How to break a loop when "enter" is pressed in C++ while taking input in an integer array

Submit a form only when 'enter' key is pressed

Invoke a serie of actions when Enter key is pressed

Form not submitting when enter key is pressed

jQuery: content to show when enter key is pressed

Call a javascript function when enter key is pressed

Make the Enter key to not create a line break

Pygame: How to make sprites face the same direction when no key is pressed

How do I make a timer stop when a key is pressed in python?

How to make Python execute a command when a key is detected to be pressed

Xcode 9, Selected code snippet type removed when enter key is pressed

code for prevent New Line in Textarea when ENTER key is pressed doesn't work

How to write "enter key pressed" to a stream?

How to check if user pressed Enter key ?