#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <string>

using namespace std;


const int MAX_SIZE = 60;


void discard_line(ifstream &in);
void print_student_records(structarray[], double scmarks[], double sfmarks[], int size) ;


int main()
{ 
    string id[MAX_SIZE];
    double cmarks[MAX_SIZE];
    double fmarks[MAX_SIZE];
    //char grade[MAX_SIZE];
    int choice;
    char response;

    int total_records;


    ifstream infile;
    cout << "This program shows student's grade."<< endl;
    cout<<endl;


    infile.open("studentData.txt");

    //check for error
    if(infile.fail())
    {
        cerr<<"Error opening file" << endl;
        system ("PAUSE");
        exit(1);
    }

    discard_line(infile);

    total_records = 0;

    while(infile>>id[total_records]>>cmarks[total_records] >> fmarks[total_records])
    {
        total_records++;
    }
     infile.close();

     print_student_records(id,cmarks,fmarks,total_records);

    system ("PAUSE");

    return 0;
}

void print_student_records(string sID[], double scmarks[], double sfmarks[], int size)
{
    cout<<"\n\nstudentID\tcoursework\tFinal exam"<<endl<<endl;
    for(int i = 0; i < size; i++)
    {
        cout<<sID[i]<<"\t\t"<<scmarks[i]<<"\t\t"<<sfmarks[i]<<endl;
    }

    cout<<endl;

}



void discard_line(ifstream &in)
{
    char c;

    do
        in.get(c);
    while (c!='\n');
}

how can i store data in a array of struct. i am confuse because i don't know how to use it in function??

Thanks

You don't have any structs. Are you trying to sort parallel arrays?

From what the OP is saying, I expect what they want is to replace

string id[MAX_SIZE];
double cmarks[MAX_SIZE];
double fmarks[MAX_SIZE];

with something like this:

struct student_grade
{
    string id;
    double cmarks;
    double fmarks;
};

student_grade students[MAX_SIZE];

This is sensible; not only does it structure the data better, it makes it feasible to sort the structs as a unit, keeping the data for each student together.

I think what they wanted to know is how to access the elements of the array of structs. Fortunately, this is quite easy:

students[this_record_index].id = "d3203032sd1";
students[this_record_index].cmarks = 82.3;
students[this_record_index].fmarks = 73.7;

Simple! I will however, give you a piece of advice: if at all possible, use a std::vector<> instead of a fixed array whenever there is any question of how large the array might have to become.

std::vector<student_grade> students;
student_grade* student;

while (infile.good() && !infile.eof())
{
    student = new student_grade();
    infile >> student.id >> student.cmarks >> student.fmarks;
    if (infile.error())
    {
        break;
    }
    students.push_back(student);
}

The std::vector<> template is more efficient memory-wise, and avoids the possibility of overrunning the array.

commented: Great advice! +15

ok thanks for the reply...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.