decrement

I need to decrement a number by 2.

my code goes something like this:

"
int won=18;

.............


(won-=2);
gotoxy(60,18);
cout<<"Points: "<<won;

"

an I am repeating this until the number is equal to 0. But when the number is supposed to be equal to 9 the answer given is 90. when it suppose to be 8 it is equal to 80.... and so on .



Please can you help tnks


You're just printing the single-digit numbers at the wrong position, that's all.
You'll have to clarify what you want here, that's a bit confusing.

So, you're starting at 18 and you want to number to deduct two from itself until it reaches 0?

1
2
3
4
5
while (x > 0)
{
   x -= 2;
   cout << "Current value of x: " << x << endl;
}
thnaks for your reply but I didn't understood you , can you please explain more.
What do you get when you write 10 with a pencil, then erase the first digit and write 9 there?
I'm making a game and i am reducing the points every time the user takes a mistake,2 points are reduced each time
1
2
3
4
if (/*whatever the mistake criteria is*/)
{
   points -= 2;
}

Last edited on
but why when it suppose to be equal to 8 and i display the points it is displayed 80 ,when it suppose to be equal to 6 it is displayed 60 .....
How about answering my question? (and by extension, your own question)
Yes I understand what you are trying to say but I don't have an idea how I am going to implement it. can you help me please, by giving me an example ?
Just print spaces to overwrite whatever was there before.
There are several ways to do this and one of them is

1
2
3
#include <iomanip>
[...]
cout << "Points: " << setfill(' ') << setw(2) << won;
Last edited on
It looks like something is going on that you do not have posted. Put a break point at this line in your code...

(won-=2);
After that line executes put your cursor over 'won' and see what is actually stored there, is it a 9 or a 90? (or whatever permutation you are looking for). Take it one step at a time.

Also just by your explanation and the code you give as an example, won could never be 9, if you initialize with 18 and subtract 2 each pass. Unless like I said you are giving arbitrary numbers for your example.
Last edited on
Thanks a lot for helping !!!!
Topic archived. No new replies allowed.