Inputting text from a text file into a 2 -D array

Discussion in 'C++' started by saltedcoffee, Apr 5, 2006.

  1. saltedcoffee

    saltedcoffee Guest

    hey, I am beginer to C++ programing.
    I am working on a project called the "maze Problem"
    First of all I am required to read a text file in( which is the maze)
    and store it into a 2 dimentional vector.
    This is what I have till now:

    #include <iostream>
    #include "maze.h"
    #include <vector>
    #include <fstream>

    using namespace std;

    int main()
    {
    typedef vector< vector<char> > vectorMaze;

    //get the name of the file

    ifstream mazeFile;
    cout<< "Enter name of file name with the maze: "<<endl;
    while(!mazeFile.eof())
    {
    getline(cin,vectorMaze);
    }

    I feel there are errors with the above attempt too, but I also don't
    know how to put this input into a 2-d vector.
    Any help would be appreciated.
    Thanks......
    saltedcoffee, Apr 5, 2006
    #1
    1. Advertising

  2. saltedcoffee

    BobR Guest

    saltedcoffee wrote in message
    <>...
    >hey, I am beginer to C++ programing.
    >I am working on a project called the "maze Problem"
    >First of all I am required to read a text file in( which is the maze)
    >and store it into a 2 dimentional vector.
    >This is what I have till now:
    >
    >#include <iostream>
    >#include "maze.h"
    >#include <vector>
    >#include <fstream>
    >using namespace std;
    >
    >int main(){
    > typedef vector< vector<char> > vectorMaze;


    Hint: Why 'typedef' something you never use?

    > //get the name of the file
    > ifstream mazeFile;


    Hint: Why declare a stream you never use?

    > cout<< "Enter name of file name with the maze: "<<endl;
    > while(!mazeFile.eof()){
    > getline(cin,vectorMaze);
    > }


    Where is the end of main()?

    >
    >I feel there are errors with the above attempt too, but I also don't
    >know how to put this input into a 2-d vector.
    >Any help would be appreciated.
    >Thanks......


    You are very lost. Read this FAQ and get a book. While you are
    looking/waiting for the book, download 'TiCPP'.

    http://www.parashift.com/c -faq-lite/

    Get "Thinking in C++", 2nd ed. Volume 1 (&2) by Bruce Eckel
    (available for free here. You can buy it in hardcopy too.):
    http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

    --
    Bob R
    POVrookie
    BobR, Apr 5, 2006
    #2
    1. Advertising

  3. saltedcoffee

    Daniel T. Guest

    In article <>,
    "saltedcoffee" <> wrote:

    > hey, I am beginer to C++ programing.
    > I am working on a project called the "maze Problem"
    > First of all I am required to read a text file in( which is the maze)
    > and store it into a 2 dimentional vector.
    > This is what I have till now:
    >
    > #include <iostream>
    > #include "maze.h"
    > #include <vector>
    > #include <fstream>
    >
    > using namespace std;
    >
    > int main()
    > {
    > typedef vector< vector<char> > vectorMaze;
    >
    > //get the name of the file
    >
    > ifstream mazeFile;
    > cout<< "Enter name of file name with the maze: "<<endl;
    > while(!mazeFile.eof())
    > {
    > getline(cin,vectorMaze);
    > }
    >
    > I feel there are errors with the above attempt too, but I also don't
    > know how to put this input into a 2-d vector.
    > Any help would be appreciated.
    > Thanks......


    You need a Matrix class, and you need to start small and work your way
    up.

    #include <iostream>
    // put any extra includes you need here

    class Matrix {
    // put your code here
    };

    int main() {
    Maxrix matrix( 1, 1 ); // create a 1 by 1 cell matrix of char
    matrix.at( 0, 0 ) = 'x';
    assert( matrix.at( 0, 0 ) == 'x' );
    cout << "Working!\n";
    }

    Add code to the "put your code here" spot until you can get. The above
    to compile, and when run print "Working!" on the screen. Once you get
    that done, post what you have and we'll give you the next step.


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Apr 5, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?amFzb253b25n?=

    Inputting Text to the Runtime Parser

    =?Utf-8?B?amFzb253b25n?=, Jan 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    359
    =?Utf-8?B?amFzb253b25n?=
    Jan 13, 2006
  2. Replies:
    1
    Views:
    352
    William Park
    May 21, 2005
  3. Replies:
    3
    Views:
    275
    Jerry
    Nov 13, 2005
  4. Replies:
    1
    Views:
    336
    Tor Rustad
    Nov 30, 2007
  5. grocery_stocker

    Inputting data into a website question

    grocery_stocker, Apr 21, 2009, in forum: Python
    Replies:
    4
    Views:
    345
Loading...

Share This Page