Need help with reading a text file and displaying it.

My objective is to read a text file that looks like this,

3450, Guido VanRossum, 10, 15, 14, 18, 20
6120, Yukihiro Matsumoto, 10, 9, 13, 12, 14
9230, James Gosling, 10, 16, 13, 12, 10

The problem is that when I run the program this is the output,

Guido VanRossum 15 18
6120 Matsumoto 9 12
9230 Gosling 16 12

Here is what I have,
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int STUDENT_INFO = 10;
const int ROWS = 10;
const int COLS = 5;
const char LETTER_GRADE = 5;

int main(){
string studentIds[STUDENT_INFO];
string studentNames[STUDENT_INFO];
string studentScores[ROWS][COLS];
char studentGrades[LETTER_GRADE];
ifstream inputFile;
inputFile.open("data121.txt");
if(inputFile){
for(int i=0; i<STUDENT_INFO; i++){
inputFile >> studentIds[i];
getline(inputFile, studentIds[i], ',');
cout << studentIds[i];
inputFile >> studentNames[i];
getline(inputFile, studentNames[i], ',');
cout << studentNames[i];
for(int j=0; j<COLS; j++){
inputFile >> studentScores[i][j];
getline(inputFile, studentScores[i][j], ',');
cout << studentScores[i][j];
inputFile.ignore(2);
}
}
}
else{
cout << "Data file not found.\n";
}
}


I noticed the pattern from the output but I can't find where the error is.
Any help is appreciated, thanks!
Last edited on
First you seem to be reading the same variables more than once in each loop. Remember that the extraction operator and getline() do basically the same operation.

Also have you studied structures or classes yet. This assignment could really use a structure or class to encapsulate the data for each student.

This
inputFile >> studentIds[i];
getline(inputFile, studentIds[i], ',');

and this
inputFile >> studentNames[i];
getline(inputFile, studentNames[i], ',');

and this
inputFile >> studentScores[i][j];
getline(inputFile, studentScores[i][j], ',');

are doing the same thing.


Last edited on
I see,
Thanks for the help!
Last edited on
Topic archived. No new replies allowed.