Hello,
I am new to C++. I have read the warnings about homework and do TRY.... I am an older student and have been trying to get this project going to no avail.... My first part of this code is attempting to create a grid of 16 playing cards laid out 4x4..... so on an x, y axis...[I]Card_face[/I] has been commented out....as I can't even get syntax for [I]card_value[/I] yet...however, I had planned to use a for loop for [I]card_face[/I] (which will be a function that would hide all cards from user by placing them face down - except a chosen pair). Can anyone help me with these errors>?> I've tried rearranging array values, adding and removing function names, and so on....
Thank you very much,
RiCk

1>------ Build started: Project: ArrayProject, Configuration: Debug Win32 ------
1>Compiling...
1>HomeworkProject.cpp
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(7) : error C2040: 'card_value' : 'int [4][4]' differs in levels of indirection from 'int'
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(9) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(9) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>Build log was saved at "file://l:\Prgm Pjts\ArrayProject\ArrayProject\Debug\BuildLog.htm"
1>ArrayProject - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

.

#include <iostream>
using namespace std;


int card_value, card_face, card_value;
//int card_face [4][4];
 int card_value[4][4];

int fill_values(card_value[4][4]);

int main()




{

        int fill_values(card_value[][])

        card_value[0][0]=1;
        card_value[0][1]=2;
        card_value[0][2]=3;
        card_value[0][3]=4;
        card_value[1][0]=5;
        card_value [1][1]=6;
        card_value [1][2]=7;
        card_value [1][3]=8;
        card_value [2][0]=9;
        card_value [2][1]=10;
        card_value [2][2]=11;
        card_value [2][3]=12;
        card_value [3][0]=13;
        card_value [3][1]=14;
        card_value [3][2]=15;
        card_value [3][3]=16;





return 0;
}


    //card_face [i][j]=0
 //for(i = 0; i < 4; i++)
 //       for(j = 0; j< 4; j++)

Recommended Answers

All 8 Replies

>int card_value, card_face, card_value;
>int card_value[4][4];
So, how many variables are you going to try to give the exact same name?

>int fill_values(card_value[4][4]);
You sure do like the name card_value, but for a function declaration you need to provide a data type for the parameters. In other words:

int fill_values ( int card_value[4][4] );

You also need to define the function somewhere or you'll get linker errors when you try to build the program.

>int fill_values(card_value[][])
And this is supposed to do what, exactly?

Nurue,
Thank you for getting back. I forgot about data type in function...with that change however, now I have many more errors, and still this one about "indirection?" I also thought I had the function fill_value sdeclared above main?? Anyway, here is the project and the errors and what I thought was the amended code:

A common memory matching game played by young children is to start
with a deck of cards that contain identical pairs. For example, given six
cards in the deck, two might be labeled “1,” two might be labeled “2,”
and two might be labeled “3.” The cards are shuffled and placed face
down on the table. The player then selects two cards that are face down,
turns them face up, and if they match they are left face up. If the two cards
do not match, they are returned to their original position face down. The
game continues in this fashion until all cards are face up.
Write a program that plays the memory matching game. Use 16 cards that
are laid out in a 4_4 square and are labeled with pairs of numbers from 1
to 8. Your program should allow the player to specify the cards that she
would like to select through a coordinate system.

(diagram of an x,y grid shown here with asteriks filled except for the pair face up)

For example, suppose the cards are in the following layout:
All of the cards are face down except for the pair 8, which has been
located at coordinates (1, 1) and (2, 3). To hide the cards that have been
temporarily placed face up, output a large number of newlines to force
the old board off the screen.
Hint: Use a two-dimensional array for the arrangement of cards and
another two-dimensional array that indicates if a card is face up or face
down. Write a function that “shuffles” the cards in the array by repeatedly
selecting two cards at random and swapping them. Random number
generation is described in Appendix 4.

oject.cpp
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(5) : error C2086: 'int card_value' : redefinition
1>        l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(5) : see declaration of 'card_value'
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(7) : error C2040: 'card_value' : 'int [4][4]' differs in levels of indirection from 'int'
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(9) : error C2087: 'card_value' : missing subscript
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(20) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(21) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(22) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(23) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(24) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(25) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(26) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(27) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(28) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(29) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(30) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(31) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(32) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(33) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(34) : error C2109: subscript requires array or pointer type
1>l:\prgm pjts\arrayproject\arrayproject\homeworkproject.cpp(35) : error C2109: subscript requires array or pointer type
1>Build log was saved at "file://l:\Prgm Pjts\ArrayProject\ArrayProject\Debug\BuildLog.htm"
1>ArrayProject - 19 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Code

#include <iostream>
using namespace std;



int card_value, card_face, card_value;
//int card_face [4][4];
int card_value[4][4];


int fill_values(card_value[4][4]);


int main()



{


int fill_values(int card_value[][])


card_value[0][0]=1;
card_value[0][1]=2;
card_value[0][2]=3;
card_value[0][3]=4;
card_value[1][0]=5;
card_value [1][1]=6;
card_value [1][2]=7;
card_value [1][3]=8;
card_value [2][0]=9;
card_value [2][1]=10;
card_value [2][2]=11;
card_value [2][3]=12;
card_value [3][0]=13;
card_value [3][1]=14;
card_value [3][2]=15;
card_value [3][3]=16;


return 0;
}



