4 Replies - 760 Views - Last Post: 16 April 2013 - 11:05 PM Rate Topic: -----

#1 LionF2013   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-April 13

Array of Struct Question!

Posted 15 April 2013 - 12:41 PM

Hello, I am having a hard time understanding how to convert my current code to an array of struct. I have looked all over the internet and cannot find how to "properly" convert the code to an array of struct. Here is my current code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cctype>

using namespace std;

const int GRADE_COUNT = 10;

const int ID_LOW = 1111;
const int ID_HIGH = 9999;
const int EXAM_LOW = 0;
const int EXAM_HIGH = 100;
const int EXAM_PASS = 75;
const int LIMIT_A = 90;
const int LIMIT_B = 80;
const int LIMIT_C = 70;
const int LIMIT_D = 60;

  struct gradesCount
    {
    string studName;
    int studId;
    float exam1;
    float exam2;
    float exam3;
    char gender;
    char examGrade;
    };

//prototypes
void printHeading(ofstream& outFile);
void printRd(ofstream& outFile, gradesCount input);
bool validData(gradesCount input);
void readRec(ifstream& inFile, gradesCount& input);
float Average(gradesCount input);
void examGrades(float avg, char& examGrade, int& aCount, int& bCount, int& cCount, int& dCount, int& fCount);
void printAvgandGrade(ofstream& outFile, float avg, gradesCount input);
void printInvalidDataMsg(ofstream& outFile);
float meanOfAvg(float total, int validCount);
void printStatistic(ofstream& outFile, int validCounter, int aCount, int bCount,
                    int cCount, int dCount, int fCount, float total, int males, int females);
void MaxAndMinExam(float& examMax, float& examMin, float exam);

int main()
{

  ifstream inFile;
  ofstream outFile;

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

  gradesCount input;

  int males = 0;
  int females = 0;
  float avg;
  float examMax = 0;
  float examMin = 100;
  float total = 0;
  int validCounter = 0;
  int invalidCounter = 0;
  int count = 1;
  int aCount = 0;
  int bCount = 0;
  int cCount = 0;
  int dCount = 0;
  int fCount = 0;

  if(inFile.fail() || outFile.fail())
  {
    cout << "Check your files" << endl;
    return 1;
  }
  outFile.setf(ios::fixed);
  outFile.precision(2);

  printHeading(outFile);

  readRec(inFile, input);
  while(inFile)
  {
    printRd(outFile, input);

    if(validData(input))
    {
      validCounter++;
      avg = Average(input);
      total = total + avg;
      examGrades(avg, input.examGrade, aCount, bCount, cCount, dCount, fCount);
      printAvgandGrade(outFile, avg, input);
      MaxAndMinExam(examMax, examMin, input.exam1);
      MaxAndMinExam(examMax, examMin, input.exam2);
      MaxAndMinExam(examMax, examMin, input.exam3);
      void MaxAndMinExam(float& examMax, float& examMin, float exam);

      if(input.gender == 'M')
        males++;
      else
        females++;
   }
    else
    {
      invalidCounter++;
      outFile << setw(15) << left << "~~ Invalid data ~~"  << endl;
    }
    inFile >> input.studName >> input.studId >> input.gender >> input.exam1 >> input.exam2 >> input.exam3;
  } // end of while

    if(validCounter > 0)
     {
     total = meanOfAvg(total, validCounter);
     printStatistic(outFile, validCounter, aCount, bCount, cCount, dCount, fCount, total, males, females);
     }
   else
     {
      outFile << endl;
      outFile << "No valid data! No mean, max, min, grade count, or gender percentages ";
      outFile << "can be found!" << endl;
     }

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

return 0;
}

