incompatible types in assignment of 'int' to 'int [7]'

I am getting an error message "incompatible types in assignment of 'int' to 'int [7]' on lines 49 & 52

Here is my whole code, writing a battleship game that's to be played against a computer

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <ctime>

using namespace std;
char box[7][7];

void drawBoard()
{
cout<<" | 1 | 2 | 3 | 4 | 5 | 6 | 7 |"<<endl;
cout<<"--+---+---+---+---+---+---+---+"<<endl;
cout<<"A |"<<box[0][1]<<" |"<<box[0][2]<<" |"<<box[0][3]<<" |"<<box[0][4]<<
" |"<<box[0][5]<<" |"<<box[0][6]<<" |"<<box[0][7]<<" |"<<endl;
cout<<"--+---+---+---+---+---+---+---+"<<endl;
cout<<"B |"<<box[1][1]<<" |"<<box[1][2]<<" |"<<box[1][3]<<" |"<<box[1][4]<<
" |"<<box[1][5]<<" |"<<box[1][6]<<" |"<<box[1][7]<<" |"<<endl;
cout<<"--+---+---+---+---+---+---+---+"<<endl;
cout<<"C |"<<box[2][1]<<" |"<<box[2][2]<<" |"<<box[2][3]<<" |"<<box[2][4]<<
" |"<<box[2][5]<<" |"<<box[2][6]<<" |"<<box[2][7]<<" |"<<endl;
cout<<"--+---+---+---+---+---+---+---+"<<endl;
cout<<"D |"<<box[3][1]<<" |"<<box[3][2]<<" |"<<box[3][3]<<" |"<<box[3][4]<<
" |"<<box[3][5]<<" |"<<box[3][6]<<" |"<<box[3][7]<<" |"<<endl;
cout<<"--+---+---+---+---+---+---+---+"<<endl;
cout<<"E |"<<box[4][1]<<" |"<<box[4][2]<<" |"<<box[4][3]<<" |"<<box[4][4]<<
" |"<<box[4][5]<<" |"<<box[4][6]<<" |"<<box[4][7]<<" |"<<endl;
cout<<"--+---+---+---+---+---+---+---+"<<endl;
cout<<"F |"<<box[5][1]<<" |"<<box[5][2]<<" |"<<box[5][3]<<" |"<<box[5][4]<<
" |"<<box[5][5]<<" |"<<box[5][6]<<" |"<<box[5][7]<<" |"<<endl;
cout<<"--+---+---+---+---+---+---+---+"<<endl;
cout<<"G |"<<box[6][1]<<" |"<<box[6][2]<<" |"<<box[6][3]<<" |"<<box[6][4]<<
" |"<<box[6][5]<<" |"<<box[6][6]<<" |"<<box[6][7]<<" |"<<endl;
cout<<"--+---+---+---+---+---+---+---+"<<endl;
}
//^ this looks really messy, its not that bad just prints the game board and labels each spot
//looks like this:
// | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
//--+---+---+---+---+---+---+---+
//A |

void shipPlacement()
{
int ShipPlacement[7][7];
int r, c;
srand(time(NULL));
for(int c=0; c<8; c++)
{
ShipPlacement[c]=((rand()%7)+1);
for(int r=0; r<8; r++)
{
ShipPlacement[r]=(rand()%7);
}
}
cout<<ShipPlacement;
}

int main()
{
drawBoard();
shipPlacement();
return 0;
}


line 49--> ShipPlacement[c]=((rand()%7)+1);
line 52--> ShipPlacement[r]=(rand()%7);
Last edited on
ShipPlacement[c] is an array of seven int values.

((rand()%7)+1) is a single int.

What you're doing makes no sense. You've got an array of seven int values, and you're saying "this array of seven int values is to be set equal to this one, single int value". Makes no sense.
Hello richieH1997,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Until I can load up the program and see what is happening it is hard to follow your code. Just add code tags to what yu have will not indent the code as it needs to be.

Soon as figure this out I will let you know.

Andy
Hello richieH1997,

In the function "shipPlacement" you define the array "ShipPlacement" as a 2D array. IMHO I would call this backwards. i would use the capital "S" for the function name and the lower case "s" for the variable name.

Anyway ShipPlacement[c]=((rand()%7)+1); "ShipPlacement" is a 2D array and you are only using one dimension to store your result. The reason this does not work is because the result of "rand" needs to be stored in the second dimension not the first.

