I got this exercise and i'am trying to read all the data from file, but it fails and throws junk values.
This is the data I'am trying to read:
7
Petras A. Petraitis 120 15 20 0
Jurgis Jurgutis 222 16 12 22
Rimas Jonas 138 15 15 59
Bei Lin Sin Mun 23 15 0 0
Zigmas Nosis 256 16 23 9
Romas Senasis 111 15 15 15
Jurgis Statys Lydeka199 16 13 9
First line is the amount of athletes starting 1 <= n <= 30.
in the next
n amount of lines are the data about athletes.
First 20 positions are athletes names. Then his identification number. And then his starting time: Hours, minutes and seconds.
And this is the code I wrote:
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 46
|
//Uzduotis: Slidininkai
#include <fstream>
#include <iostream>
using namespace std;
struct slidininkai{
char name[20]; // athlete names
int startNumb; // athletes identification numbers
int timeHours; // starting time: hours
int timeMin; // starting time: minutes
int timeSec;// starting time: seconds
};
int main()
{
slidininkai athletes[30];
fstream duom ("U2.txt");
int n; // starting athletes
duom >> n;
for (int i=0; i<n; i++){
for (int j=0; j<20; j++){
duom >> athletes[i].name[j];
}
duom >> athletes[i].startNumb;
duom >> athletes[i].timeHours;
duom >> athletes[i].timeMin;
duom >> athletes[i].timeSec;
}
//cout << n << endl;
for (int i=0; i<n; i++){
for (int j=0; j<20; j++){
cout << athletes[i].name[j];
}
cout << " " << endl;
cout << athletes[i].startNumb << " ";
cout << athletes[i].timeHours << " ";
cout << athletes[i].timeMin << " ";
cout <<athletes[i].timeSec << endl;
}
return 0;
}
|
What wrong am I doing again?