Home > Software engineering >  How to repeat the question until you get the correct answer C#?
How to repeat the question until you get the correct answer C#?

Time:01-27

I am a beginner in C# and I am currently writing an ATM console where customer: 1 - Check Account Balance 2 - Withdraw Money 3 - Paying In 4 - Press Q to exit I got stuck at the second option because if the user enters a higher amount than the account balance, I would like C# to repeat the question until it gets a valid reply from the user. I used do-while loop but I still haven't managed to get it right My code

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

namespace ATM
{
    internal class Program
    {
        
        static void Main(string[] args)
        {
            
            Console.WriteLine("Welcome to Barclays, please choose one of the options",
           "/n 1 - Check Account Balance",
           "/n 2 - Withdraw Money"  
           "/n 3 - Paying In"  
           "/n 4 - Press Q to exit");
            string optionChosen = Console.ReadLine(); 
            int costumerBalance = 5000;
            switch (optionChosen)
            {
                case "1":
                    Console.WriteLine("Your balance is "   costumerBalance);
                    break;
                case "2":
                    Console.WriteLine("Please enter the amount you would like withdraw: ");
                    int amountEntered = Convert.ToInt32(Console.ReadLine());

                if(amountEntered > costumerBalance)
                    do
                    {
                        Console.WriteLine("Insufficent balance please enter another amount: ");
                        amountEntered  ;
                       

                    }while(amountEntered < 5);

                else
                    {
                        Console.WriteLine("Your new balance is "   (costumerBalance - amountEntered));
                    }

                        break;

                case "3":
                    Console.WriteLine("Pleas enter the amount you would like to pay in: ");
                    int amountPaidIn = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Your new balance is "   (costumerBalance   amountPaidIn));
                    break;

                case "4":
                    Console.WriteLine("Thank you, have a nice day");
                    
                    break;
                    
                default:
                    Console.WriteLine("Please enter a valid name");
                    break;
            }

            Console.ReadKey();
        }

       
 }

}

CodePudding user response:

Here is one solution.

You need to loop across the whole case statement logic. I'm also not sure what use amountEntered is? So I have used a boolean to detect whether the amount is valid and used that as the condition in the do while loop.

case "2":
    bool validAmount = true;
    do
    {
        Console.WriteLine("Please enter the amount you would like withdraw: ");
        int amountEntered = Convert.ToInt32(Console.ReadLine());

        if(amountEntered > costumerBalance)
        {
            Console.WriteLine("Insufficent balance please enter another amount:");
            validAmount = false;
        }
        else
        {
            Console.WriteLine("Your new balance is "   (costumerBalance - amountEntered));
        }
    } while(!validAmount);
    break;
  •  Tags:  
  • Related