-
December 11th, 2012, 03:20 AM
#1
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);
}
-
December 11th, 2012, 03:47 AM
#2
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 ...
-
December 11th, 2012, 02:11 PM
#3
Re: reading and storing a sentence from a .txt file
how do i work the getline into the return statement?
-
December 11th, 2012, 03:07 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|