-5

I am supposed to create a function which receives the arrays as parameters and which searches for the student with the highest average of the 3 grades and return to main the location of the student in the array.

The 4 arrays are: Student number(9 digits) Math grade Science grade English grade

And finally, it's supposed to read the data from the file into the array.

File data is:

123456789
60
70
80
987654321
70
80
90
999888777
75
85
65
111222333
55
65
75
444555666
63
73
83

I need help with how to read the file data using the arrays and functions.

This is what I have so far:

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

int findHighest(int Mgrade[5], int Sgrade1[5], int Egrade[5], long Snumber[5]);

main()
{
    int Mgrade[5], Sgrade[5], Egrade[5];
    long Snumber[5];

    char num_from_file;
    ifstream infile;
    char mystring[20];
    int grade;
    infile.open("testdata.txt", ios::in);

    if (infile)
    {

        cout<<"File opened successfully\n";
        {
            do
            {

                infile.get(mystring,21); //(example from other program)
                infile.ignore(80,'\n');// (what should go here instead)

                infile>> grade;//(example from other program)
                infile.ignore(80,'\n');// (what should go here instead)

                if (infile.eof())
                {
                    break;
                }

                cout<<mystring<<'\t'<<grade<<endl<<endl;
                //cout<<'\t'<<num_from_file<<endl;
            }
            while(1);
        }
    }
    else
    {
        cout<<"error opening file";
    }

    infile.close();

    return 0;
}
gsamaras
  • 66,800
  • 33
  • 152
  • 256

1 Answers1

1

Simply use getline(), like this:

#include <fstream>
#include <iostream>
#include <string>

int main(void) {
    int Mgrade[5], Sgrade[5], Egrade[5];
    long Snumber[5];
    std::ifstream input("testdata.txt");
    int index = 0, counter = 0;
    for( std::string line; getline( input, line ); ) {
        if(counter == 4) {
            counter = 0;
            index++;
        }
        if(counter == 0) {
            Snumber[index] = std::stol(line);
        } else if(counter == 1) {
            Mgrade[index] = std::stoi(line);
        } else if(counter == 2) {
            Sgrade[index] = std::stoi(line);
        } else if(counter == 3) {
            Egrade[index] = std::stoi(line);
        }
        counter++;
    }
    return 0;
}

The code reads the file (variable input in the code) line by line and uses a for loop to achieve that.

In every iteration of the loop, the variable line will contain the current line of the file that are we are reading. For example, in the first iteration, line will be equal to "123456789".

Now I use two integers, index and counter. The first is used to index the arrays, since we are reading the first student, we want to populate the first cell of every array (that would be an index equal to 0).

counter is used in order to keep track of how many lines are been read for the idnex-th student. We expect to read 4 lines for every student, so counter will be 0 when we read his number, 1 when we read his math grade, 2 when we read his science grade and 3 when we read his English grade. The counter gets increased by one, in the end of every iteration.

Now, when counter is equal to 4, that means that we should start reading data for the next student, thus we have to increment index by one, so that for example if we were reading data for the first student(index equal to 0), now we would need (index equal to 1).

Moreover, counter should be re-initialized to 0, so that we read his number and grades correctly, with the if-else statements.

gsamaras
  • 66,800
  • 33
  • 152
  • 256
  • [`std::stoi`](http://www.cplusplus.com/reference/string/stoi/) is defined in `string` header and is C++11 feature. It just converts a string to an integer, so you could implement it yourself or google it. However, I highly recommend to use a compiler that supports C++11 (since it's not that new). For example, with an old GCC, you can use `-std=c++0x` flag to enable the C++11 features, like this: `g++ prog.cc -Wall -Wextra -std=c++11`. @user8859795 – gsamaras Dec 06 '17 at 07:27
  • Yes you could it with [`atoi()`](http://www.cplusplus.com/reference/cstdlib/atoi/) too, although you should keep up with the new functions. Example: `#include ` and `atoi(line.c_str());`. Similary, with [`atol()`](http://www.cplusplus.com/reference/cstdlib/atol/), you would do `atol(line.c_str());`. – gsamaras Dec 06 '17 at 07:30
  • It's ok @user8859795. Well `std` is just a namespace, and these functions are defined in it, don't get confused about it now. I updated my answer with explanation, hope that helps. I insist on my solution over your, since it will help you learn a bit. =) – gsamaras Dec 06 '17 at 07:49
  • @user8859795 no this isn't the way Stack Overflow works. We post here, so that future users can see the solutions as well. – gsamaras Dec 06 '17 at 08:13
  • Well, given OP's input file, maybe a simple `int i = 0; while (i < 5 && input >> Snumber[i] >> Mgrade[i] ...) {... ++i; ...}` could be enough. – Bob__ Dec 06 '17 at 09:35