Need help!

May 30, 2013 at 8:33pm
I'm doing this project for class and it requires that the variable never hits 1. The variable also either goes up by 1, or down by 1 every time you do something.
So for example it can go :
-2 -1 0 2 3 4 . . .

I have no idea how to make that work. Please help!
Also I'mm using C++.
May 30, 2013 at 8:38pm
What does "you do something" mean? And what does "the variable either goes up or down" mean?
May 30, 2013 at 8:41pm
If someone chose option "A" the variable will go up 1.
If someone chose option "B" the variable will go down 1.
It depends on what the user chooses.
May 30, 2013 at 8:43pm
And what is the problem?
May 30, 2013 at 8:47pm
The variable can never be the number 1. Never. So if the variable is 0 at the moment, and I chose option "A" it has to go to 2, if I do it again it goes to 3. Now, if I chose option "B" it has to go to 2, again, it has to go back 0.
May 30, 2013 at 8:48pm
So what?
Last edited on May 30, 2013 at 8:50pm
May 30, 2013 at 8:55pm
How do I have the variable never touch 1. That's my problem.
May 30, 2013 at 8:56pm
Have you invested into a switch?
Sounds to me like
initialize variable
loop
-input
-change variable
--if variable is equal to 1 change again
-restart loop
quit program
May 30, 2013 at 9:01pm
I have not, and this sounds like my solution. Thank you!
May 30, 2013 at 9:11pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int theVariable = 0;
char c;


do
{
   std::cout << "Variable is equal to " << theVariable << std::endl << std::endl;

   std::cout << "Enter A to increase the variable, B to decrease it or Q to quit: ";

   std::cin > c;


   switch ( toupper( c ) )
   {
      case 'A':
         theVariable += 1 + !theVariable;
         break;

      case 'B':
         theVariable -= 1 + ( theVariable == 2 );
         break;
   }
} while ( c != 'Q' );
Last edited on May 30, 2013 at 9:13pm
Topic archived. No new replies allowed.