I have to write a memory game, where the user gives a file name and that file reads in 8 strings that will be placed on the board. The code I have so far is down below, if anyone can help me fix it up I would appreciate it.

#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
#include <iomanip>
using namespace std;

const int size = 4;  //controls dimensions of array, here there will be a 4 X 4 grid

void displaygrid(int ch1, int ch2, string board[][size], bool dec[]);
void getgrid(string filename, string output[][size], bool choice []);
void drawbar();
int getchoice(int num1);

int main()
{
	srand((int)time(NULL));
	int ch1 = 1, ch2 = 1;
	string board [size][size];
	bool dec [size];
	string filename;
		
	cout << "  How good is your memory? " << endl << endl << endl << " Enter the name of the file  ";
	getline(cin,filename);

	getgrid(filename,board,dec);
	
	drawbar();
	displaygrid(ch1,ch2,board,dec);
	drawbar();
	

	return 0;
}

void drawbar()
{
	cout << "--------------------------------------------------------" << endl;
}
int getchoice(int num1)	//Takes choice for match during each round, so it can be checked
{

	cout << "Enter your choice (1-16) ";
	cin >> num1;
	
	return num1;
}
void getgrid(string filename, string output[][size], bool choice []) //gets the name of the file and put it in to the array	
{
	ifstream inf;
	inf.open(filename.c_str());
	
	string name; // read in stuffs from the file.
	inf >> name;
	
	int ct = 0;
	int random = rand() % 8;

	while (inf)	// to read in the names from the file to array.
	{
		string names[8];
		names[ct]= name;
		inf >> names[ct];
		ct++;
	}
	for(int row = 0; row < size; row++)
		{
			for(int col = 0; col < size; col++)
			{	
				string names[8];
				names[ct]= name;
				
				output [row][col] = names[random];
				cout << output[row][col];
			}
		}inf.close();
	
}

void displaygrid(int ch1, int ch2, string board[][size], bool dec[])
{
	int ctr = 0;
	for (int r = 0; r < size; r++)
		{
			for (int c = 0; c < size; c++)
			{
				if (dec[size] == true || ch1 ==  r * size + c + 1 || ch2 ==  r * size + c + 1)

					cout << setw(15) << board[r][c];
				else
					cout << setw(15) << "***" << ctr << "***" << board[r][c];
				ctr++;
				
			}		
		}
	cout << endl;
}
void begingrid (bool dec[16])
{
	for (int ct = 0; ct < 16; ct++)
	{
			dec[ct]= false;
			ct++;
	}
}

Sample interaction with the user should look like this:

How's Your Memory?
how long will it take you to match all 16 entries?

Enter filename t.in

--------------------------------------------------------
| *** 1*** | *** 2*** | *** 3*** | *** 4*** |
| *** 5*** | *** 6*** | *** 7*** | *** 8*** |
| *** 9*** | ***10*** | ***11*** | ***12*** |
| ***13*** | ***14*** | ***15*** | ***16*** |
--------------------------------------------------------
Enter your choice (1-16) 1
--------------------------------------------------------
| chair | *** 2*** | *** 3*** | *** 4*** |
| *** 5*** | *** 6*** | *** 7*** | *** 8*** |
| *** 9*** | ***10*** | ***11*** | ***12*** |
| ***13*** | ***14*** | ***15*** | ***16*** |
--------------------------------------------------------
Enter your choice (1-16) 2
--------------------------------------------------------
| chair | dog | *** 3*** | *** 4*** |
| *** 5*** | *** 6*** | *** 7*** | *** 8*** |
| *** 9*** | ***10*** | ***11*** | ***12*** |
| ***13*** | ***14*** | ***15*** | ***16*** |
--------------------------------------------------------
Press enter
(clear screen)


--------------------------------------------------------
| chair | dog | chair | ran |
| rainbow | dog | hat | blue |
| ran | computer | blue | computer |
| rainbow | cat | cat | hat |
--------------------------------------------------------
It took you 16 turns to solve it!

Ok, here's a comparable question. My car wouldn't start this morning. Can you fix it? Pretty broad scenarion, eh? Please be more specific with your question. If the code doesn't compile give the error messages. If the code gives wrong output, give observed output in addition to desired output. If you're not sure how to do something, tell us specifically what you are trying to do, and what you've tried to do that didn't work, etc.

