0

Hey so ive been trying to fix this for quite a while now and I cant figure out what went wrong. I made the code to store the data from the files into the structure. When I did that and ran it I was able to input a file name, but after inputting it the screen didn't change, leaving the filename as the only output. Any suggestions on what went wrong, thank you for any help!

Here is the input text:

Smith Jr., Joe
111-22-3333
3
Physics I
A 5
English 1A
B 4
Chemistry 101
F 5

Here is the code:

#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

struct Student {
    string fullName;
    string idNumber;
    double gpa;
    int unit;
    char grade;
    string title;
};
bool openFile(ifstream & fin);
void Read_info(Student list[], ifstream & fin);
int main()
{
    Student empList[10];
    ifstream fin;
    if (openFile(fin)) {
        Read_info(empList, fin);
        cout << "2";
    }
    return 0;
}

bool openFile(ifstream& fin) {
    string fileName;
    getline(cin, fileName);
    fin.open(fileName);
    if (fin.fail()) {
        cout << "Bad file.\n";
        return false;
    }
    if (fin.peek() == EOF) {
        cout << "No data to process.\n";
        return false;
    }
    return true;
}
void Read_info(Student  list[], ifstream& fin) {

    getline(fin, list[0].fullName);
    getline(fin, list[0].idNumber);
    while (!fin.eof()) {
        
        for (int i = 0; i < 10; ++i) {
            getline(fin, list[i].title);
            fin >> list[i].grade >> list[i].unit;
            fin.ignore(10, '\n');
        }
    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Johnny
  • 11
  • 2
  • Visual Studio has an amazing debugger. Run the program in the debugger, get to where it freezes, paus the program, see where it is. Maybe step around for a while to see what has jammed. Don't forget to check out that callstack. Always good info to be found in the callstack. – user4581301 Nov 30 '20 at 03:57
  • Helpful reading: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) Son of a gun, that's pretty much the bug. In addition more checking of the stream state after transactions is necessary all-around. – user4581301 Nov 30 '20 at 03:59

0 Answers0