Problem with character/pointer arrays

netsniper

Newcomer
Joined
Aug 21, 2003
Messages
12
Hi. I am having a small problem with an exercise from Bronson's "A First Book of C++" second edition. It can be found in section 9.3, exercise #6. Here is my code and the comments tell what it is supposed to do:

-----------------------------------------
/*
Date: 2003.12.10
Purpose: Write a function that will accept ten lines of user-input text and store the entered lines
as ten individual strings. Use a pointer array in your function.
*/

#include <iostream>
using namespace std;

const int MAXCHARS = 80;
const int MAX = 10;
void getLines(char *m);

int main()
{
char *messages = new char[MAX];
char *pnt[MAX];
int i;


for(i=0; i<MAX; i++)
{
cout << "Enter a string: ";
cin.getline(messages+i, MAXCHARS);
}

//getLines(messages);

for(i=0; i<MAX; i++)
{
//*(pnt+i) = *(messages+i);
cout << "Line " << i+1 << ": " << *(messages+i) << endl;
}

return 0;
}

// get the user input
void getLines(char *m)
{
int i = 0;

while(i<MAX)
{
cout << "Please enter a string: ";
cin.getline(m+i, MAXCHARS);
m++;
i++;
}
}

/*
Analysis:
*/
-----------------------------------------

Any ideas? cin.getline() only grabs one character, what's wrong with me?

Netsniper
 
I think 1 problem is, is you declare the variable message with a size of MAX instead of MAXCHARS.

and you want to change

cin.getline(messages+i, MAXCHARS);
to
cin.getline(messages, MAXCHARS);

with getline, the 1st parameter is the char array you want to store it in and the 2nd parameter is the maximum number of characters to accept. in your code, you were writing the the cstring begning at index i and you had declared the char array with a length of 10. Eventually you would end up writing data to the wrong place in memory or have a buffer overflow.
 
I did get it somewhat working, although, I'm not sure how to declare a set of arrays to store the user supplied data during runtime. What I mean is, if they want to input a variable number of arrays, my program won;t work. The modified code below only works for a fixed array set of 10:

Purpose: Write a function that will accept ten lines of user-input text and store the entered lines
as ten individual strings. Use a pointer array in your function.
*/

#include <iostream>
using namespace std;

const int MAXCHARS = 80;
const int MAX = 10;
void getLines(char *m[MAX]);

int main()
{
char str1[MAXCHARS], str2[MAXCHARS], str3[MAXCHARS], str4[MAXCHARS], str5[MAXCHARS],
str6[MAXCHARS], str7[MAXCHARS], str8[MAXCHARS], str9[MAXCHARS], str10[MAXCHARS];
char *messages[MAX] = {str1, str2, str3, str4, str5, str6, str7, str8, str9, str10};
int i;

getLines(messages);

for(i=0; i<MAX; i++)
{
cout << "Line " << i+1 << ": " << *(messages+i) << endl;
}

return 0;
}

// get the user input
void getLines(char *m[MAX])
{
int i = 0;

for(i=0; i<MAX; i++)
{
cout << "Enter a string: ";
cin.getline(*(m+i), MAXCHARS);
}
}

/*
Analysis: This was BY FAR the hardest problem I have encountered yet. I had much trouble trying to figure out
how to store the user input without hard coding the individual storgae arrays! I tried assigning the message pointer
to new char[MAX], but this only allowed for 10 characters to be input. I could not figure out a way to create and new
element of 10 character arrays, versus ten characters total. Any ideas? I tried "new string[MAX]", but didn't work.
I tried "new char[MAX]", like I said before, but this only allows ten characters to be pointed to, not ten arrays of
characters. Hrmmm... Maybe I am just really dumb...
*/

Netsniper
 
Use a dynamic array that contains pointers to char arrays

Code:
/*
Date: 2003.12.10
Purpose: Write a function that will accept ten lines of user-input text and store the entered lines
as ten individual strings. Use a pointer array in your function.
*/

#include <iostream>
#include <string>

using namespace std;

const int MAXCHARS = 80;
//const int MAX = 10;

int main()
{
	int MAX;
	cout << "Enter MAX value: ";
	cin >> MAX;
	cout << endl;
	//an array is created. THe length of the array is equal to whatever number
	//the user entered above. Each index of the array can store a char array.
	char** ptr2ptr = new char*[MAX];
	for(int i = 0; i < MAX; i++)
	{
		//initialize the index with char array.
		ptr2ptr[i] = new char[MAXCHARS];
		cout << "Enter a string: ";
		//read in a user specified value
		cin.getline(ptr2ptr[i], MAXCHARS);
	}
	//iterate through each char array in the array 'ptr2ptr' and output the
	//value the user entered.
	for(i=0; i<MAX; i++)
	{
		cout << "Line " << i+1 << ": " << ptr2ptr[i] << endl;
		//delete the array we created.
		delete[] ptr2ptr[i];
	}
	//delete 'ptr2ptr'.
	delete[] ptr2ptr;
	return 0;
}
 
Sweet! Thanks for your help ;-) I have never used double pointers before. How long have you been programming C++? I am taking an intro class right now, but am about to finish it up and move onto some Java...

Netsniper
 
I stopped using c++ last spring.
I was using it on and off for 4 years...college

It's all about .net now though!
The advantages are:
projects get completed in less time
it's a really good alternative to mfc and the win32 api (I never learned those.)
GUI is simple

The only drawback is the cruntime is usually faster, but speed has not been an issue so far w/ .net runtime. Java is cool too, but I use .net because it's more integrated into windows. If I was in a unix or mac environment, I would have no choice but to use c++ apis foreign to me or java.

Anyways, c++ is still a pimpin language and I hope you enjoy it.
 
Back
Top