How can I retrieve strings using getline and enter them into a two dimensional aray?

This is a discussion on How can I retrieve strings using getline and enter them into a two dimensional aray? within the C++ Programming forums, part of the General Programming Boards category; C++. For example the user enters the name Fionna Issac as one whole string. This string is then to be ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    How can I retrieve strings using getline and enter them into a two dimensional aray?

    C++. For example the user enters the name Fionna Issac as one whole string. This string is then to be entered into the two dimensional array seats[2][4].

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Two dimensional array of what? Characters? Strings?

    How is the string supposed to be split into that two dimensional array? According to the space between the first and last name?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I think the two dimensional array is irrelevant. Given the variable name "seats" it implies he has 2 rows by 4 columns of seats for the people read from the file.

    There's a getline example on the first Google result (http://www.cplusplus.com/doc/tutorial/files/):

    Code:
    // reading a text file
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string line;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
          cout << line << endl;
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }
    All you have to do is modify it so under getline you go seats[x][y] = line; x and y you can keep track of by incrementing them after seat assignment. eg.

    Code:
    const int MAX_ROWS = 2, MAX_COLUMNS = 4;
    int x = 0, y = 0;
    if(y < MAX_COLUMNS) { ++y; } else { if(x < MAX_ROWS) { ++x; y = 0; } else { std::cout << "No more seats available!" << std::endl; } }
    Too much information? Save yourself a ++post_count.
    Last edited by Dae; 12-23-2009 at 05:35 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,600
    Quote Originally Posted by Dae
    There's a getline example on the first Google result (http://www.cplusplus.com/doc/tutorial/files/):
    Oh dear, that is actually a bad example. By using eof() to control the loop in that way, control enters the loop body one more time than it should. The reason is that after the last line is read, eof() returns false. It is only when control enters the loop and yet another getline() is performed does eof() return true, but by then the cout << line << endl; statement has been executed when it should not. Rather, the loop should be:
    Code:
    while (getline(myfile, line))
    {
      cout << line << endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21