[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with homework problem???

Posted on 2008-11-14
5
Medium Priority
?
197 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:jasonbrandt3
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 22962590
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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 22962599
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
 
LVL 3

Expert Comment

by:emce
ID: 22962657
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
 

Author Closing Comment

by:jasonbrandt3
ID: 31516895
I got it!!!  the strncopy is what I was missing.  I'm using strncopy to populate myArray which indirectly copies to myBuffer.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22963494
>> 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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question