//card_face [j]=0
//for(i = 0; i < 4; i++)
//       for(j = 0; j< 4; j++)

>with that change however, now I have many more errors
While it's amusing that you ignored the better part of my post, I'll note that each quote and the reply to that quote in my response identified a problem with your code. If you only corrected one of those problems, clearly you'll still get errors. :icon_rolleyes:

Functions, like variables, need to be declared before being used. They can be declared from within other functions, though most frequently, they are declared before main() is called. Then, paralleling the need to give a variable some meaningful variable before trying to use it, you need to provide the function with some meaningnful information before you use it, too. This is done in the function definition. Sometimes the definition is done at the time of declaration, but often the code is placec after main(). If the function is declared before use and defined somewhere, then you can use it in your program by writing a function call.

In your case this:
int fill_values(card_value[4][4]);
is a valid function declaration. But this:
int fill_values(int card_value[][])
appears to be some ill conceived conglomeration. Given the type name preceding a name followed by the ()s it looks like a possible redeclaration of fill_values, except it doesn't have the necessary size of the array in the parenthesis and it doesn't have a semicolon after the closing parenthesis. If it was supposed to be a funtion call, then it shouldn't have the type name in front of the function name and it just needs the name of the array in the (), though it still would need a semicolon after the closing parentheis. My advice would be to add comments to your code so you know exactly what you are trying to do until you become comfortable doing it as second nature and don't need the explicit thought process written out.

#include <iostream>
using namespace std;

//function declaration
int fill_values(card_value[4][4]);

int main()
{
   int card_value[4][4]; //no need to be global scope
   fill_values(card_value); //function call
   return 0;
}
//function definition
int fill_values(card_value[4][4])
{
  card_value[0][0]=1;
  card_value[0][1]=2;
  card_value[0][2]=3;
  card_value[0][3]=4;
  card_value[1][0]=5;
  card_value [1][1]=6;
  card_value [1][2]=7;
  card_value [1][3]=8;
  card_value [2][0]=9;
  card_value [2][1]=10;
  card_value [2][2]=11;
  card_value [2][3]=12;
  card_value [3][0]=13;
  card_value [3][1]=14;
  card_value [3][2]=15;
  card_value [3][3]=16;
}

Ahhh...Thank you....
However,
I don't understand how to get a "grid." Ostensibly, an array [4][4] will place assigned values indexed at 0-3 on an x and y axis, will it not? I tried to cout either card_value or fill_value and get only a single line? :

int  card_face;
//int card_face [4][4];
 int card_value[4][4];

void fill_values(int card_value[][4]);

int main(void)




{

int fill_values(card_value[4][4]);

     card_value[0][0]=1;
     card_value[0][1]=2;
     card_value[0][2]=3;
     card_value[0][3]=4;
     card_value[1][0]=5;
     card_value [1][1]=6;
     card_value [1][2]=7;
     card_value [1][3]=8;
     card_value [2][0]=9;
     card_value [2][1]=10;
     card_value [2][2]=11;
     card_value [2][3]=12;
     card_value [3][0]=13;
     card_value [3][1]=14;
     card_value [3][2]=15;
     card_value [3][3]=16;



       cout << fill_values << endl;

return 0;
}

>I tried to cout either card_value or fill_value and get only a single line?
fill_value returns an int, so if you try to print the result you'll only get one int. Likewise, if you try to print the function rather than the return value, you'll get an address:

#include <iostream>

int foo()
{
  return 10;
}

int main()
{
  std::cout<< foo() <<'\n';
  std::cout<< foo <<'\n';
}

In the case of card_value, it's an array, so to print all of the contents of the array, you need to loop through them. Printing only one element, or the array itself will naturally only give you one result:

#include <iostream>

int main()
{
  int a[4][4] = {
    {1,  2, 3, 4},
    {5,  6, 7, 8},
    {9, 10,11,12},
    {13,14,15,16}
  };

  std::cout<< a <<'\n';

  for ( int i = 0; i < 4; i++ ) {
    for ( int j = 0; j < 4; j++ )
      std::cout<< a[i][j] <<' ';
    std::cout<<'\n';
  }
}

The array is declared in such a fashion as to appear like a grid, but how you display the information is up to you.

To display the information in a 2 dimensional (2 sets of []s) array in a grid pattern you would typically use a nested loop, often a for loop within a for loop. The outer loop can control the index of either the rows or the columns and the inner loop the other. If the grid is square, then it doesn't really matter which is which. Some people prefer to have the outer loop correspond to the first set of [] and the inner the second set of [], but it's up to you. Typically I'll use the first set of [] to be the rows of the grid and the outer loop and the second the set to be the columns and controlled by inner loop, but each time I use the nested loop I have to think about which represents which, and if I'm not comfortable I comment it so I explicitly remind myself of what I am doing.

Gentlemen,
Thank you very much. I can't believe the amount of help I've recieved in such a short time! I need to write two more functions to output this data, so I wouldn't have wanted to output card_value as it is anyway: 1-16. I've got to write for a cards face down (hidden), then I'll attempt to write a function "show_board" that will output the "grid" as you've shown me
Much work to do-thanks again everybody!
Rick

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.