The program compiles ok but when I run it I get an error message asking me to abort and I don't know why.

It says debug assertion failed, and
Expression: !dst= NULL

I need the program to pass arrays and use the functions to get the output I posted above during a game simulation

Problem is probably in getGrid(). Probably line 72, but I can't swear to that.

What I do know is that each time through the loop starting on line 59 you redeclare an array of 8 strings (line 61). Same with line 70. You definitely don't want to do that.

What I think you want to do is to select 16 unique numbers in range of 0-15 two at a time randomly. Each of the two numbers represent a cell in the grid to put the word in. The cells are numbered from 0-3 in first row, 4-7 in second row, etc. To determine which cell to use the row will be random number divided by 4 and the column will be random number modulo 4. Maybe something like this:

bool notUnique= true;
int temp[16] = 0;
while(inf >> name)  //for each new name in the file
{
   //get 2 unique numbers ranging from 0-15 
   for(int i = 0; i < 2; ++i)
   {
      notUnique = true;
      while(notUnique)
      {
        a = rand() % 16;
        if(temp[a] == 0)
        {
           temp[a] = 1;
           notUnique = false;
        }
     }
     //convert unique number a into row and col indexes
     row = a/4;
     col = a % 4;
     //assign name to appropriate cell of board
     names[row][col] = name;
   }
}

What type should the name variable and the a variable be, and also the row and col variables? And should this replace the entire getgrid() function I have or should it just be inserted somewhere?

I would declare names as a 2D array of strings in main() just like you did the variable called board. In my version I would pass names to getGrid() as a single argument. Variables local to getGrid() would be the file name to open, an ifstream to open the file, a string variable calle name to store the word read from the file, 3 ints named a, row and col (in additon to any int variables used in loops as counters), an array of int (could be boolean array I suspose instead of int if you wish) called temp to be sure that only unique numbers are used when selecting cells within the grid and a single bool variable to act as a flag when obtaining the unique numbers. The purpose of getGrid() is to assign the words in the file randomly into names so that two cells in names each have the same word in it. This means that names will act as the solution to the puzzle for this game.

displayGrid() could be written to display any 2D array of strings. I' not going to worry about trying to center strings in the display until I get the logic of the game itself worked out.

The logic could be something like this. In each new game board is initially filled with a default string meaning a given cell with this value is available to be chosen. An outer loop will continue until 8 matches have been completed, meaning the game is over. An inner loop allows the user to choose two cell numbers, one at a time. The first cell number would determine which word in names is temporarily displayed in board by assigning the string in the chosen cell in names to the corresponding cell in board and displaying board. Once the player sees which word is displayed they choose a second cell, testing their memory to see if the can remember where else they saw that word before. The second selection is then obtained which is also temproarily transfered to board displaying both of the users choices. If the two words displayed in board are the same, then the default value in the two chosen cells in board is changed to a different value indicating the cells are no longer availble to be chosen and the variable to keep track of the number of matches correctly made so far in this game is incremented by one. If the two cells displayed don't match, then the default value is reassigned to the chosen cells in board. In either case, 2 additional cells are selected over and over again until all 8 matches have been found.

Do you think you could give me some idea of what code for the main function should look like....I don't really know how to implement the displaygrid and getgrid functions together

getGrid() could be written by declaring variables as previously described, add code you've already written to open the file with the 8 words to use in the puzzle and copy/paste the code for getGrid() I posted previously.

void getGrid(string names[SIZE][SIZE];
void displayGrid(string temp[SIZE][SIZE]);
int main()
{
   const int SIZE = 8;
   string board[SIZE][SIZE];
   string names[SIZE][SIZE];
   string mysteryWord = "********"; //default meaning not yet matched
   
   //load board with default values
   for(int row = 0; row < SIZE; ++row)
   {
      for(int col = 0; col < SIZE; ++col)
      {
         board[row][col] = mysteryWord;
      }
   }
   displayGrid(board);

   getGrid(names);
   displayGrid(names);
   
   //run game loop here, after you can successfully set up and display both board and names.  

   cin.get();
   return 0;
}

void displayGrid(string temp[SIZE][SIZE])
{
   for(int row = 0; row < SIZE; ++row)
  {
    for(int col = 0; col < SIZE; ++col)
    {
      cout << temp[row][col] << ' ';
    }
    cout << endl;
  }
}
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.