"While" loop generating same result every time

Afternoon all,

I have a while loop in my program. It's a simplified Battleships game, and this is the part where you select a space to bomb. The computers' numbers are randomly generated earlier on.
My problem is, that whatever number is entered, the console returns a "HIT!". I think my problem lies within the initial "IF" statement, but I'm not completely sure why.
Any help would be greatly appreciated :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while (shipCount > 0)
{
cout << "Select a number between 26 and 50 to bomb" << endl;
cin >> playerPick;

if (playerPick == compAirCarr || compBatt || compDest || compSub || compPatrol)
{
               cout << "HIT!" << endl;
               cout << "Please enter another number to bomb" << endl;
               shipCount--;
}

else           cout << "MISS!" << endl;
               cout << "Computer's turn" << endl;
}

closed account (zb0S216C)
I think you need playerPick == for every comparison in your if statement. For example:
1
2
3
4
if (playerPick == compAirCarr || playerPick == compBatt || ... )
{
    //...
}
You need to make the if statement like this:

1
2
if (playerPick == compAirCarr || playerPick == compBatt || playerPick == compDest ||
    playerPick == compSub || playerPick == compPatrol)


[edit]: Too late! :|
Last edited on
Thank you both very much :) I tried everything but that :P
Topic archived. No new replies allowed.