void printHeading(ofstream& outFile)
{
  outFile << "*~~< Stud Exam Report >~~*" << endl << endl;
  outFile << setw(15) << left << "Name" << left << setw(15) << left << "Stud Id"
          << setw(15) << left << "Sex" << setw(15) << left << "Exam 1"
          << setw(15) << left << "Exam 2" << setw(15) << left << "Exam 3"
          << setw(15) << left << "AVG of Exams" << setw(15) << left << "Grade" << endl;
  outFile << setw(15) << left << "----" << left << setw(15) << left << "-------"
          << setw(15) << left << "---" << setw(15) << left << "------"
          << setw(15) << left << "------" << setw(15) << left << "------"
          << setw(15) << left << "------------" << setw(15) << left << "-----" << endl;
}

bool validData(gradesCount input)
{
  return((input.studId >= ID_LOW) && ((input.gender == 'M') || (input.gender == 'F')) &&
         (input.studId <= ID_HIGH) && (input.exam1 >= EXAM_LOW) && (input.exam1 <= EXAM_HIGH) &&
         (input.exam2 >= EXAM_LOW) && (input.exam2 <= EXAM_HIGH) && (input.exam3 >= EXAM_LOW) &&
         (input.exam3 <= EXAM_HIGH));
}

void readRec(ifstream& inFile, gradesCount& input)
{
  inFile >> input.studName >> input.studId >> input.gender >> input.exam1 >> input.exam2 >> input.exam3;
}

float Average(gradesCount input)
{
  return(ceil((input.exam1 + input.exam2 + input.exam3)/3));
}

void printAvgandGrade(ofstream& outFile, float avg, gradesCount input)
{
 outFile << setw(15) << left << (int)avg << setw(15) << left << input.examGrade << endl;
}

float meanOfAvg(float total, int validCount)
{
  return(total / validCount);
}

void printInvalidDataMsg(ofstream& outFile)
{
  outFile << "~~ Invalid Data ~~" << endl;
}

void examGrades(float avg, char& examGrade, int& aCount, int& bCount, int& cCount, int& dCount, int& fCount)
{
  if (avg >= LIMIT_A)
  {
    examGrade = 'A';
    aCount++;
  }
  else if (avg >= LIMIT_B)/>
    {
    examGrade = 'B';
    bCount++;
    }
  else if (avg >= LIMIT_C)
    {
    examGrade = 'C';
    cCount++;
    }
  else if (avg >= LIMIT_D)
    {
    examGrade = 'D';
    dCount++;
    }
  else
    {
    examGrade = 'F';
    fCount++;
    }
  }

void printStatistic(ofstream& outFile, int validCounter, int aCount, int bCount,
                    int cCount, int dCount, int fCount, float total, int males, int females)
{
  float perMale;
  float perFemale;
  char t;

  perMale = ((float(males) / validCounter) * 100);
  perFemale = ((float(females) / validCounter) * 100);

  outFile << endl;
  outFile << "A|" << aCount;
  for (char t = 0; t < aCount; t++)
    {
      outFile << "***" << endl;
    }
      outFile << "B|" << bCount;
  for (char t = 0; t < bCount; t++)
    {
      outFile << "***";
    }
      outFile << endl;
      outFile << "C|" << fCount;
  for (char t = 0; t < cCount; t++)
    {
      outFile << "***";
    }
      outFile << endl;;
      outFile << "D|" << dCount;
  for (char t = 0; t < dCount; t++)
    {
      outFile << "***" << endl;
    }
      outFile << endl;
      outFile << "F|" << fCount;
  for (char t = 0; t < fCount; t++)
    {
      outFile << "***" << endl << endl;
    }
  outFile << setw(15) << left << "**Mean of AVG: " << total << endl;
  outFile << "**Number of Female: %" << perFemale << endl;
  outFile << "**Number of Male: %" << perMale << endl << endl;
  outFile << setw(15) << "*< end >*" << endl;
}

void printRd(ofstream& outFile, gradesCount input)
{
  outFile << setw(15) << left << input.studName << setw(15) << left << input.studId
          << setw(15) << left << input.gender << setw(15) << left << input.exam1
          << setw(15) << left << input.exam2 << setw(15) << left << input.exam3;
}

void MaxAndMinExam(float& examMax, float& examMin, float exam)
{
  if(exam > examMax)
    examMax = exam;

  if(exam < examMin)
    examMin = exam;
}



Thank you very much to anyone that can help!

