Question
Question
CC#
Created by Question on 4/8/2023 in #help
❔ ✅ How to store a method's variable to main method?
!close
12 replies
CC#
Created by Question on 4/8/2023 in #help
❔ ✅ How to store a method's variable to main method?
this should work
12 replies
CC#
Created by Question on 4/8/2023 in #help
❔ ✅ How to store a method's variable to main method?
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;
}

}
12 replies
CC#
Created by Question on 4/8/2023 in #help
❔ ✅ How to store a method's variable to main method?
I guess I'll post my updated code here and close
12 replies
CC#
Created by Question on 4/8/2023 in #help
❔ ✅ How to store a method's variable to main method?
got it to work now
12 replies
CC#
Created by Question on 4/8/2023 in #help
❔ ✅ How to store a method's variable to main method?
fixed that too
12 replies
CC#
Created by Question on 4/8/2023 in #help
❔ ✅ How to store a method's variable to main method?
also I used a string instead of an int
12 replies
CC#
Created by Question on 4/8/2023 in #help
❔ ✅ How to store a method's variable to main method?
my bad, I put the method inside the main method
12 replies
CC#
Created by Question on 4/8/2023 in #help
❔ ✅ How to store a method's variable to main method?
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();
}
}
12 replies