Thread: (C++) two dimensional char arrays and cin

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    2

    (C++) two dimensional char arrays and cin

    Code:
    	char buffer[3][char_length]={"hello",
    			    "why am I here",
    			    "the beyond consumes me"};
    would save 3 different strings in an array, and I would be able to access them..
    However, if I try to alter a string by doing:
    Code:
     cin.get(buffer[1], 23);
    it would wipe over the remaining strings. In other words, it would inkrement the first of the two index's (if you can call 'em that).

    Now I've been poking around with this the past few days and it's starting to get at me. Does anyone please know how I could go about doing this correctly ?
    And please explain why if you can too Would like to learn why I should do as I should.

    thanks in advance

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Well something like this works for me using cin.getline()
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    {
      const int char_length=100;
      char buffer[3][char_length]={"hello",
    			    "why am I here",
    			    "the beyond consumes me"};
      cin.getline(buffer[1],'\n');
      for(int i = 0;i<3;i++)
      {
        cout<<buffer[i];
      } 
      return 0; 
    }
    Now the only thing that you have to remeber is that you array starts at 0 not at 1
    Woop?

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    2
    thanks!
    Actually I do know that arrays start at 0. I had 3 sentences that looked like that one (one for each element) so I just copied a random one :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers with arrays, (*(pntr + 1) vs. *pntr[1])
    By RaisinToe in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2009, 12:40 PM
  2. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  3. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 07:54 PM
  4. cin and char arrays
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2003, 09:11 PM
  5. Char Arrays
    By Borommakot in forum C++ Programming
    Replies: 17
    Last Post: 09-28-2002, 01:58 PM