C
C#14mo ago
krfx

How can i do this:?

Hi, this is my code
using System;
using System.Text;
using System.Threading.Tasks;

namespace Workspace
{
class Program
{
static void Main(string[] args)
{
int result;
Console.WriteLine("Enter the numbers:");
int input1 = Convert.ToInt32(Console.ReadLine());
int input2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Result: {0}", input1 + input2);

}
}

}
using System;
using System.Text;
using System.Threading.Tasks;

namespace Workspace
{
class Program
{
static void Main(string[] args)
{
int result;
Console.WriteLine("Enter the numbers:");
int input1 = Convert.ToInt32(Console.ReadLine());
int input2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Result: {0}", input1 + input2);

}
}

}
9 Replies
krfx
krfxOP14mo ago
How can i listen for both of these numbers so i need to input 1 2 once not, 1 and then 2 Like in C here: int inputs = scanf("%d %d", &input1, &input2);
Thinker
Thinker14mo ago
firstly just some general advice: - use int.Parse and not Convert.ToInt32 - use string interpolation - Console.WriteLine($"Result {input1 + input2}"); If you wanted to take two inputs at once then you'd have to split the input on some kind of delimiter, for instance " " or ", ", then parse the two strings on either side Something like
string[] input = Console.ReadLine().Split(" ");
int input1 = int.Parse(input[0]);
int input2 = int.Parse(input[1]);
string[] input = Console.ReadLine().Split(" ");
int input1 = int.Parse(input[0]);
int input2 = int.Parse(input[1]);
krfx
krfxOP14mo ago
Thanks What if i want to define the operator (- or +)? im trying char operator = '+'; but it doesnt work
Pobiega
Pobiega14mo ago
? oh you want to make a variable?
krfx
krfxOP14mo ago
yes var doesnt work either
Pobiega
Pobiega14mo ago
operator is a keyword char op = '+'; works
krfx
krfxOP14mo ago
oh i see One more question please, in C for example, i need to put \n for the print /consolewrite to be on a new line I see that on C# i dont need to put that, its automaically on a new line but what if i want all on the same line
Thinker
Thinker14mo ago
Use Console.Write Console.WriteLine writes some text and then a newline, hence the name
krfx
krfxOP14mo ago
Thanks a lot
Want results from more Discord servers?
Add your server