Variable Question?

I have a variable A in my program that is equal to -2. I want it to be if the user types A it will subtract 2 from another variable.

Like.

6(B variable)A *6 -2 = 4* B = 4.

But I only want it to be on user input. Is this an if statement or something else?
Umm, didn't get you... be a little more specific.. Please explain again what you want... Maybe like a sample output of your program...
1
2
3
int var1;
signed int A = -2
cin >> var1; 


If the user inputs the letter A along with their input of var1 it will automatically subtract 2 from var1.
So basically, suppose I entered 4A, the result would be 2? If that's what you want, this would involve strings...
You are correct. Really? Even though the variables in question are integers? How so?
Because you want them to put in character and numeral input in one go, you are probably best using a string. You will need to test if the string contains A and then extract the numbert, ignoring any invalid inputs.

http://www.cplusplus.com/reference/string/string/

You could otherwise get them to do two inputs and avoid strings:

1
2
3
4
5
6
7
8
int var1;
char c = 'x';
cin >> var1; 
cin >> c; 
if(c == 'A')
{
    var1 = var1 - 2;
}


Up to you.
Last edited on
Topic archived. No new replies allowed.