-1

I ran into an problem while working on my school assignment. I have an input file from which I should construct objects of a structure.

int student_count = 0;
struct Student {
  string name;
  string surname;
  int ID;
  vector<int> grade;
} students[50];

int main() {
  ifstream inp;
  inp.open("PODACI.txt");

  while (!inp.eof()) {
    static int i = 0;
    inp >> students[i].name >> students[i].surname >> students[i].ID;
    student_count++;
    i++;
  }

  for (int i = 0; i < student_count; i++) {
    cout << students[i].name << students[i].surname << students[i].ID;
  }
}

This is what I have done so far. It takes in the first line as the name, second line as the ID, and the third line is an comma-separated array of numbers in the file, I want to take that as an input and store as an vector of the struct.

##Look of input file.txt
John Doe
1542
5,6,4,7,10

Note: There are multiple students in the input file, first three lines of file is first student, next three lines second student and so on...

mortex
  • 3
  • 2
  • There's something missing from the specific question that you posted to Stackoverflow. That would be a specific question, and some people think that a specific question is the most important part of a specific question. You have described your homework assignment, but forgot to ask a specific question. What is your specific C++ question? – Sam Varshavchik Apr 25 '21 at 16:16
  • [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) – Some programmer dude Apr 25 '21 at 16:17
  • Sorry, this is my first post and I'm not very experienced. In the nutshell, I want to take the line from while which is comma-separated array of numbers, and store it into the "vector grade". – mortex Apr 25 '21 at 16:20
  • Post the input here as text, not as an image. – PaulMcKenzie Apr 25 '21 at 16:23
  • @PaulMcKenzie Done. – mortex Apr 25 '21 at 16:27
  • Read the entire last line as a single string and process that string appropriately. That's what you should have done at a high-level. How to process that string once read in is what you should focus on, and not the actual reading in of the string. Basically then the focused question would be "I have a string with comma separated numbers -- how do I parse this string?", and no need to show file reading code or anything irrelevant. – PaulMcKenzie Apr 25 '21 at 16:28
  • @PaulMcKenzie Got it, thank you. – mortex Apr 25 '21 at 16:34

1 Answers1

0

First of all, as was mentioned in a comment under your post, you shouldn't use while (!inp.eof()), use while (inp >> yourVariable(s)) instead, you can read about that in the mentioned link.

Second, from you input file example you stuck with the grades part, I guess there are two problems you can't overcome

  1. You don't know how many grades student will have
  2. Even if you know, they are separated with commas and you can't just do inp >> grade as many times as you wish

To solve this problem you can do the following, read the last line as a string using std::getline, then split it by , (here is an example of how to do it). After that, you'll be able to push_back all your grades to the corresponding vector member.