4 Replies - 305 Views - Last Post: 07 December 2012 - 06:51 AM Rate Topic: -----

#1 rjcast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-November 12

array of structures task

Posted 06 December 2012 - 01:32 PM

Im working on an assignment and we have to modify the code to be an array of structures and im finding this difficult as everything i try doesnt seem to work at all. To be clear about this im not asking anyone to do this for me, i just need some guidance and place to start from.

This is the full code and a test file. Any help is appreciated.
Test data:
Zoob,Don 7788 m 77 88 99
Shool,Dong 8866 F 68 98 91
Ropee,Naz 7543 f 44 55 66
Loop,Roop 9921 M 77 73 70



  
# include <fstream>
# include <iomanip>
# include <string>
# include <cmath>
# include <cctype>

using namespace std;


enum gradeType {A,B,C,D,F};

struct ExamData
{
	float e1;
	float e2;
	float e3;
	int examAvg;
	int totA;
	int totB;
	int totC;
	int totD;
	int totF;
	float highest;
	float lowest;
	gradeType stuGrade;
};

struct StuData
{
	string name;
	int id;
	char sex;
	int totFe;
	int totMa;
	float perFe;
	float perMa;
	ExamData exams;
};
StuData student;

const int STUIDMIN = 1111;
const int STUIDMAX = 9999;
const int EXAMMIN = 0;
const int EXAMMAX = 100;
const int MAXA = 100;
const int MINA = 90;
const int MAXB = 89;
const int MINB = 80;
const int MAXC = 79;
const int MINC = 70;
const int MAXD = 69;
const int MIND = 60;
const int MAXF = 59;
const int MINF= 0;
const char SEXF = 'F';
const char SEXM = 'M';
const int MAX_SIZE = 10;

void printHead(ofstream&);

char sexUpper(StuData);

void printData(ofstream&, StuData);

bool validData(StuData);

int avg(StuData);

gradeType grade(StuData&);

int sexCount(StuData&);

void examHighLow(StuData&);

void printAvgAndGrade(ofstream&,StuData);

void printInvalidData(ofstream&);

void printNoValidData(ofstream&);

void printStat(ofstream&, StuData);

