Allowing user to place objects on a array

I want to make the code so a user can manually input 5 different types of "ships"
e.g
Aircraft Carrier 5 long
Patrol Boat 4 long etc all different lengths

Ive already tried using a string and create 5 different classes but to no avail and now im stuck.

I want to input them on the function clear, but am not sure how. Any input/help is greatly appreciated



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <ctime>
#include <random>
#include <string> 

//Set up Variables 
//Set up Menu
//Set Up AI Grid
//Set up Player Grid
//Set up Playerships

using namespace std;

const int row = 10; // Sets up Grid*
const int collum = 10; // Sets up grid
const char water = 247; // Water on grid
const char hit = 'x'; //Displyed when a ship is hit
const char Pship = 's'; //Displayed where a player places a ship



int maxship = 5; //Sets Max # of ships on AI board
int matrix[row][collum]; //*
const int shipnum = 5;



void clear() // Clears the grid
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < collum; j++)
		{
			matrix[i][j] = 0;

		}
	}
}




void show() //Displays the Players grid
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < collum; j++)
		{
			cout << matrix[i][j] << " ";
		}
		cout << endl;
	}
}

int numship() //Tells the player how many AI ships are left
{
	int c = 0;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < collum; j++)
		{
			if (matrix[i][j] == 3) //
				c++;
		}
	}

	return c;

}

void setshipsuser() //Sets up the ships randomly for the AI player (Sets AI ships to 3 on the array/ AI Grid)
{
	int s = 0;
	while (s < maxship)
	{
		int x = rand() % row;
		int y = rand() % collum;
		if (matrix[x][y] != 3)
		{
			s++;
			matrix[x][y] = 3;

		}
	}
}

void setships() //Sets up the ships randomly for the AI player (Sets AI ships to 3 on the array/ AI Grid)
{
	int s = 0;
	while (s < maxship)
	{
		int x = rand() % row;
		int y = rand() % collum;
		if (matrix[x][y] != 3)
		{
			s++;
			matrix[x][y] = 3;

		}
	}
}

bool attack(int x, int y) //Allows the player to fire on X,Y Cords of their choice 
{
	if (matrix[x][y] == 3)
	{
		matrix[x][y] = 2;
		return true;

	}
	return false;
}



int main()
{

	bool Quit = 0; //If Quit = 1 it will exit the program 
	//Player Selection Menu

Begin:
	cout << "1: Play Game \n"
		"2: Quit Game \n"
		"3: Game Credits \n"
		"4: How To Play \n";

	int Select; //Variable to allow player to select a option 
	cout << "Please Enter a Number from the Options: ";
	cin >> Select;



	if (Select == 1)
	{
		cout << "\n"
			"\n"
			"\n";

		srand(time(NULL));
		clear();
		show();
		cout << "------------------------" << endl;
		setships(); // *
		show(); // Shows AI Board (Testing Feature only)*
		cout << "------------------------" << endl;
		// setshipsuser();
		// show();
		int pos1, pos2;
		while (1)
		{
			cout << "Please input Location (X then Y):"; cin >> pos1 >> pos2; // Asking player where to "fire"
			if (attack(pos1, pos2)) //If the hit is succesful 
				cout << "Hit succesful" << endl;
			else
				cout << "Hit Failed" << endl; //If there is no hit
			cout << "Remaining Ships: " << numship() << endl;

		}
		if (numship == 0)
			cout << "Remaining Ships: " << numship() << endl;
		cout << "You are out of ships!";



		system("pause");
		return 0;
	}
}
Did you say you wanted to make a class for every ship they enter? That doesn't seem necessary if that's what you intend. You can make one class with all the variables needed.

Otherwise, you can input what the user wants like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <string>

int main()
{
	std::string ships[5];

	for (int input = 0; input < 5; input++)
	{
		std::cout << "Enter A Ship: ";
		std::getline(std::cin, ships[input]);
		std::cout << '\n';
	}

	//Output the string
	std::cout << '\n';
	for (int i = 0; i < 5; i++)
		std::cout << ships[i] << '\n';
}


Then, if you want to use it in the function clear, simply give the variable to it as you've done for other variables on line 103/153
Any input/help is greatly appreciated

Certainly wasn't last time.
"Any input/help will be completely ignored." seems more accurate. :-)
Topic archived. No new replies allowed.