Reputation: 1
I want to add two strings together like a calculator, not next to each other. This is my code:
void MainAdd()
{
Console.WriteLine("");
string Add1 = Console.ReadLine();
Console.WriteLine("");
string Add2 = Console.ReadLine();
string add = Add1 + Add2;
Console.WriteLine(add);
startcode();
}
How can I make it work like a calculator?
Upvotes: 0
Views: 424
Reputation: 81493
Please read about built in types, you can't do numerical operations on string
(text):
int Add1 = int.Parse(Console.ReadLine());
int Add2 = int.Parse(Console.ReadLine());
int add = Add1 + Add2;
Console.WriteLine(add);
Also, this was just an example, you should never trust the user to enter the right thing, Use TryParse
.
Upvotes: 0
Reputation: 14231
static void Main(string[] args)
{
double add1 = InputValue("Input first addend");
double add2 = InputValue("Input second addend");
double sum = add1 + add2;
Console.WriteLine("Sum: " + sum);
}
static double InputValue(string message)
{
double value;
string input;
do
{
Console.WriteLine(message);
input = Console.ReadLine();
}
while (!double.TryParse(input, out value));
return value;
}
Upvotes: 1
Reputation: 52
int add = Int32.Parse(Add1) + Int32.Parse(Add2);
Console.WriteLine(add);
Upvotes: 0