Hey guys, im currently doing my frigate project for my semester class intro to c++ and im stuck at a part where my program crashes after the ships move for a while, any clues? Also, im stuck in the overall development of the game, im not sure how to get the shooting done from ship to ship. If you can se, the loop i have in the gamestart() function is that it ends when either the players ship armor or the cpu's ship armor reached -5, the problem is that i don't know how to get shooting done. Any suggestions or clues as to how to start with that?

Heres my code:

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>


using namespace std;

    struct ships
    {
           int agility; //the amount of moves it has each turn
           int armor;
           int weapons;
           int range;
    };

void printBoard (char[][11]);
void initGrid(char grid[][11], int, int, int, int);
void movement (char gameboard[][11], int&, int&, int&, int&, ships&);
void assignAttributes (ships & p1, ships & cpu);
void cpumovement (char gameboard[][11], int&, int&, int&, int&, ships&);
bool gameOver (ships p1, ships cpu);

int main(int argc, char *argv[])
{
    
    char choice;
    ships p1;
    ships cpu;
    char gameboard[11][11];
    
    p1.agility;
    p1.armor;
    p1.weapons;
    p1.range;
    
    cpu.agility;
    cpu.armor;
    cpu.range;
    cpu.weapons;
    
    srand(time(0));
    int p1_row = rand()%9+1;
    int p1_col = rand()%9+1;
    int cpu_row = rand()%9+1;
    int cpu_col = rand()%9+1;
    
    
    cout << "\n\n";
    cout << "\t Heyo, wanna play the game? (Y = yes, N = exit): ";
    cin >> choice;
    cout << "\n\n";
    
    
    
    switch (choice)
    {
        case 'Y':
        case 'y':
            //if(gameOver(p1, cpu) = true)
            {
                
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                assignAttributes (p1, cpu);
                do
                {
                    movement (gameboard, p1_row, p1_col, cpu_row, cpu_col, p1);
                    cpumovement (gameboard, p1_row, p1_col, cpu_row, cpu_col, cpu);
                }while(gameOver (p1, cpu));
            }
            break;
            
        case 'N':
        case 'n':
            exit(0);
            break;
         
        default:
            cout << "Wrong input. It's either: Y to play, or N to quit.\n";
    }
    
    
         
    system("PAUSE");
    return EXIT_SUCCESS;
}


void initGrid(char gameboard[][11], int p1_row, int p1_col, int cpu_row, int cpu_col) 
{
    for(int r = 0; r < 11; r++)
    {
        for (int c = 0; c < 11; c++)
        {
            if (r == 0 || r == 10)
                gameboard[r][c] = 205;
            else if(c == 0 || c == 10)
                gameboard[r][c] = 186;
            else
                gameboard[r][c] = ' ';
        }

    } 
    
    
    gameboard[0][0] = 201;
    gameboard[10][0] = 200;
    gameboard[0][10] = 187;
    gameboard[10][10] = 188;
    
    
    
    gameboard[p1_row][p1_col] = 193; //single T- player
    gameboard[cpu_row][cpu_col] = 208; //double TT- computer
}

void printBoard (char gameboard[][11])
{
    for (int r = 0; r < 11; r++)
    {
        for (int c = 0; c < 11; c++)
        {
			cout << gameboard[r][c];
        }
        cout << endl; 
    }
}


void movement (char gameboard [][11], int& p1_row, int& p1_col, int& cpu_row, int& cpu_col, ships & p1)
{
    char move;
    
    for(int moves = 0; moves < p1.agility; moves++)
    {
        cout << "Please enter your moves : ";
        cin >> move;
        switch (move)
        {
            case 'w': //if "move" is assigned w or W .. move up on the board
            case 'W':
                p1_row = p1_row - 1;
                gameboard[11][11] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_row <= 0)
                {
                    p1_row = p1_row + 10;
                    gameboard[11][11] = ' ';
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
            case 'a': //if "move" is assigned a or A.. move left on the board
            case 'A':
                p1_col = p1_col - 1;
                gameboard[11][11] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_col <= 0)
                {
                    p1_col = p1_col + 10;
                    gameboard[11][11] = ' ';
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
            case 'd': //if "move" is assigned d or D.. move right on the board
            case 'D':
                p1_col = p1_col + 1;
                gameboard[11][11] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_col >= 10)
                {
                    p1_col = p1_col - 10;
                    gameboard[11][11] = ' ';
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
            case 's': //if "move" is assigned s or S.. move down on the board
            case 'S':
                p1_row = p1_row + 1;
                gameboard[11][11] = ' ';
                gameboard[p1_col][p1_row] = 193;
                system("CLS");
                initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                printBoard(gameboard);
                if (p1_row >= 10)
                {
                    p1_row = p1_row - 10;
                    gameboard[11][11] = ' ';
                    gameboard[p1_col][p1_row] = 193;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                }
                break;
            default:
                cout << "boom";
                break;
        }
    }
    
    
}