int main()
{
  ifstream inFile;
  ofstream outFile;

  inFile.open("in.data");
  outFile.open("out.data");

  if(outFile.fail() || inFile.fail())
   {
    outFile << "Files have failed, check for errors." << endl;
    return 1;
   }

  outFile.setf(ios::fixed);
  outFile.setf(ios::showpoint);
  outFile.precision(2);

  printHead(outFile);

  student.exams.totA = 0;
  student.exams.totB = 0;
  student.exams.totC = 0;
  student.exams.totD = 0;
  student.exams.totF = 0;
  student.exams.highest = -1;
  student.exams.lowest = -1;
  student.totMa = 0;
  student.totFe = 0;
  student.perFe = 0;
  student.perMa = 0;

  inFile >> student.name >> student.id >> student.sex >> student.exams.e1 >> student.exams.e2 >> student.exams.e3;

  while (inFile)
  {
    student.sex = sexUpper(student);

    printData(outFile, student);

    if (validData(student))
    {

      student.exams.examAvg = avg(student);

      student.exams.stuGrade = grade(student);

      sexCount(student);

      examHighLow(student);

      printAvgAndGrade(outFile, student);
    }

    else
      printInvalidData(outFile);

    inFile >> student.name >> student.id >> student.sex >> student.exams.e1 >> student.exams.e2 >> student.exams.e3;
  }

  if(student.exams.highest + student.exams.lowest == -2)

    printNoValidData(outFile);


  else
    printStat(outFile, student);

  inFile.close();
  outFile.close();

  return 0;
}

 void printHead(ofstream& outFile)
 {
   outFile << "~~Stud Exam Report~~" << endl;
   outFile << left << setw(15) << "Name" << setw(15) << "Stud ID" << setw(10) << "Sex"
           << setw(10) << "Exam 1" << setw(10) << "Exam 2" << setw(10) << "Exam 3"
           << setw(15) << "Avg of Exams" << setw(10) << "Grade" << endl;
   outFile << left << setw(15) << "----" << setw(15) << "-------" << setw(10) << "---"
           << setw(10) << "------" << setw(10) << "------" << setw(10) << "------"
           << setw(15) << "------------" << setw(10) << "-----" << endl;
 }

 char sexUpper(StuData student)
 {
   return toupper(student.sex);
 }

 void printData(ofstream& outFile, StuData student)
 {
    outFile << left << setw(15) << student.name << setw(15) << student.id << setw(10) << student.sex
            << setw(10) << student.exams.e1 << setw(10) << student.exams.e2 << setw(10) << student.exams.e3;
 }

 bool validData(StuData student)
 {
   if((student.id >= STUIDMIN && student.id <= STUIDMAX)&& (student.sex == SEXF || student.sex == SEXM) &&
      (student.exams.e1 >= EXAMMIN && student.exams.e1 <= EXAMMAX) &&  (student.exams.e2 >= EXAMMIN && student.exams.e2 <= EXAMMAX) &&
      (student.exams.e3 >= EXAMMIN && student.exams.e3 <= EXAMMAX))

     return true;

   else
     return false;
 }

 int avg(StuData student)
 {
   return ceil((student.exams.e1 + student.exams.e2 + student.exams.e3)/3);
 }

 gradeType grade(StuData& student)
 {
   switch (student.exams.examAvg)
   {
      case 90 ... 100:
      {
        student.exams.totA++;
        return A;
	break;
      }
      case 80 ... 89:
      {
        student.exams.totB++;
        return B;
        break;
      }
      case 70 ... 79:
      {
        student.exams.totC++;
        return C;
	break;
      }
      case 60 ... 69:
      {
        student.exams.totD++;
        return D;
	break;
      }
      case 0 ... 59:
      {
        student.exams.totF++;
        return F;
	break;
      }
   }
 }

 int sexCount(StuData& student)
 {
   if(student.sex == 'F')
    return student.totFe++;
   else
     return student.totMa++;
 }

 void examHighLow(StuData& student)
 {
   if(student.exams.highest == -1)
     student.exams.highest = student.exams.e1;
   else if (student.exams.highest < student.exams.e1)
     student.exams.highest = student.exams.e1;
   else if (student.exams.highest < student.exams.e2)
     student.exams.highest = student.exams.e2;
   else if (student.exams.highest < student.exams.e3)
     student.exams.highest = student.exams.e3;

   if(student.exams.lowest == -1)
     student.exams.lowest = student.exams.e1;
   else if (student.exams.lowest > student.exams.e1)
     student.exams.lowest = student.exams.e1;
   else if(student.exams.lowest > student.exams.e2)
     student.exams.lowest = student.exams.e2;
   else if(student.exams.lowest > student.exams.e3)
     student.exams.lowest = student.exams.e3;

 }

 void printAvgAndGrade(ofstream& outFile, StuData student)
 {
   switch(student.exams.stuGrade)
   {
     case A:
     {
       outFile << setw(15) << student.exams.examAvg;
       outFile << setw(10) << "A" << endl;
       break;
     }
     case B:
     {
       outFile << setw(15) << student.exams.examAvg;
       outFile << setw(10) << "B" << endl;
       break;
     }
     case C:
     {
       outFile << setw(15) << student.exams.examAvg;
       outFile << setw(10) << "C" << endl;
       break;
     }
     case D:
     {
       outFile << setw(15) << student.exams.examAvg;
       outFile << setw(10) << "D" << endl;
       break;
     }
     case F:
     {
       outFile << setw(15) << student.exams.examAvg;
       outFile << setw(10) << "F" << endl;
       break;
     }
   }
}

 void printInvalidData(ofstream& outFile)
 {
    outFile << setw(15) << "~~Invalid Data~~" << endl;
 }

 void printNoValidData(ofstream& outFile)
 {
   outFile << endl;
   outFile << "There no valid data." << endl << endl;
   outFile << "*< end >*" << endl;
 }
 void printStat(ofstream& outFile, StuData student)
 {
   student.perFe = (float(student.totFe) / float (student.exams.totA + student.exams.totB + student.exams.totC + student.exams.totD + student.exams.totF)) * 100;
   student.perMa = (float(student.totMa) / float (student.exams.totA + student.exams.totB + student.exams.totC + student.exams.totD + student.exams.totF)) * 100;


   outFile << endl;
   outFile << ">>Total A: " << student.exams.totA << endl;
   outFile << ">>Total B: " << student.exams.totB << endl;
   outFile << ">>Total C: " << student.exams.totC << endl;
   outFile << ">>Total D: " << student.exams.totD << endl;
   outFile << ">>Total F: " << student.exams.totF << endl << endl;

   outFile << ">> " << "Max of all exams: " << student.exams.highest << endl;
   outFile << ">> " << "Min of all exams: " << student.exams.lowest << endl << endl;
   outFile << ">> " << "Percent of Females: % " << student.perFe << endl;
   outFile << ">> " << "Percent of Males: % " << student.perMa << endl << endl;
   outFile << "*< end >*" << endl << endl;
 }









Is This A Good Question/Topic? 0
  • +

Replies To: array of structures task

#2 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: array of structures task

Posted 06 December 2012 - 01:40 PM

I am not sure of what you are asking, if you could state it more clear, it would be better :)
Was This Post Helpful? 0
  • +
  • -

#3 rjcast  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 17-November 12

Re: array of structures task

Posted 06 December 2012 - 02:02 PM

I need help to convert the program posted to use an array of structure but i just dont know where to start.
Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: array of structures task

Posted 07 December 2012 - 03:32 AM

You see the code is just for one student, it is working with one struct which has name, id, grades and stuff...
You have 1 struct type variable:
039	StuData student;


If you want to have it, say for 10 students, you can:
StuData student[10];



Then you can work with each student like this:
student[0].id = 4;



It would be more easier if you get the students info and stuff using a loop...
Was This Post Helpful? 2
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 12,681
  • Joined: 16-October 07

Re: array of structures task

Posted 07 December 2012 - 06:51 AM

Your code is rather confused. You have final stats stored with student data and then student data holding summary data. I'd go with:
struct StuData {
	string name;
	int id;
	char sex;
	float exams[3];
};



That is a data item in your file. To read the file into an array, you'd do something like:
const int MAX_SIZE = 20;

// return false if not read
bool read(istream &, StuData &student);

// return number read
int read(istream &, StuData students[]);

StuData students[MAX_SIZE];
int studentsSize = read(inFile, students);
//...



Once you have a list of student already stored, then you can do all calculations from that, and not bother storing anything else.

e.g.
float getHighest(StuData students[], int size);

//...
outFile << ">> " << "Max of all exams: " << getHighest(students, studentsSize) << endl;



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1