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 to repeat the question until you get the correct answer C#?

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();
        }

       
 }

}

>Solution :

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

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;
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