C
C#2d ago
Moha

Bank program help

using System;

class Bank
{
static void Main()
{
Console.WriteLine("Welcome to the bank. Please select one of the given options.");
Console.WriteLine("1. Withdraw money");
Console.WriteLine("2. Deposit money");
Console.WriteLine("3. Check balance");
Console.WriteLine("4. Send money");

int balance = 0;
int option = int.Parse(Console.ReadLine());

while (option >= 5)
{
Console.WriteLine("Please choose a valid option.");
option = int.Parse(Console.ReadLine());
}

if (option == 1)
{
Console.WriteLine("Please write the amount of money you are gonna withdraw.");
int amount = int.Parse(Console.ReadLine());
Console.WriteLine("Withdrawing " + amount + "...");

balance -= amount;

Console.WriteLine("Your new balance is " + balance + ".");
}

if (option == 2)
{
Console.WriteLine("Please write the amount of money you are gonna deposit.");
int amount = int.Parse(Console.ReadLine());
Console.WriteLine("Depositing " + amount + "...");

balance += amount;

Console.WriteLine("Your new balance is " + balance + ".");
}




}
}
using System;

class Bank
{
static void Main()
{
Console.WriteLine("Welcome to the bank. Please select one of the given options.");
Console.WriteLine("1. Withdraw money");
Console.WriteLine("2. Deposit money");
Console.WriteLine("3. Check balance");
Console.WriteLine("4. Send money");

int balance = 0;
int option = int.Parse(Console.ReadLine());

while (option >= 5)
{
Console.WriteLine("Please choose a valid option.");
option = int.Parse(Console.ReadLine());
}

if (option == 1)
{
Console.WriteLine("Please write the amount of money you are gonna withdraw.");
int amount = int.Parse(Console.ReadLine());
Console.WriteLine("Withdrawing " + amount + "...");

balance -= amount;

Console.WriteLine("Your new balance is " + balance + ".");
}

if (option == 2)
{
Console.WriteLine("Please write the amount of money you are gonna deposit.");
int amount = int.Parse(Console.ReadLine());
Console.WriteLine("Depositing " + amount + "...");

balance += amount;

Console.WriteLine("Your new balance is " + balance + ".");
}




}
}
Now here what I'm asking is if I want to do an "Would you like to do any other missions?" Would I put it inside the if or outside the if?
19 Replies
Angius
Angius2d ago
You mean, if you wanted to go back to the start if the player wants? If so, then you want a loop
Moha
MohaOP2d ago
Yeah but would I put it inside the if or outside And I would use a while loop right?
Angius
Angius2d ago
Yes, a while loop. Or a do..while, so no need for any ifs
string selection;
do {
Console.WriteLine("Type `quit` to quit");
Console.WriteLine("Press enter to continue");
selection = Console.ReadLine();
// all the other code
} while (selection != "quit");
string selection;
do {
Console.WriteLine("Type `quit` to quit");
Console.WriteLine("Press enter to continue");
selection = Console.ReadLine();
// all the other code
} while (selection != "quit");
something like this should work It will loop until the user types quit when prompted
Moha
MohaOP2d ago
Can you show me the code with a normal while loop not a do while, also what does selection mean
Angius
Angius2d ago
What do you mean "what does selection mean"? It's just the name of the variable
Moha
MohaOP2d ago
Ohhh I DIDNT SEE IT
Angius
Angius2d ago
You can do it with a while loop too, sure
while (true)
{
Console.WriteLine("Type `quit` to quit");
Console.WriteLine("Press enter to continue");
selection = Console.ReadLine();
if (selection == "quit")
{
break;
}
// all the other code
}
while (true)
{
Console.WriteLine("Type `quit` to quit");
Console.WriteLine("Press enter to continue");
selection = Console.ReadLine();
if (selection == "quit")
{
break;
}
// all the other code
}
would be one way to do so
Moha
MohaOP2d ago
It helped thanks @Angius is there any way I can save progress after I run the program? For example if I deposit 400 and the program ends and I run it again my balance will be 400 is it possible?
Angius
Angius2d ago
Of course, you can just save it to a file
Moha
MohaOP2d ago
I don't wanna load it to a file just straight out save, something that I can write to the code?
Angius
Angius2d ago
Where do you want to save it if not in a file?
Moha
MohaOP2d ago
Like maybe a constant? Something similiar
Angius
Angius2d ago
Every single value in your code gets reset to the default value when the application is closed and ran again
Moha
MohaOP2d ago
So no way I can do that
Angius
Angius2d ago
You can persist data in a file, in a database (that's also a file)
Moha
MohaOP2d ago
Dang okay yeah thanks anyways Mb for taking your time
Angius
Angius2d ago
No problem
MODiX
MODiX6h ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?