❔ ✅ How to store a method's variable to main method?
I'm creating a simple ATM program, but I've encountered an issue:
I have created a method called "withdraw", the purpose of this method is to subtract a "WithdrawAmount" entered via the parameter to the users current balance.
I've managed to correctly have it subtract the balance, but I do not know how to store that result as a variable and bring it back to my main method?
the aim is to update the balance in my main method to the new balance after a withdraw has been made
I've attempted to use a
return
statement, but no luck.2 Replies
public class Program // simple atm system
{
public static void Main()
{
// setting account variables, a balance, pin, and usernmae
int balance = 500;
int pin = 1234;
string user = "Jack";
// setting account attempts
int Attempts = 1;
int AttemptsLimit = 3;
bool auth = false;
// while attempts are less than the attempt limit and auth is false loop:
while (Attempts < AttemptsLimit && auth == false)
{
//input pin
Console.WriteLine("Enter pin: ");
int AttemptPin = Convert.ToInt32(Console.ReadLine());
// if attempted pin is the correct pin
if (AttemptPin == pin)
{
//execute the method
Console.WriteLine("Welcome, " + user);
auth = true;
withdraw(balance, 200);
//Console.WriteLine(balance);
}
// if attempted pin is not equal to pin
else if (AttemptPin != pin)
{
Console.WriteLine("Error \n");
Console.WriteLine("You have made "+Attempts+" attempt");
// add onto attempts made
Attempts++;
}
else
{
Console.WriteLine("Error: \n you have run out of attempts");
}
}
static int withdraw(int TheBalance, int WithdrawAmount)
{
//TheBalance = TheBalance - WithdrawAmount;
//Console.WriteLine(TheBalance);
return TheBalance = TheBalance - WithdrawAmount;
}
// keep the CLI running
Console.ReadLine();
}
}
public class Program // simple atm system
{
public static void Main()
{
// setting account variables, a balance, pin, and usernmae
int balance = 500;
int pin = 1234;
string user = "Jack";
// setting account attempts
int Attempts = 1;
int AttemptsLimit = 3;
bool auth = false;
// while attempts are less than the attempt limit and auth is false loop:
while (Attempts < AttemptsLimit && auth == false)
{
//input pin
Console.WriteLine("Enter pin: ");
int AttemptPin = Convert.ToInt32(Console.ReadLine());
// if attempted pin is the correct pin
if (AttemptPin == pin)
{
//execute the method
Console.WriteLine("Welcome, " + user);
auth = true;
withdraw(balance, 200);
//Console.WriteLine(balance);
}
// if attempted pin is not equal to pin
else if (AttemptPin != pin)
{
Console.WriteLine("Error \n");
Console.WriteLine("You have made "+Attempts+" attempt");
// add onto attempts made
Attempts++;
}
else
{
Console.WriteLine("Error: \n you have run out of attempts");
}
}
static int withdraw(int TheBalance, int WithdrawAmount)
{
//TheBalance = TheBalance - WithdrawAmount;
//Console.WriteLine(TheBalance);
return TheBalance = TheBalance - WithdrawAmount;
}
// keep the CLI running
Console.ReadLine();
}
}
string
instead of an int
fixed that too
got it to work now
I guess I'll post my updated code here and close
public class Program // simple atm system
{
public static void Main()
{
// setting account variables, a balance, pin, and usernmae
int balance = 500;
int pin = 1234;
string user = "Jack";
// setting account attempts
int Attempts = 1;
int AttemptsLimit = 3;
bool auth = false;
// while attempts are less than the attempt limit and auth is false loop:
while (Attempts < AttemptsLimit && auth == false)
{
//input pin
Console.WriteLine("Enter pin: ");
int AttemptPin = Convert.ToInt32(Console.ReadLine());
// if attempted pin is the correct pin
if (AttemptPin == pin)
{
//execute the method
Console.WriteLine("Welcome, " + user);
auth = true;
int result = withdraw(balance, 200);
Console.WriteLine("test "+result);
}
// if attempted pin is not equal to pin
else if (AttemptPin != pin)
{
Console.WriteLine("Error \n");
Console.WriteLine("You have made "+Attempts+" attempt");
// add onto attempts made
Attempts++;
}
else
{
Console.WriteLine("Error: \n you have run out of attempts");
}
}
// keep the CLI running
Console.ReadLine();
}
static int withdraw(int TheBalance, int WithdrawAmount)
{
TheBalance = TheBalance - WithdrawAmount;
Console.WriteLine(TheBalance);
return TheBalance;
}
}
public class Program // simple atm system
{
public static void Main()
{
// setting account variables, a balance, pin, and usernmae
int balance = 500;
int pin = 1234;
string user = "Jack";
// setting account attempts
int Attempts = 1;
int AttemptsLimit = 3;
bool auth = false;
// while attempts are less than the attempt limit and auth is false loop:
while (Attempts < AttemptsLimit && auth == false)
{
//input pin
Console.WriteLine("Enter pin: ");
int AttemptPin = Convert.ToInt32(Console.ReadLine());
// if attempted pin is the correct pin
if (AttemptPin == pin)
{
//execute the method
Console.WriteLine("Welcome, " + user);
auth = true;
int result = withdraw(balance, 200);
Console.WriteLine("test "+result);
}
// if attempted pin is not equal to pin
else if (AttemptPin != pin)
{
Console.WriteLine("Error \n");
Console.WriteLine("You have made "+Attempts+" attempt");
// add onto attempts made
Attempts++;
}
else
{
Console.WriteLine("Error: \n you have run out of attempts");
}
}
// keep the CLI running
Console.ReadLine();
}
static int withdraw(int TheBalance, int WithdrawAmount)
{
TheBalance = TheBalance - WithdrawAmount;
Console.WriteLine(TheBalance);
return TheBalance;
}
}
Closed!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.