+ 3

C++ signs as variable?

Is there a way to make a variable with the value of a sign like + or - ?

26th Nov 2019, 7:55 PM
Christian0972
8 Antworten
+ 5
char a = '+'; if(a == '+') cout << a << endl;
26th Nov 2019, 8:27 PM
rodwynnejones
rodwynnejones - avatar
+ 5
Yes, but how can we compare the value when we can't even create a variable named + or - ? int + = 5; // doesn't work int a = 5; // works if(a == +) // + is not variable Tell me if I misunderstood you.
26th Nov 2019, 8:25 PM
Ipang
+ 4
Yes char variables are what you need.
26th Nov 2019, 8:55 PM
Sonic
Sonic - avatar
+ 3
Valid variable name should begin with letter or underscore, followed by mixture of letters (uppercase or lowercase), underscore or number (in no distinct sequence). Symbolic character such as + or - are not qualified to form a variable name, nor to be part of a variable name. This is as I understand it.
26th Nov 2019, 8:13 PM
Ipang
+ 3
Ipang I mean as the value for the variable like how you would say int a = 2 or float b = 4. I’m wondering if there is something i could do to make the value a + or -
26th Nov 2019, 8:19 PM
Christian0972
+ 3
Ipang It turns out I had to be using char
26th Nov 2019, 8:36 PM
Christian0972
+ 2
Just in case someone doesn’t understand it would be something like “if (variable name == +) {cout << variable name}
26th Nov 2019, 7:58 PM
Christian0972
+ 2
Yeah, now it's obvious : ) Well good to know you found what you're looking for 😁👍
26th Nov 2019, 8:40 PM
Ipang