for loop is a bit off

The for loop seems a bit off. The user has 20 turns to find treasure
when I enter the first direction it says I have 20 turns remaining shouldn't that be 19? It countdowns fine it just starts at 20 when it should read 19 anyone see where I went wrong? any suggestions will be appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// for loop to count down number of turns
	for (count = 20; count >= 0; --count)
	{   // prompting for a direction to move.
		cout << "\n Please enter the direction which you want to move:\n";
		cin >> direction;
		cout << " You have " << count << " turns remaining:\n";


HERE IS THE OUTPUT 

 Hello a Welcome to TREASURE HUNT!!
 You have 20 turns to find the treasure.
 You're starting at location is (2,2,)
 n is for North, s is for South, e is for East and w is for West)

=====================================================================

 Please enter the direction which you want to move:
n
 You have 20 turns remaining:

a moved North
 Your current location is: (2,3)

Please enter the direction which you want to move:







   
Your for loop is iterating 21 times, because you're going from 20 to 0, inclusive.

You are starting the count at count = 20, and then printing count within that iteration. So it prints 20 the first time, at the end of the turn. If you want it to start at 19, then do count = 19.
for (count = 19; count >= 0; --count)

Or, make it print the "you have X turns remaining" before the actual turn is made, but then do count > 0 in the for loop instead of count >= 0.
for (count = 20; count > 0; --count)
Last edited on
thanks where would I put it before the turn is made I tried it and it just ran through all the turns at once

LIKE THIS ?
// for loop to count down number of turns
{ // prompting for a direction to move.

cout << " Please enter the direction which you want to move:\n";
cin >> direction;
for (count = 20; count >= 0; --count)
{
cout << " You have " << count << " turns remaining:\n";
}
Something like...

1
2
3
4
5
6
7
8
for (int count = 20; count > 0; count--)
{
    cout << " You have " << count << " turns remaining:\n";
    cout << " Please enter the direction which you want to move:\n";
    cin >> direction;
  
    // ...
}
Last edited on
Great Thanks Ganado that's what I wanted it to do appreciate it
Topic archived. No new replies allowed.