How to turn off getline?

I have an assignment where I need to extract data from a file using getline and write it to a new file. I got that to work, but now I can ONLY extract string variables using getline. I need to extract int and double variables from the file that come after the string but no matter what I put after getline, it gives me a random number whenever I try to extract and fout non-string variables.

What do I need to put after the last getline or fin.ignore so I can start reading int and double variables from the file again? I can't find anything in my textbook or on the forum about what to do once you're done with getline.

Thanks!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  #include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

    string Name, Add, Ph, SSN;
    fstream TestFiles;
    ifstream fin;
    int age;

    TestFiles.open("TestFiles.txt");

    ofstream fout;
	fout.open("Student_Data.txt");

    fin.ignore();
    getline(TestFiles, Name);
    fin.ignore();
    getline(TestFiles, Add);
    fin.ignore();
    getline(TestFiles, Ph);
    fin.ignore();
    getline(TestFiles, SSN);
    fin.ignore();




    fout << Name << endl;
    fout << Add << endl;
    fout << Ph << endl;
    fout << SSN << endl;
    fout << age << endl;


    TestFiles.close();
    fout.close();


    return 0;
}
Here's the reference for std::getline(): http://www.cplusplus.com/reference/string/string/getline/
As you can see it always reads into std::string
To go from getline() to int, double etc you'd need std::istringstream
Here's a reference, particulary the 3rd reply in the thread:
http://stackoverflow.com/questions/10039383/how-do-i-use-getline-and-stringstream-to-parse-formatted-date-and-time-input
Okay so I kind of see what's going on in the second link with stringstream. So would I have to getline every int and double as a string first then convert it?

This is what my input file looks like:
Peter Griffin
101 Spooner St.
5125555555
123456789
25
3
88
80
96
86
88



25 and 3 are int, rest are double. I don't think I can tweak this too much without affecting my grade (otherwise I would have solved this hours ago by putting the int/doubles first and strings last and then just sorted them out as I wrote them to a new file) but I could probably get away with putting the same type of variables on one line. So my input would be:

Peter Griffin
101 Spooner St.
5125555555
123456789
25 3
88 80 96 86

So after my last getline I would put something like...?

char dummy; //to read the spaces?

stringstream lineStream(line);

getline(TestFiles, ints);
fin.ignore();

lineStream >> age >> dummy >> years >> dummy;

getline(TestFiles, testGrades);
fin.ignore();


lineStream >> T1 >> dummy >> T2 >> dummy >> T3 >> dummy ... etc?


And that will work if I have
int age, years;
double T1, T2, T3;

right?
Sweet, I got it to work with my int variables. Doubles should be pretty similar. Thank you so much!
Topic archived. No new replies allowed.