Is This A Good Question/Topic? 0
  • +

Replies To: Array of Struct Question!

#2 jimblumberg   User is offline

  • member icon

Reputation: 5782
  • View blog
  • Posts: 17,685
  • Joined: 25-December 09

Re: Array of Struct Question!

Posted 15 April 2013 - 01:06 PM

Do you understand how to fill in the values of a single structure?

When I'm trying to learn something new, I usually start with a new program that will just use the new methods. For this I would start with a single simple structure, fill in the values of the structure and print the structure contents. Once you know how to do a single instance then moving to arrays or vectors should be simple.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 LionF2013   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-April 13

Re: Array of Struct Question!

Posted 15 April 2013 - 02:38 PM

I understand how to fill the values of the structure I created in my original code. as it included everything except the printStatistic function. I guess I'll try looking at your "Functions in C and C++, Part II: Argument Passing and Arrays" link under your message.
Was This Post Helpful? 0
  • +
  • -

#4 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Array of Struct Question!

Posted 16 April 2013 - 10:51 PM

View PostLionF2013, on 15 April 2013 - 04:38 PM, said:

I understand how to fill the values of the structure I created in my original code. as it included everything except the printStatistic function. I guess I'll try looking at your "Functions in C and C++, Part II: Argument Passing and Arrays" link under your message.


You might also like to see this recent DIC link ...

struct example
Was This Post Helpful? 0
  • +
  • -

#5 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: Array of Struct Question!

Posted 16 April 2013 - 11:05 PM

View PostLionF2013, on 15 April 2013 - 04:38 PM, said:

I understand how to fill the values of the structure I created in my original code. as it included everything except the printStatistic function. I guess I'll try looking at your "Functions in C and C++, Part II: Argument Passing and Arrays" link under your message.



And you may like to see this student type problem example solution that uses to advantage some features of a C++ struct

Note that the data and the functions that process that data can all be 'encapsulated' by placing them in the same struct

// bp_structWithOverloadedOut.cpp

// http://developers-heaven.net/forum/index.php/topic,46.0.html

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

using namespace std;

const string MY_FILE = "BP.txt";

// data file is structured like this ...
/*
Smith Jane 111/76
Jones Rob 98/70
Costello Judy-Ann 144/90
Frank-Anderson Bibby_Bonnie 190/30
Sue Peggy 10/5
James-Thomas Andrew 190/111
*/


struct BP
{
    string lname;
    string fname;
    int sys;
    int dia;
    
    string get_diagnosis() const
    {
        if( sys < 120/2 || dia < 80/2 )
            return " :\n"
                   " *** Unrecognized diagnosis ***\n"
                   " *** Please check validity of data entered ***";
        if( sys < 120 && dia < 80 )
            return " : Normal";
        if( (sys >= 120 && sys < 140) || (dia >= 80 && dia < 90) )
            return " : Pre-Hypertension";
        if( (sys >= 140 && sys < 160) || (dia >= 90 && dia < 100) )
            return " : Stage 1 Hypertension";
        if( sys >= 160 || dia > 100 )
            return " : Stage 2 Hypertension";
        // else ...
        return " :\n"
               " *** Unrecognized diagnosis ***\n"
               " *** Please check validity of data entered ***";
        
    }
} ; // <-- don't forget semi-colon here to signify the struct ends here

// overloaded << for output of BP objects ...
ostream& operator << ( ostream& os, const BP& p )
{
    return os << p.lname <<", " << p.fname << " blood pressure was "
              << p.sys << '/' << p.dia << p.get_diagnosis();
}



int main()
{
    ifstream fin( MY_FILE.c_str() ); // C++ string gets converted to C string

    if( fin )
    {
        BP tmp;
        char skip;
        while( fin >> tmp.lname >> tmp.fname >> tmp.sys >> skip >> tmp.dia )
        {
            cout << tmp << endl;
        }
        fin.close();
    }

    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get(); // keep 'Window' open until 'Enter' key is pressed ...
}

This post has been edited by David W: 17 April 2013 - 08:48 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1