C
C#10mo 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
krfx10mo 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
Thinker10mo 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
krfx10mo ago
Thanks What if i want to define the operator (- or +)? im trying char operator = '+'; but it doesnt work
Pobiega
Pobiega10mo ago
? oh you want to make a variable?
krfx
krfx10mo ago
yes var doesnt work either
Pobiega
Pobiega10mo ago
operator is a keyword char op = '+'; works
krfx
krfx10mo 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
Thinker10mo ago
Use Console.Write Console.WriteLine writes some text and then a newline, hence the name
krfx
krfx10mo ago
Thanks a lot
Want results from more Discord servers?
Add your server
More Posts
❔ WHY is my app not running?https://github.com/EmelieH-cloud/WPF-timer This is driving me nuts. I have created a grid with a tim✅ Help with authentication/authorization for my application with .net 7Hi, I was hoping someone could help me out with Claims/Identity/Roles. I have an application with ❔ WPF Binding to collection in static instance not updating listI have a collection in a static member of a class that's being changed. It should be firing `Proper✅ How to sum datetimeoffset and timespan?I have following structure: ```cs [JsonProperty( PropertyName = "timestamp" )] [JsonRequired] ✅ Code Hints Not Working??? Visual Studio 2022Has anyone else ran into this problem of the drop down box that shows what you can select code-wise ❔ DataGrid hyperlink cells without a Navigation WindowMy dialog isn't a Navigation Window. But it does have two `DataGrid` instances each of which has `D❔ Which library is best for mouse hooks in .net 6/7/8? MouseKeyHook/WindowsHook don't work.I tried them both in a console app and got this issue https://github.com/topstarai/WindowsHook/issue✅ Help LeetCode Problemusing System; using System.Collections.Generic; using System.Collections.Immutable; using System.Com❔ NSwag and API helpI'm learning how to build an APIs and connect to it from a Blazor front-end. I'm using NSwag to buil❔ Calling an Azure AD protected Web API endpoint from an Azure AD protected Web App [.NET 7]I am having trouble getting the correct flow/configuration to call my Web API Endpoint that is prote