Hi! I need help understanding a piece of code. I\'m trying to solve this problem
ID: 671591 • Letter: H
Question
Hi! I need help understanding a piece of code. I'm trying to solve this problem:
Implement an object-oriented student database using the given data. Each line in the data file should contain a student’s name in col. 1-20, class standing in col. 21-30, and GPA in col. 31-34. Use the following data in your input file.
My program should be able to read from a file the above data into an array and sort the array by the student’s name in an ascending order. Selection sort algorithm is strongly recommended on the array of objects. And also the record office would like to have a sorted class list for each class, freshman, sophomore, junior, and senior.
We were given this data:
Williams Leonard Freshman 1.85
Smith Sheila Senior 2.99
Anderson Andy Sophmore 3.01
Wiser Bud Freshman 4.00
Robertson Jully Junior 2.78
Koran Korn Junior 3.50
Smith Sam Junior 2.14
Johnson Jim Junior 3.05
Johnson Jane Junior 3.75
Potter Pam Senior 2.98
Brown Bil Sophmore 2.55
Crooks Cathy Freshman 1.99
Gregg Howard Senior 2.44
Nicholas Judy Senior 3.69
White Bob Sophmore 1.64
Walsh Fred Junior 4.00
Dennis Susan Senior 2.06
Roberts,Rachel Sophmore 4.00
Fredericks Mary Freshman 2.89
Holmes Wendy Senior 2.56
Edwards James Sophmore 3.00
Green Barbara Sophmore 3.67
Brown David Freshman 2.00
Williams Walt Sophmore 2.95
and this code:
Class Cstudent
{
PRIVATE:
NAME: char [20];
CLASS: char [10];
GPA: float;
PUBLIC:
-----
-----
}
#include <fstream.h> /* for file i/o */
#include <iostream.h> /* interactive i/o*/
Void Main ()
Cstudent students [25], fresh [25], soph[25], jun [25], sen [25]; /* the arrays of students (objects) */
char s [80];
ifstream infile (“prog1.dat”) /* read student data from the file*/
for (int i=0; i<24; i++)
{
Infile.getline (s, 80)
cout<< s;
substr (s, 0, 20,Students[i].name);
substr (s, 20, 10, Students[i].class);
substr(s, 30, 4, Students[i].gpa);
*
*
/* Use Selection sort to sort the array */
Char temp[20];
For ( i=0, i<25, i++)
{
Temp = students [i].name;
For ( int j = i+1, j< 25, J++)
{
If ( temp > students [j].name)
Then
{
Temp = students [j].name;
Int k = j;
}
}
swap (students [i],students [k]) ;
}
function swap (Cstudent stdt1, Cstudent stdt2)
{
Cstudent temp = stdt1
Stdt1 = std2; /* Swap two students’ records*/
Stdt2 = temp;
}
/* copying freshman, sophomore, junior and senior
Fresh =freshman list
Soph = sophomore list
Jun=junior list
Sen=senior list */
Int fr=0, so=0, ju=0,se=0; (*fr=freshman index, so=sophomore index, ju=junior index, se=senior index*)
For (i=0, i<25, i++)
{
Switch (students [i].name[1])
Case ‘r’: /*freshman llist */
Fresh [fr++] = students [i];
Break;
Case ‘o’: /* sophomore list*/
Soph [so++] = students [i];
Break;
Case ‘u’: /*junior list */
Jun [ju++] =students [i];
Break;
Case ‘e’: /* senior list */
Sen[se++] =students [i];
Break;
}
}
The code is not complete but is supposed to be a skeleton. But the problem is I don't understand what is going on at all. I don't even know if the way I put the data in the file is correct. I just would like some understanding of this code. Also I would like to know how the .txt file should be formatted. I am open to another approach then the code provided. Any help is appreciated.
Explanation / Answer
//the file prog1.txt must to have format according to
//Each line in the data file should contain a student’s name in
//col. 1-20, class standing in col. 21-30, and GPA in col. 31-34
/***** prog1.txt have the format with enough spaces between the name, class, and gpa--
/*** I put ---- in the place of spaces
Williams Leonard----Freshman--1.85
Smith Sheila--------Senior----2.99
Anderson Andy-------Sophmore--3.01
Wiser Bud-----------Freshman--4.00
Robertson Jully-----Junior----2.78
Koran Korn----------Junior----3.50
Smith Sam-----------Junior----2.14
Johnson Jim---------Junior----3.05
Johnson Jane--------Junior----3.75
Potter Pam----------Senior----2.98
Brown Bil-----------Sophmore--2.55
Crooks Cathy--------Freshman--1.99
Gregg Howard--------Senior----2.44
Nicholas Judy-------Senior----3.69
White Bob-----------Sophmore--1.64
Walsh Fred----------Junior----4.00
Dennis Susan--------Senior----2.06
Roberts,Rachel------Sophmore--4.00
Fredericks Mary-----Freshman--2.89
Holmes Wendy--------Senior----2.56
Edwards James-------Sophmore--3.00
Green Barbara-------Sophmore--3.67
Brown David---------Freshman--2.00
Williams Walt-------Sophmore--2.95
*/
#include <fstream> /* for file i/o */
#include <iostream> /* interactive i/o*/
#include <string> // substr
using namespace std;
/*****************************************************
DECLARATION OF THE CLASS
****************************************************/
class Cstudent
{
private:
/*data of the class*/
std::string NAME;
std::string STANDING;
std::string GPA; // first is the type, second the name
//NOW WE HAVE DATA LIKE STRING, IT IS BETTER
public:
// Declaration of the functions of the class Cstudent
void name(std::string str); //asign the value of a to name
void standing(std::string str); //asign the value of b to standing
void gpa(std::string str); //asign the value of c to GPA
char give_standing();
std::string give_name();
};
/**************************************************
IMPLEMENTATION OF THE FUNCTIONS
****************************************************/
void Cstudent::name(std::string str) //asign the value of a to name
{
NAME.assign(str);
}
void Cstudent::standing(std::string str) //asign the value of b to standing
{STANDING.assign(str);
}
void Cstudent::gpa(std::string str) //asign the value of c to GPA
{GPA.assign(str);
}
char Cstudent::give_standing()
{return STANDING[0];
}
std::string Cstudent::give_name()
{return NAME;
}
/*
void swap (Cstudent stdt1, Cstudent stdt2)
{
Cstudent temp = stdt1;
stdt1 = stdt2; // Swap two students’ records
stdt2 = temp;
}
*/
/**********************************************************
MAIN PROGRAM
*********************************************************/
int main(int argc, char** argv) {
/**********************************************************
DECLARATION OF VARIABLE
***********************************************************/
Cstudent students[25], fresh[25], soph[25], jun[25], sen[25]; /* the arrays of students (objects) */
char s[40];
std::string name_aux;
std::string standing_aux;
std::string gpa_aux;
std::string temp;
int i,j,k=0;
ifstream infile ("prog1.txt"); // read student data from the file
for (int i=0; i<24; i++) // read line by line; 25 lines
{
infile.getline (s, 40); // put each line in the "s" variable
// cout<< s; //this prints are from tests
// cout<<" ";
name_aux.assign(s,0,20);
standing_aux.assign(s,20,10);
gpa_aux.assign(s,30,5);
/*
for (int j=0; j<20; j++) //in this cycles descompose the variable s
{name_aux[j]=s[j]; // the firsts 20 values goes to name
}
int k=0;
for (int j=20; j<30; j++) //between 20 and 30 goes to standing
{k=k+1; // k is an auxiliar variable to initialize the count
standing_aux[k]=s[j];
}
k=0;
for (int j=30; j<35; j++) // and the gpa
{k=k+1;
gpa_aux[k]=s[j];
}
***********/
students[i].name(name_aux); //call the function to asign the value taken of each line
students[i].standing(standing_aux);
students[i].gpa(gpa_aux);
//cout<<name_aux; //this print is for test
//cout<<" ";
}
/* Use Selection sort to sort the array */
/*
/***********************************************
SORT ALGORITHM
**********************************************/
for ( i=0; i<24; i++)
{
//temp = students[i].give_name();
for (j = 0; j <24 ; j++)
{k=j+1;
if ( students [j].give_name() > students [k].give_name())
swap (students [j],students [k]) ;
}
}
/*******************************
verification of the sort
*****************************/
//for(i=0;i<24;i++)
//{cout<<students[i].give_name()<<" ";}
/* copying freshman, sophomore, junior and senior
Fresh =freshman list
Soph = sophomore list
Jun=junior list
Sen=senior list */
/*******************************************
ORDER BY CLASS STANDING
*******************************************/
int fr=0, so=0, ju=0,se=0; /*fr=freshman index, so=sophomore index, ju=junior index, se=senior index*/
for (i=0; i<25; i++)
{
switch (students [i].give_standing())
{
case 'r': //freshman llist/
fresh [fr++] = students [i];
break;
case 'o': // sophomore list
soph [so++] = students [i];
break;
case 'u': //junior list
jun [ju++] =students [i];
break;
case 'e': // senior list
sen[se++] =students [i];
break;
}
// cout<<students[i].give_name();
}
}
/*************************************
note: the algorithm of sort is a bubble.
the print of the results is not made, this part is for you ;)
the case sensitive is important in c++
with string is much better, it have own functions very useful
i hope have been help for you in understanding the problem
***********************************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.