0
What does it mean and how can you fix it.
Error: Operator '-' cannot be applied to operands of type 'string' and 'string'
13 Antworten
+ 8
int age1 = 18;
int age2 = 15;
int output = age1 - age2;
Console.WriteLine(output);
//gives 3
+ 1
I wanted to subtract someone else's age from mine
+ 1
Make age type int.
int age;
Then you can substract the value of age and also prevent the age to be "very old"
(Please notic your title is not very descriptive and you have a good question. Make you title like "Why do I get error : you cannot use the subtraction operator on two string operands").
+ 1
K thnx
I'm just a beginner it's gonna take a while to get used to this.
+ 1
Ash, you need to use integer values and not string values. Therefore, you would do something like this.
int myAge = 18;
int friendsAge = 17;
int ageDifference;
ageDifference = myAge - friendsAge;
or
ageDifference = 18 - 17;
If you're using number literals, then you need to remove the quotes from around them.
+ 1
Your are welcome. It is a good question. Keep asking.
0
What do you want to do ?
0
I want to subtract 2 string values
0
Ash, you cannot use the subtraction operator on two string operands. You can concatenate two strings together using the '+' operator, but that's it.
0
Is it a string or a value
like ""cat"
or a value "2"
C# won't recognise "2" as a value
"1" + "2" = 12
"cat" + "and" + "dog" = cat and dog
"1" - "2" = //makes no sense
0
I want the other person to type in their age
so i used
string YourAge;
Console.WriteLine("How old are you");
YourAge = Console.ReadLine();
now I wanted to use that age to subtract from mine
0
int YourAge;
Console.WriteLine("How old are you");
YourAge = Convert.ToInt32(Console.ReadLine()); .
0
thnx a lot