String from a file

I have a text file that reads like:

Mike
Thomas
100 Main Street
1009
456
199
John
Smith
200 Second Ave
etc......

I can get the values I need but when I try to get the address it only gives me up to the space. Is there a way to get the entire line as one string ?

Code is below ignore the commented out stuff I'm still working on it.



#include <cstdlib>
#include <iostream>
#include <string>
#include "person.h"
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
Person person1;
Student student1;
ifstream infile;
string name, lastname, add;
float point, credit, pointin, creditin;
int idnumin, count = 0;

//infile.open("stData.txt");

if (!infile)
{
cout << " The input file does not exist. "
<< " Program Terminates. " << endl;
system("pause");
return 1;
}
infile.open("stData.txt",ios::in);
// do
// {
// infile >> name;
while (!infile.eof())
{
infile >> name;
infile >> lastname;
infile >> add;
infile >> idnumin;
infile >> creditin;
infile >> pointin;

student1.setfirstname(name);
student1.setlastname(lastname);
student1.setaddress(add);
student1.setidnum(idnumin);
student1.setcredits(creditin);
student1.setpoints(pointin);
point = student1.getpoints();
credit = student1.getpoints();
student1.setgpa(credit,point);
student1.display();
// count ++;
}
infile.close();//while( count < 2 );

system("PAUSE");
return 0;
}
[code]Your code goes here[/code]
Try getline from string or iostream
To get a full line and not just up to a space do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include <iostream>
#include <string>
#include <conio.h>

int main ()
{
           using namespace std;
           string sInput [100];
           int iNumberOfLinesInFile = 0; // set this to the number of lines you want to get
           fstream file;
           file.open ("YourFile.txt");
           for (int iLoopCounter = 0; iLoopCounter <= iNumberOfLinesInFile; iLoopCounter++)
           {
                       getline (file, sInput [iLoopCounter]);
           }
           for (int iLoopCounter = 0; iLoopCounter <= iNumberOfLinesInFile; iLoopCounter++)
           {
                       cout << sInput [iLooPCounter] << endl;
           }
           file.close ();
           getch ();
           return 0;
}
Topic archived. No new replies allowed.