-1

I am trying to read this data file into a struct. however i am having trouble doing so. here is the code i have so far. the part i am having trouble with is the readData function

#include<iostream>
#include<fstream>
#include<sstream>
#include<cstring>
#include<string>

using namespace std;

const int CAP = 100;
const int MAX_CHAR = 31;
int totalLines = 0;
struct Student
{
    char name[31];
    int score[10];
};

int readData(ifstream& iFile, Student list[]);
void findLowestEach(Student list[], int size);
void findHighestEach(Student list[], int size);
void calcAverage(Student list[], int size);

int main()
{
    Student list[CAP];

    ifstream inFile;

    inFile.open("data.txt");
    if (!inFile.is_open()) {
        cout << "Error: File cannot be opened.\n";
        exit(1);
    }
    readData(inFile, list);

    // calls all of the other functions
    //readData(inFile, list[]);
    //findLowestEach(list[], size);
    //cout << endl;
    //findHighestEach(scores, names);
    //cout << endl;
    //calcAverage(scores, names);
}

int readData(ifstream& iFile, Student list[])
{
    int i = 0;
    while (!iFile.eof())
        iFile >> list[i].name;
        for (int i = 0; i < 10; i++)
        {
            iFile >> list[i].score;
        }
    return 0;
}

here is the data file

Bob 56 67 83 76 84 94 68 86 78 56
John 76 89 95 64 78 34 99 89 104 98
Joe 65 68 89 78 45 69 98 101 99 89
Amy 86 77 89 76 94 54 78 89 78 66
Nick 76 69 95 94 78 64 99 89 110 88
Alex 95 88 89 78 95 69 88 101 99 89
Marga 96 67 89 76 64 94 98 83 78 56
Mike 76 89 95 64 78 34 99 89 104 98
Bella 85 68 89 78 45 69 98 101 99 89
Priya 86 77 89 76 94 94 78 89 78 96
Karen 78 69 95 94 78 94 99 89 110 88
Amit 95 88 79 78 95 89 88 101 99 89

Just hoping for a push in the right direction for reading in the data file. I think that I am sort of on the right track but im not sure

273K
  • 29,503
  • 10
  • 41
  • 64
jpeter
  • 3
  • 1
  • 6
    It seems you missed { and } around the while loop body. And read it https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – 273K Dec 02 '19 at 05:39
  • What sort of "trouble"? (Did your computer explode? Unlikely, but at the same time definitely trouble.) – JaMiT Dec 02 '19 at 06:20

1 Answers1

2

I'm not sure what exactly problem do you have. But if you want to write this code in C++, my suggestions are:

  1. Don't use char name[31] in student struct. You should use std::string. If you are using C style strings, you have to manage by you own to handle copying memory and to be sure that array is big enough to handle all data in input. With std::string you don't have this problems and operator>> will work correctly.

  2. I suggest to use std::vector as a container of you students struct. As it is standard library container you may use it in other functions from standard library to sort or find some elements.

Working code is below.

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <string>
#include <vector>

using namespace std;

static constexpr int CAP = 100;

struct Student
{
    std::string name{};
    int score[10] = {};
};

std::vector<Student> readData(ifstream& iFile);
void findLowestEach(Student list[], int size);
void findHighestEach(Student list[], int size);
void calcAverage(Student list[], int size);
void printStudents(const std::vector<Student> &a_vector);

int main()
{
    ifstream inFile;

    inFile.open("text.txt");
    if (!inFile.is_open()) {
        cout << "Error: File cannot be opened.\n";
        exit(1);
    }
    printStudents(std::move(readData(inFile)));
}

void printStudents(const std::vector<Student> &a_vector) {
    for(const auto & s: a_vector) {
        std::cout << s.name << " ";
        int i = 0;
        while( i < 10) {
            std::cout << s.score[i] << " \n";
            i++;
        }
        std:: cout << std::endl;  
    }

}

std::vector<Student> readData(ifstream& iFile)
{
    std::vector<Student> students;

    int i = 0;
    while (!iFile.eof()) {
        Student student{};
        iFile >> student.name;
           for (int i = 0; i < 10; i++)
            {
                iFile >> student.score[i];
            }
            students.push_back(student);
    }
    return students;
}

My conclusion is, that if you are using C++ please use standard library containers and algorithms. This will make writing code easier for you.

GoRo3
  • 161
  • 5