void assignAttributes (ships & p1, ships & cpu)
{
    
    cout << "Please enter agility (only from 0 to 6): ";
    cin >> p1.agility;
    cout << endl;
    cout << "Please enter armor (only from 0 to 6): ";
    cin >> p1.armor;
    cout << endl;
    cout << "Please enter weapons (only from 0 to 4!!!): ";
    cin >> p1.weapons;
    cout << endl;
    cout << "Please enter range (only from 0 to 6): ";
    cin >> p1.range;
    cout << endl;
    
    
    
    
    cpu.agility = rand()%20+1;
    cpu.armor = rand()%6+1;
    cpu.weapons = rand()%4+1;
    cpu.range = rand()%6+1;
    
    
    while(p1.agility < 0 || p1.agility > 6)
    {
        cout << "Too low or too high! PLease enter agility again (only from 0 to 6): ";
        cin >> p1.agility;
        cout << endl;
    }
    while(p1.armor < 0 || p1.armor > 6)
    {
        cout << "Too low or too high! PLease enter armor again (only from 0 to 6): ";
        cin >> p1.armor;
        cout << endl;
    }
    while(p1.weapons < 0 || p1.weapons > 6)
    {
        cout << "Too low or too high! PLease enter weapons again (only from 0 to 4!!!): ";
        cin >> p1.weapons;
        cout << endl;
    }
    while(p1.range < 0 || p1.range > 6)
    {
        cout << "Too low or too high! PLease enter range again (only from 0 to 6): ";
        cin >> p1.range;
        cout << endl;
    }
    
}

void cpumovement (char gameboard [][11], int& p1_row, int& p1_col, int& cpu_row, int& cpu_col, ships & cpu) //random cpu ship movement
{
    
    int s;
    
    for(int moves = 0; moves <= cpu.agility; moves++)
    {
            s = rand()%4+1;
            switch (s)
            {
                
                case 1: //if "s" is assigned 1.. move up on the board
                    cpu_row = cpu_row - 1;
                    gameboard[11][11] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_row <= 0)
                    {
                        cpu_row = cpu_row + 10;
                        gameboard[11][11] = ' ';
                        gameboard[cpu_row][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                case 2: //if "s" is assigned 2.. move left on the board
                    cpu_col = cpu_col - 1;
                    gameboard[11][11] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_col <= 0)
                    {
                        cpu_col = cpu_col + 10;
                        gameboard[11][11] = ' ';
                        gameboard[cpu_col][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                case 3: //if "s" is assigned 3.. move right on the board
                    cpu_col = cpu_col + 1;
                    gameboard[11][11] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_col >= 10)
                    {
                        cpu_col = cpu_col - 10;
                        gameboard[11][11] = ' ';
                        gameboard[cpu_col][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                case 4: //if "s" is assigned 4.. move down on the board
                    cpu_row = cpu_row + 1;
                    gameboard[11][11] = ' ';
                    gameboard[cpu_col][cpu_row] = 208;
                    system("CLS");
                    initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                    printBoard(gameboard);
                    if (cpu_row >= 10)
                    {
                        cpu_row = cpu_row - 10;
                        gameboard[11][11] = ' ';
                        gameboard[cpu_col][cpu_row] = 193;
                        system("CLS");
                        initGrid(gameboard, p1_row, p1_col, cpu_row, cpu_col);
                        printBoard(gameboard);
                    }
                    break;
                default:
                    break;
            }
    }
            


}

bool gameOver (ships p1, ships cpu)
{
    bool over = true;
    if(p1.armor <= -6 || cpu.armor <= -6)
    {
        over = false;
    }
    return over;
}

Please try to "abstract" your question. This has many benefits (more on this here: http://daviddoria.blogspot.com/2010/05/problem-abstraction-helping-others-help.html ). For example, instead of:

and im stuck at a part where my program crashes after the ships move for a while

which has little meaning to anyone not in your class, you should try to pose your question/problem as something generic and basically related to a programming concept:

and im having trouble passing an object by reference

(clearly that is unrelated, but just showing that it has nothing to do with you boats or your game, but a concept of general interest to other programmers (who are going to help you) and other readers (who are going to learn from the question/answer pair)).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.