0

What does it mean and how can you fix it.

Error: Operator '-' cannot be applied to operands of type 'string' and 'string'

14th Aug 2017, 8:29 PM
Ash
13 Answers
+ 8
int age1 = 18; int age2 = 15; int output = age1 - age2; Console.WriteLine(output); //gives 3
14th Aug 2017, 8:51 PM
Nomeh Uchenna Gabriel
Nomeh Uchenna Gabriel - avatar
+ 1
I wanted to subtract someone else's age from mine
14th Aug 2017, 8:43 PM
Ash
+ 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").
14th Aug 2017, 8:48 PM
sneeze
sneeze - avatar
+ 1
K thnx I'm just a beginner it's gonna take a while to get used to this.
14th Aug 2017, 8:49 PM
Ash
+ 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.
14th Aug 2017, 8:50 PM
Benjamin McKissick Jr.
Benjamin McKissick Jr.   - avatar
+ 1
Your are welcome. It is a good question. Keep asking.
14th Aug 2017, 8:53 PM
sneeze
sneeze - avatar
0
What do you want to do ?
14th Aug 2017, 8:31 PM
sneeze
sneeze - avatar
0
I want to subtract 2 string values
14th Aug 2017, 8:33 PM
Ash
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.
14th Aug 2017, 8:40 PM
Benjamin McKissick Jr.
Benjamin McKissick Jr.   - avatar
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
14th Aug 2017, 8:41 PM
sneeze
sneeze - avatar
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
14th Aug 2017, 8:55 PM
Ash
0
int YourAge; Console.WriteLine("How old are you"); YourAge = Convert.ToInt32(Console.ReadLine()); .
14th Aug 2017, 9:16 PM
sneeze
sneeze - avatar
0
thnx a lot
14th Aug 2017, 9:19 PM
Ash