CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Dec 2012
    Posts
    2

    reading and storing a sentence from a .txt file

    Hi, im doing an assignment, and im a little stuck. Ive created a class that is supposed to store first name, last name, date of birth, date of death, and a fact about a person (all variables within the class). Im trying to fill these variables with a read function. it reads a .txt file like this

    Firstname Lastname 1987 1988 this guy did this

    the problem is, i dont know how to handle the last variable. the variable needs to hold the entire "this guy did this" sentence. i made it a string, just because i was clueless, and as expected, it only holds "this"

    this is my .h:

    #include <string>
    #include <fstream>
    #include <iostream>
    using namespace std;

    class Person
    {
    public:
    Person();
    Person(const Person & person);
    ~Person();

    void Set(const string Fname, const string Lname, const int Dob, const int Dod, const string Fact);
    void Get(string & Fname, string & Lname, int & Dob, int & Dod, string & Fact) const;
    void Print() const;
    bool Read(ifstream & input);

    private:
    int dob;
    int dod;
    string fname;
    string lname;
    string fact;
    };

    --------------------------------------------------------

    here is the read function in the .cpp:

    bool Person::Read(ifstream & input)
    {
    return (input >> fname >> lname >> dob >> dod >> fact);
    }

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    Re: reading and storing a sentence from a .txt file

    you can use getline( input, fact ).

    BTW, in Set() you should pass by const reference or by value ...

  3. #3
    Join Date
    Dec 2012
    Posts
    2

    Re: reading and storing a sentence from a .txt file

    how do i work the getline into the return statement?

  4. #4
    Join Date
    Apr 2008
    Posts
    725

    Re: reading and storing a sentence from a .txt file

    hint: you don't have to do everything on one line!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured