Jan 5, 2012 at 7:13am UTC
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?
Jan 5, 2012 at 7:37am UTC
Umm, didn't get you... be a little more specific.. Please explain again what you want... Maybe like a sample output of your program...
Jan 5, 2012 at 8:01am UTC
So basically, suppose I entered 4A, the result would be 2? If that's what you want, this would involve strings...
Jan 5, 2012 at 8:17am UTC
You are correct. Really? Even though the variables in question are integers? How so?
Jan 5, 2012 at 12:54pm UTC
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 Jan 5, 2012 at 12:54pm UTC