Bucle while not working.

Hello, I tried to do a reverse counter (as if it is a bullet counter for a weapon), but I think that in the line 16 I coded something wrong and I can't get it working.

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
#include <conio.h>
#include <iostream>

using namespace std;

int magazine = 31;
int shoot;

int main()
{
	cout << "Click 1 to shoot or 0 to reaload. \n" << endl;
	cout << "Magazine: 31/31 " << endl;
	
	cin >> shoot;
	
	while (shoot == 1)
	{
		magazine--;
		cout << "Magazine: " << magazine << "/31\n" << endl;
	}
	
	while (shoot == 0)
	{
		magazine = 31;
		cout << "Magazine: " << magazine << "/31\n" << endl;
	}
	
	cout <<"Remaining bullets: " << magazine << endl;
	
	getch();
	return 0;
}
Last edited on
shoot = 1 vs. shoot == 1 Know the difference! :)

One is assignment, the other tests equality.
Last edited on
Thanks for correcting that booradley60 ;)

But it still doesn't work properly. :s
Well, both of those while loops will be infinite if you enter them, since you do nothing to change the condition upon which they loop. What's the desired behavior?

Change the 'while's to 'if's and see how your program behaves.
Line 16,22: Your while loops will do nothing. The ; at the end of the line terminates the loop. Lines 17-20 and 23-26 will be executed unconditionally. Remove the ; after your while condition.

Last edited on
That worked, AbstractionAnon!!

Thank you both AbstractionAnon & booradley60 for helping. :)
Topic archived. No new replies allowed.