"srand" should be near the beginning of main because you only need to call it once. Should your game play again as is "srand" would be called again when "shipPlacement" is called and the numbers may not be as random as you might think.

((rand() % 7) + 1) this will miss the number zero. This may work, but for an array that starts at zero you would be missing an entire row or column.

To set a 2D array you will need an outer for loop to control the row and the inner for loop, that you have, to control the columns.

I offer this suggestion for your program:

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
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <ctime>

constexpr unsigned int MAXROW{ 7 };
constexpr unsigned int MAXCOL{ 7 };

void shipPlacement(int ShipPlacement[MAXROW][MAXCOL])
void drawBoard(char box[MAXROW][MAXCOL])


int main()
{
	char box[MAXROW][MAXCOL];
	int ShipPlacement[MAXCOL][MAXCOL];

	srand(time(NULL));  // <---Needs called only once. Best done here.

	drawBoard();
	shipPlacement();


	return 0;
}

// Functions here 


Hope that helps,

Andy
Thank you andy, I realize my mistake thanks so much!
Side note I haven't learned in my c++ class what "constexpr" is or "unsigned" if you could just explain what those do that'd be greatly appreciated!
Hello richieH1997,

You are welcome.

If you are finished be sure to green check the thread so others will know.

Andy
closed account (E0p9LyTq)
constexpr (since C++11):
http://en.cppreference.com/w/cpp/language/constexpr

unsigned:
http://en.cppreference.com/w/cpp/language/types

A simpler explanation of unsigned. An integer type that can not hold any negative values.

For example:
int is a signed type. It can hold negative and positive values. -1 or 125.

To understand the differences between signed and unsigned run this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <limits>

int main()
{
   std::cout << "type\tlowest()\tmin()\t\tmax()\n\n";

   std::cout << "int\t"
      << std::numeric_limits<int>::lowest() << '\t'
      << std::numeric_limits<int>::min() << '\t'
      << std::numeric_limits<int>::max() << '\n';

   std::cout << "\nunsigned int\t"
      << std::numeric_limits<unsigned int>::lowest() << '\t'
      << std::numeric_limits<unsigned int>::min() << '\t'
      << std::numeric_limits<unsigned int>::max() << '\n';
}

type    lowest()        min()           max()

int     -2147483648     -2147483648     2147483647

unsigned int    0       0       4294967295


int can be written as signed int, unsigned int can be written as unsigned.
I use eclipse for my coding, and it says the program gave has errors , its probable just eclipse i guess, never seen the std:: stuff.
also constexpr didn't work, i changed it to just const int MAXROW[7];
closed account (E0p9LyTq)
constexpr didn't work

Your compiler is either unable to use a newer C++ standard, or you haven't set the language standard to make constexpr available.

I don't use Eclipse, so I don't know what the problem might be.

never seen the std:: stuff.
using namespace std; will do that. It is considered a bad thing to do.
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice.

You might want to learn about the C++ random library.
http://en.cppreference.com/w/cpp/numeric/random

srand()/rand() have serious drawbacks.
http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/
Hello richieH1997,

As FurryGuy said "constextp" is from C++11 on, but "const" will work just as well. What this is likely saying is that your compiler and header files need upgraded to at least cover the C++11 standards.

In short the unsigned means that the variable can only hold positive numbers. This is useful because there are many functions that return an "unsigned int" and the subscript of any type of array can only have a positive number. Using "unsigned" just makes sure that what is going into the variable is a positive number.

Hope that helps,

Andy
closed account (E0p9LyTq)
const tells the compiler you, the programmer, won't be trying to change an object, a variable. If you do, the compiler slaps your hand and says, "no, no no."

constexpr expands what const can do to functions.
1
2
3
4
constexpr int factorial(int n)
{
    return n <= 1 ? 1 : (n * factorial(n - 1));
}

Any use of the function is evaluated at compile-time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

constexpr int factorial(int n)
{
   return n <= 1 ? 1 : (n * factorial(n - 1));
}

int main()
{
   // this is evaluated at compile-time, not run-time
   int fact4 = factorial("A");

   std::cout << "4!: " << fact4 << '\n';
}

Error	C2664	'int factorial(int)': cannot convert argument 1 from 'const char [2]' to 'int'
Error (active)	E0167	argument of type "const char *" is incompatible with parameter of type "int"

So you can find possible problems at compile-time instead of when your program is running.
Last edited on
Topic archived. No new replies allowed.