#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0

    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. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0
    I have having trouble coding the information that dwise1_aol give me. What would the coding of the main module look like
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    It sounds like you're trying to get someone to do your homework for you. :)
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by wizard2468
    I have having trouble coding the information that dwise1_aol give me. What would the coding of the main module look like
    Show us what you're having trouble with, then we can tutor you.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    3
    Rep Power
    0
    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
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally posted by wizard2468
    For the record this is not homework. I am learning C++ on my own with the books you can buy at any bookstore.

    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.

    Originally posted by wizard2468
    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
    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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    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];
    }
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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!].
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    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/

IMN logo majestic logo threadwatch logo seochat tools logo