|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
two dimension array
I am tring to set up a 2x10 two dimensional array that holds two seperate strings (char firstName and char lastName) for the two columns and i want to store up to 10 people (rows). I want the user to enter in the names and be able to display the names after they are typed in. I am unsure how to set up the char array and I am unsure how to have the user enter the names. Any information that will help me code this will be thankful
|
|
#2
|
||||
|
||||
|
|
|
#3
|
||||
|
||||
|
I would think:
char *names[10][2]; // will need to dynamically allocate memory space for each name or char *names[10][2][15]; // static allocation, assuming no name over 14 letters In effect, you'll have a 3-dimensional array, when you figure that each element of your 2D array will itself be a 1D array. How the user will input the names depends in part on whether you're using C or C++. In C++, it seems popular now to use iostreams: cin >> firstName >> last Name; Then strcpy those strings into the appropriate elements of the array. Of course, if you are using the dynamic allocation approach, be sure to allocate one character more than the string length for the null terminator. |
|
#4
|
|||
|
|||
|
I have having trouble coding the information that dwise1_aol give me. What would the coding of the main module look like
|
|
#5
|
|||
|
|||
|
It sounds like you're trying to get someone to do your homework for you.
![]() |
|
#6
|
||||
|
||||
|
Quote:
Show us what you're having trouble with, then we can tutor you. |
|
#7
|
|||
|
|||
|
For the record this is not homework. I am learning C++ on my own with the books you can buy at any bookstore. The code that I have been working with is not right but here it is
#include <iostream.h> int main() { char name [10][20]; int x; for (x=0; x<10; ++x) { cout << "\nEnter the name: "; cin.getline(name,20) } cout << name; return 0; } but the cin.getline(name,20) developes an error so i changed it to #include <iostream.h> int main() { char name [10][20]; int x; for (x=0; x<10; ++x) { cout << "\nEnter a name: "; cin >> name ) cout << name; return 0; } With this code it allowed me to enter 10 names but it wou't display them |
|
#8
|
||||
|
||||
|
Quote:
Pardon our assumptions. It's just that we tend to get a lot of students looking for help with their homework. I don't mind tutoring, but I'm reluctant to just give an answer. If it just gets done for them, then they won't learn. Quote:
There was a discussion here, about a month ago, about a problem with getline(). IIRC, it has to do with how it handles (or doesn't quite handle) the newline. Now, since name is a two-D array and a string would only go into a 1-D array, you should do that: cin >> name[i]; And the same when you output it. You would probably either want to echo it out immediately or write a second for-loop to output the contents of the array. Preferably the latter, I would think. |
|
#9
|
|||
|
|||
|
char text [10][20];
text is the name of the whole conglomeration of parts that make up the array, so when you say: cin>>text; or cout<<text; you're not correctly accessing the storage containers that make up the array. The two dimensional char array you declared looks like this when filled with data: row1: aaaaaaaaaaaaaaaaaaa\0 (19 char's and a terminating \0) .... .... .... row10:aaaaaaaaaaaaaaaaaaa\0 So, what you want to do is input data into each row of the array. To do that, you must specify which row you want to read data into, e.g. cin>>text[0]; cin>>text[1]; ... ... ... ... cin>>text[9]; Typically, you would do that in a loop like this: for(int i=0; i<10; i++) { cin>>text[i]; } |
|
#10
|
||||
|
||||
|
One further note. Both the << and >> operators are overloaded, which means that they will run different code depending on the data-type/class being output or input. That is why they handle char* differently than int or double.
Your code as it stands is telling them to handle a char** . I personally have difficulty believing that the iostream library writer would have written a method to handle a char**, but apparently they did because it does compile (doesn't it?). Still, I have no idea how it would actually be trying to handle it. Unless the character data being input was being interpreted as a pointer [shudder!]. |
|
#11
|
|||
|
|||
|
the stream input operator won't play particularly nicely with char*. You really want to input to strings. So you might use something like this:
Code:
string name[10];
for(int i=0; i < 10; i++) {
cout << "Name: ";
cin >> name[i];
}
for(int j=0; j < 10; j++)
cout << name[j] << endl;
__________________
Clay Dowling Lazarus Notes Articles and commentary on web development http://www.lazarusid.com/notes/ |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > two dimension array |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|