I want to write code that calculate sum of 2 number. My main problem I can not enter two numbers in a row. When I enter first number program duplicate it and stop working. For my plan programm should work until second number and then stop. Here is my code:
namespace test;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter numbers to calculate or tap ENTER to exit");
string enter = Console.ReadLine();
while(true){
int x = Convert.ToInt32(enter);
int y = Convert.ToInt32(enter);
if(x > 0 && y > 0){
int z = x y;
Console.WriteLine(z);
break;
}else if(enter == ""){
break;
}else{
Console.WriteLine("Invalid option");
break;
}
}
}
}
I tried writing x outside "when", but did not work. I also tried to run this code without "If" but I can't. I just want to enter 2 number in a row.
CodePudding user response:
This because you only use Console.ReadLine()
once and then you use that value both for the x and y.
Instead you should do something like that:
namespace test;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter numbers to calculate or tap ENTER to exit");
string enter;
while(true){
enter = Console.ReadLine();
int x = Convert.ToInt32(enter);
enter = Console.ReadLine();
int y = Convert.ToInt32(enter);
if(x > 0 && y > 0){
int z = x y;
Console.WriteLine(z);
break;
}else if(enter == ""){
break;
}else{
Console.WriteLine("Invalid option");
break;
}
}
}
}
CodePudding user response:
try this:
Console.WriteLine("Enter numbers to calculate or tap ENTER to exit");
string enter1 = Console.ReadLine();
string enter2 = Console.ReadLine();
while (true)
{
int x = Convert.ToInt32(enter1);
int y = Convert.ToInt32(enter2);
if (x > 0 && y > 0)
{
int z = x y;
Console.WriteLine(z);
break;
}
else if (enter1 == "" || enter2 == "")
{
break;
}
else
{
Console.WriteLine("Invalid option");
break;
}
}
CodePudding user response:
You can try the below, as Console.ReadLine()
will give a string.
int x = Convert.ToInt32(enter.Split()[0]);
int y = Convert.ToInt32(enter.Split()[1]);
CodePudding user response:
There are a few problems with your code.
You don't need a while loop in this case. because you just need to take two inputs from the user, and then calculate the sum and exit.
You are using the same enter
variable for your calculation. instead, you should read two times and store two variables.
As a suggestion, you can use int.TryParse to convert the string to int. it provides you flexibility if the given string is not convertible to int. you can check its return value.
CodePudding user response:
Here is how you can loop until a positive number is entered for each of the two numbers.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Sum of two numbers.");
int x = 0;
int y = 0;
while (!AskForNumber("Enter the first number:", out x) || x < 0)
{
Console.WriteLine("Invalid input.");
}
while (!AskForNumber("Enter second number:", out y) || y < 0)
{
Console.WriteLine("Invalid input.");
}
int sum = x y;
Console.WriteLine($"{x} {y} = {sum}");
}
static bool AskForNumber(string prompt, out int number)
{
Console.WriteLine(prompt);
string input = Console.ReadLine();
return int.TryParse(input, out number);
}
}
Example output
Sum of two numbers. Enter the first number: Invalid input. Enter the first number: ALSDJD Invalid input. Enter the first number: 7 Enter second number: -3 Invalid input. Enter second number: 3 7 3 = 10
The function AskForNumber()
prompts the user for a number and returns true if the input can be parsed into a number, or false otherwise. The actual numeric value is assigned to an out
parameter named number
.
The main function, has two loops, one for each input. Each loop not only checks if the input is valid, but also the inputs are non-negative (the or x<0
) condition. Once a valid number is set the program moves on.
PS.
In your question, the main while(true)
loop serves no purpose because all the branches of the if
statement inside result in a break;
which exists in the loop. You should make one a continue;
in order for the loop to loop back and try again.