Help with homework problem???

The problem I'm working is writing a function that will accept 10 lines of user input, store the ten lines as ten individual c-strings, I'm supposed use a pointer array.  I'm having trouble actually assigning the input to my array?  Any help or guidance is appreciated.  Note:  I want help to learn, not an answer to the program.  Thanks!
#include <iostream>
#include <cstring>
using namespace std;
 
 
 
int main()
{
	int const NUMELS = 81;
	char *myArray[10];
	char myBuffer[810];
	char message[NUMELS];
	int i, j, pointerAddr, counter = 0;
	
 
	for (i = 0; i < 810; i+=81){
		myArray[counter++] = &myBuffer[i];
				
	}
	
	for (j = 0; j < 10; j++){
		cout << "Please enter a string: ";
		cin.getline(message, 81);
		myBuffer[i] = message[i];
		
		
	
	
	}
	cin.ignore(1);
	return 0;
 
 
 
 
 
 
 
}

Open in new window

jasonbrandt3Asked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
Oh, and assigning the entered text requires copying it, e.g.
        for (j = 0; j < 10; j++){
                cout << "Please enter a string: ";
                cin.getline(message, 81);
                strcpy(myArray[i],message);
        }

Open in new window

0
 
jkrCommented:
Well, first of all, you only have 10 elements in 'myArray' but are initilizing 810 elements. That can't be what you want ;o)

Then, notice that they all point to the same buffer, just a little 'shifted'. That's also not what you want. You could either dynamically allocate memory or use a 2d array. The 1st approach would be like
        for (i = 0; i < 10; i++){
                myArray[i] = new char[NUMELS + 1];                     
        }
 
// ...
 
// delete the memory before exiting
 
        for (i = 0; i < 10; i++){
                delete [] myArray[i];                     
        }

Open in new window

0
 
emceCommented:
firstly, think what i is in the for loop.
secondly you probably want to copy the characters from message into the right place in mybuffer.
0
 
jasonbrandt3Author Commented:
I got it!!!  the strncopy is what I was missing.  I'm using strncopy to populate myArray which indirectly copies to myBuffer.
0
 
Infinity08Commented:
>> Well, first of all, you only have 10 elements in 'myArray' but are initilizing 810 elements.

That's not exactly true ... The increment was i += 81, not ++i or something similar.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.