Hi guys just trying to understand Getline and implement it in a problem im trying to solve.
First off i have a program that is working:
Code:#include <iostream.h> #include <string> #include <fstream> using namespace std; void main() { ifstream fin("data.txt"); ofstream fout; char name[30]; char jersey_number[10]; char best_time[10]; char sport[40]; char high_school[40]; while(!fin.getline(name, 30, '|').eof()) { fin.getline(jersey_number, 10, '|'); fin.getline(best_time, 10); fin.getline(sport, 40, '|'); fin.getline(high_school, 40); cout << jersey_number << best_time << sport << high_school << endl; } }
This works fine with the input data text file containing the following:
Code:John|83|52.2 swimming|Jefferson Jane|26|10.09 sprinting|San Marin
Now the problem! I tried to learn from this and make a new program, which should do almost the same thing, read data from a text file, and cout the information i want.
The data in the text file is in a different format, for example:
Here is what i programmed, going from the first example:Code:firstName middleName surname 38 47 38 27 36 firstName middleName surname 84 37 29 34 72
Code:#include <iostream.h> #include <string> #include <fstream> using namespace std; void main() { ifstream fin("test.txt"); ofstream fout("output.txt"); char name[30]; char fullName[30]; char marks[30]; while(!fin.getline(name, 30, '\n').eof()) { fin.getline(fullName, 30, '\n'); fin.getline(marks, 30, '\n'); cout << fullName << " " << marks << endl; } }
All this does is bring up the program window which just runs forever, what am i doing wrong?
The different thing between the working program and the one that isnt working is that the working one uses "|" but 2nd one uses "\n" for end of each line i believe.
ideas anyone or help?



LinkBack URL
About LinkBacks



CornedBee