简体   繁体   中英

I'm having difficulty with my do while loop. It only executes once

The battleship program I wrote is supposed to loop a infinite amount of times until a condition is met. However it stops after the first execution. What is the matter with my loop? The code runs, however towards the end it outputs game over twice when its not supposed to. There are still more ships remaining in the game.

#include<iostream>
#include<fstream>
#include<string>
#include<cmath>

using namespace std;
bool FleetSunk();
void Fire(int&, int&, char&);
char ocean[25][25];

int main()
{
    int x;
    int y;      
    char spot;
    ifstream input;
    input.open("ocean.txt");

        for(x=0;x<25;x++)
        {
            for(y=0;y<25;y++)
            {
                input >> ocean[x][y];

            }
        }

 FleetSunk == false;
    do
    {
        cout << "Please input x coordinate: ";
        cin >> x;
        cout << endl << "Please input y coordinate: ";
        cin >> y;
        cout<< endl;
        Fire(x,y,spot);
        FleetSunk();

    }while(FleetSunk() == false);
}

void Fire(int& x, int&y, char&spot)
{
    spot = ocean[x][y];
    if(spot == '#')
    {
        cout << "You Have hit a ship"<< endl;
        ocean[x][y] = 'H';
    }
    else if(spot == 'H')
    {
        cout << "HIT AGAIN" << endl;
    }
    else if(spot == '-')
    {
        cout << "MISS" << endl;
    }
}

bool FleetSunk()
{
    int m;
    int n;
    for(m=0;m < 25; m++)
    {
        for(n=0;n<25;n++)
        { 

            if(ocean[m][n] == '#')
            {
                cout << "You still have ships remaining" << endl;
            }
            else
            {
                cout<< "You have hit all the ships. GAME OVER!" << endl;
                return true;
            }
        }
    }
}

Your FleetSunk function has two problems. One should have been a compiler warning, and one is a logical problem.

Firstly, not all paths in the function return a value... Technically. Although in your case that's not quite true because of the logic problem. Let me be more specific: If your entire board is populated with '#' , and no shots are taken, then the function behaviour is undefined. It completes the loop and then does not return a value.

So now let's do the logic. You cannot know whether there are any un-hit ship locations until you have examined the entire board. That means you cannot exit from your inner loop in the way you are doing. How about instead you return false if you encounter an "alive" position, and return true at the end of the function (which is only reached if you never encounter an "alive" position).

bool FleetSunk()
{
    for( int m = 0; m < 25; m++ )
    {
        for( int n = 0; n < 25; n++ )
        { 
            if( ocean[m][n] == '#' ) return false;
        }
    }
    return true;
}

See in the comments under your question for other suggestions related to how you are calling FleetSunk in your loop. I also recommend (as evident in my code example) that you don't write stuff to cout in a function that is testing for some condition. It's the responsibility of the caller to do that, not the function itself.

Finally, I would just like to say that the line FleetSunk == false; above your loop really does not do what you might think. That will take the function pointer, convert it to boolean and compare it with false . It's a crazy thing to do, and also useless because the resulting value isn't used for anything. Just delete that line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM