I have been working on a crypto currency market simulation console app. I have a block of code that's supposed to ask the user to enter how much of a coin they want to buy and then repeat (after user input) as long as the "maxcoins" variable has a value greater than 0. Just running this causes an infinite loop which doesn't allow the user time to input anything. I have tried using Console.ReadKey() to break the loop which works but still doesn't allow the user to type anything as the loop repeats as soon as they type a character. I've tried google but this is too specific of a question for me to find any helpful answers. Please go easy on me I'm still pretty new to programming!
My code starts here but the top few lines won't go into the code box for some reason.
int input = 0;
string inputst;
int maxcoins = 100;
while (maxcoins > 0)
{
//Tried Console.ReadKey(); here.
Console.WriteLine("Type a key number to show what you want to buy");
Console.WriteLine("Key Numbers: Monke Coin 1 | Buy None 00 |");
Console.WriteLine("You can buy " maxcoins " more coins this turn");
if (input == 1)
{
Console.WriteLine("How Much Monke Coin Do you want to buy? Enter number 0-" maxcoins);
inputst = Console.ReadLine();
input = Convert.ToInt32(inputst);
maxcoins = (maxcoins - input);
Console.WriteLine("" maxcoins);
}
}
CodePudding user response:
Your input
variable is never equal to 1, because you do not read it in the beginning of the loop.
Add the following line:
Console.WriteLine("You can buy " maxcoins " more coins this turn");
input = Convert.ToInt32(Console.ReadLine());
and it will do what you need.
CodePudding user response:
you need get input before your if