Reading Text File with Delimiter.

closed account (S37X216C)
So the file contains integers, string, and a delimiter ','.
It looks like this in the file

1234, Whitney Frost, 10, 11, 12, 13, 14

There are 10 students and 5 test scores for each student.
Please help!

Create 4 arrays: a 1-D array to store the students’ id, 1-D array to store the students’ names, a parallel 2-D array to store the test scores, and a parallel 1-D array to store letter grades.
Read data into the 3 arrays from the file data121.txt. If the file is not found, the program needs to terminate with this error, “Data file not found.”
Calculate and store a letter grade into a 1-D array based on the total score, sum up all scores for each student, for example if the total score is 82, then the grade is a B.
Score Letter grade
90 ~ 100 A
80 ~ 89 B
70 ~ 79 C
60 ~ 70 D
0 ~ 60 F
Once the data has been read into the arrays, implement the following 3 steps.
Display all students’ names, scores, and their grades,
Calculate and display a student name that received overall maximum points,
Calculate and display the average of all elements in the array test scores,
Generate a new text file report121.txt and save students’ names and grades.
Last edited on
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.

Look at getline: http://www.cplusplus.com/reference/string/string/getline/
closed account (S37X216C)
Well I have,
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
const int STUDENT_INFO=10;
int studentIds[STUDENT_INFO];
string studentNames[STUDENT_INFO];
const int ROWS=10;
const int COLS=5;
int studentScores[ROWS][COLS];
char letterGrades[5] = {'A', 'B', 'C', 'D', 'F'};

ifstream inputFile;
inputFile.open("data121.txt");
if(!inputFile){
cout << "Error.\n";
}
else{
for(int i=0; i<STUDENT_INFO; i++){
getline(inputFile, studentIds[i], '.');
inputFile >> studentIds[i];
cout << studentIds[i] << endl;
}
}
But when I run it the error is in the line where getline is at, saying that the type mismatched.
Last edited on
You do have:
1
2
3
4
int studentIds[STUDENT_INFO];

ifstream inputFile;
getline( inputFile, studentIds[i], '.');

where studentIds[i] is clearly int.

The available getline function is:
istream& getline ( istream& is, string& str, char delim );

The int is not string&. A mismatch.


Question of logic: Is the student ID a number or a word? Does it have to be a number?
In other words, you can decide to go around that problem.


The scores are numbers. Again, there are two options:
A: Read number into string and then convert it into int.

B: Read directly to int ( inputFile >> score; ) and then handle the comma.
( if stream has 12, 13, 14 and you use the >>, then score==12 and stream has still , 13, 14)
Topic archived. No new replies allowed.