I'm having a bit of a problem with a two dimensional array of characters. The idea is that the program copies a bit of text using strcpy, and it's supposed to place it in the first line of that array. Where abouts in that line is determined by the length of the strings being copied and a for loop. That particular bit of code is here:

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<stdlib.h>

using namespace std;

#define NLOCATIONS 5

#define HTSIZE 80
#define VTSIZE 25

void main()
{

	int i = 10;

	char Table[VTSIZE][HTSIZE] = {0};

	char szLocations[NLOCATIONS][20] = {"Terminal", "Passenger lounge", "Crew lounge", "Service area", "Private lounge"};

	switch(cMenuOption)
	{

	case '4':
		for(y=0; y<NLOCATIONS; y++)
		{
			strcpy(Table[0][i],szLocations[y]);

			i = i + strlen(szLocations[y]);
			
		}

		cout << Table << endl;

		break;
	}
}

I believe that the problem is due to the fact that I'm trying to copy several strings to one line of the array, and I can only set the start coordinate for that string to be copied to, but the program only wants to set the string in position i, which obviously only allowes one character. Is there any way I can change this to do what I want it to do?

Thanks

The problem with that algorithm is that you can easily create a buffer overrun with it. Each row of Table can only hold 24 characters plus null terminator. Yet your program is attempting to copy a string potentially 19 characters long into the 19th position of Table, meaning that there is only room for 5 more characters in Table. The first 18 characters of Table would remain unused.

I think you need to re-read the assignment. My hunch is that you have the instructions wrong.

Forgive me if I'm wrong, but as far as I know, when declaring a two-dimensional array, the first index is the vertical one and the second index is the horizontal. So, in theory, the array should allow up to 79 usable horizontal characters on each line, which is more than enough to store all the strings and any spaces in between them.

Yes you are right -- I had the 25 and 80 turned around. But the same problem still occurs. Take the first string "Terminal". Your program will copy that string starting in the 10th position of Table[0], e.g. Table[0][10]. What do you intend to do with the first 10 characters of that row?

>>and any spaces in between them
There are no spaces between them because you didn't put any there.

To answer your specific original question, you have to pass addresses to strc py() strcpy(&Table[0][i],szLocations[y]); -- Note the use of the & address operator before Table.

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks for the reply.

The reason why I set up the horizontal character limit to 79 is because I know the default size of the command prompt window allows for 80 characters per line. Oddly enough 79 characters is also the exact number of characters I need to display on the first line.

The array is going to be used to show a table, and these strings I'm trying to display are meant to be the headers of the columns. This table is going to be populated with numbers which represent a temperature reading for each month in each of the 5 locations for a year. That's why I want the first 10 characters to be empty for the first line, because on the next lines the name of each month is going to be written.

I see what you're saying. I will try it tomorrow morning, because I can't test it tonight, but I will let you know if it works, which I really think it will.

Edited 6 Years Ago by drider: n/a

That's perfect, it solved my problem. Thanks, Ancient Dragon!

I've been programming for about 3 years now in different languages and it still amazes me how much of a difference a character can make to a program.

Thanks again

This article has been dead for over six months. Start a new discussion instead.