battleship can't find fatal error

For a school assignment I have to create a battleship game where one random 4 length battleship (horizontal or vertical) is generated in a 8x8 gameboard. The player has 15 torpedoes to try and sink the ship. I used a 2D vector and am pretty much done with the code:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    #include <iostream>
    #include <vector>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    void generateship(vector<vector<int> >&field);
    void fire(vector<vector<int> >&field);
    void display(const vector<vector<int> >field);
    
    int main()
    {
    	srand(time(0));
    
    	vector<vector<int> >field(8);
    
    	for (int x = 0; x < field.size(); x++)
    		field[x].resize(8);
    	for (int x = 0; x < field.size(); x++)
    		for (int y = 0; y < field[y].size(); y++)
    			field[x][y] = 0;
    
    	generateship(field);
    	fire(field);
    
    
    	system("pause");
    
    	return 0;
    }
    void generateship(vector<vector<int> >&field)
    {
    	int row, col;
    	int direction = rand() % 4;
    	switch (direction)
    	{
    	case 0: 	row = rand() % 5 + 3;
    				col = rand() % 8;
    				field[row][col] = 1;
    				field[row - 1][col] = 1;
    				field[row - 2][col] = 1;
    				field[row - 3][col] = 1;
    				break;
    	case 1:		row = rand() % 8;
    				col = rand() % 5;
    				field[row][col] = 1;
    				field[row][col + 1] = 1;
    				field[row][col + 2] = 1;
    				field[row][col + 3] = 1;
    				break;
    	case 2:		row = rand() % 5;
    				col = rand() % 8;
    				field[row][col] = 1;
    				field[row + 1][col] = 1;
    				field[row + 2][col] = 1;
    				field[row + 3][col] = 1;
    				break;
    	case 3:		row = rand() % 8;
    				col = rand() % 5 + 3;
    				field[row][col] = 1;
    				field[row][col - 1] = 1;
    				field[row][col - 2] = 1;
    				field[row][col - 3] = 1;
    				break;
    	}
    	display(field);
    }
    void fire(vector<vector<int> >&field)
    {
    	int row, col;
    	int torps = 15;
    	int hitcounter = 0;
    	while (hitcounter != 4 || torps != 0)
    	{
    		cout << torps << " torpedoes remain. Fire where? ";
    		cin >> row >> col;
    		switch (field[row][col])
    		{
    		case 0: cout << "Miss!" << endl << endl;
    			field[row][col] = 2;
    			break;
    		case 1: cout << "Hit!" << endl << endl;
    			field[row][col] = 3;
    			hitcounter = hitcounter + 1;
    			break;
    		case 2: cout << "Missed again!" << endl << endl;
    			break;
    		case 3: cout << "Hit again!" << endl << endl;
    			break;
    		}
    		torps = torps - 1;
    		display(field);
    	}
    	if (hitcounter == 4)
    		cout << "You win!";
    	else if (torps == 0)
    		cout << "You are out of torpedoes! Game over.";
    }
    void display(const vector<vector<int> >field)
    {
    	for (int row = 0; row < 8; row++)
    	{
    		for (int col = 0; col < 8; col++)
    		{
    			switch (field[row][col])
    			{
    			case 0:		cout << ". ";
    				break;
    			case 1:		cout << ". ";
    				break;
    			case 2:		cout << "X ";
    				break;
    			case 3:		cout << "O ";
    				break;
    			}
    		}
    		cout << endl;
    	}
    }


But when I run the code a big dialogue box comes up with saying things like "fatal error" and asks me if I want to abort or retry. In my compiler it says there is nothing wrong with the code. Am I misusing double vectors? (This is my first time implementing them).

Any help is appreciated!
Stop spamming the forum. Don't double post. Use the same thread, you can ask more than one question.

Redirect answers here - http://www.cplusplus.com/forum/general/182129/
Last edited on
This isn't a double post, nor am I spamming. Please read my entire post before assuming I double posted off the get go.
Last edited on
It's about the same code and same game. You don't need to create a new thread, just ask your question in your original one.

Not to mention that yesterday you posted 3 or more threads titled lab5 homework, lab6 homework, lab7 homework etc within 2 minutes, in which they only contained a mediafire link with nothing else. Please read this before you post next time - http://www.cplusplus.com/forum/beginner/1/

Thank you.
Last edited on
It's about the same code and same game.

No, it's not. Same game, different code, different issue. The sort of post I would've recommended the OP move to a new thread.

On line 21, field[y].size() should be field[x].size().

However, there is no actual need for the nested loops that line occurs in. The elements in vectors are value-initialized and for int types that means they are set to 0. In fact, you can reduce main considerably:

1
2
3
4
5
6
7
8
9
int main()
{
    srand(time(0));

    vector<vector<int> >field(8, vector<int>(8));

    generateship(field);
    fire(field);
}


I did not inspect the rest of your code.
Topic archived. No new replies allowed.