Help Please!! I still getting use to programming and the teacher is going fast w
ID: 3664190 • Letter: H
Question
Help Please!! I still getting use to programming and the teacher is going fast we must use struct in the program. I know we are NOT allowed to use global variables.Here is more details.
Problem description:
The university needs an efficient way to handle student information. Given the large number of students, it has become very difficult to manually track all information regarding current students and alumni. This has led the university to hire you to program a solution.
The goal of the program is to read student records from a file (students.txt - provided). Each line in the file is a student record. Each record is best represented by a struct, so all records must be read into an array of structs.
Each line consists of the following fields separated by spaces:
For current students:
FirstName LastName ID Major GPA GraduationStatus
For example: Alexander Desplat 000000001 CSC 4.0 N
For alumni:
FirstName LastName ID Major GPA GraduationStatus GraduationDate
For example: Bruno Coulais 000000002 CSC 4.0 Y 08:04:2015
After reading the records from a file, the program should present a menu which handles the following requests:
0. Printing all students
1. Printing only students who have graduated
2. Printing only students who have GPA >= a given GPA
3. Printing only students of a given major
Note:
1. The GPA must be formatted to 2 decimal places
2. Dates printed on the screen must be in the format: MM:DD:YY
The main() function of your program should be very simple. The main function should be a collection of variables declaration and functions call. You will need to use different functions to read the data from the students.txt, print the menu, print students fulfilling a user specified criteria. This means your program must consist of at least 3 functions (excluding main).
Hints: • Do not use global variables; instead all information must be passed as parameters to functions. • Some parameters may be passed by reference • Please note that some sub-fields like Date item are best represented by struct data type. This means the struct data type representing students must have a struct representing the GraduationDate inside it (a struct inside a struct)
• Your program should read the required data from file: students.txt
student.txt:
10
Alex Kapour 000000000 CSC 4.0 N
Brit Claus 000000001 ENG 2.0 Y 08:04:2015
Nick Ugluman 000000002 PHY 3.5 Y 02:03:2015
Yasi Tier 000000003 CSC 4.0 N
Clone Marshall 000000004 PHY 3.0 N
Sabaz Valy 000000005 ENG 3.0 Y 11:09:2015
Sarah Arguni 000000006 MUS 4.0 N
Greg Lorrong 000000007 SOC 3.5 Y 06:11:2014
Michael Gia 000000008 PHY 3.0 Y 09:11:2013
Alian Morri 000000009 POL 2.8 Y 09:11:2012
My Program:universityRecords.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
struct currentStudentRec
{
double ID;
string firstName;
string lastName;
string major;
float GPA;
float graduationDate;
}
currentStudent student[4];
if stream infile;
for (counter=0; counter <=4; counter++)
{
infile>> alumini[counter].firstName
>> student[counter].lastName
>> student[counter].ID
>> student[counter].major
>> stuedent[counter].GPA
>> student[counter].graduationDate
}
cout<<student[counter].firstName<<""<<student[counter].lastName<<"";
cout<<student[counter].GPA<<"";
cout<<setprecision(2)<<GPA<<endl;
}
alumRec.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
struct alumRec
{
double ID;
string firstName;
string lastName;
string major;
float GPA;
float graduationDate;
char graduationStatus;
}
aluminiRec alumini[6];
if stream infile;
for (counter=0; counter <=4; counter++)
{
infile>> alumini[counter].firstName
>> alumini[counter].lastName
>> alumini[counter].ID
>> alumini[counter].major
>> alumini[counter].GPA
>> alumini[counter].graduationDate
>>alumini[counter].graduationStatus
}
record.h
#ifndef_student_H
#define_student_H
{};
#endif
Explanation / Answer
//universityRecords.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct Student
{
double ID;
string firstName;
string lastName;
string major;
float GPA;
char graduationStatus;
string graduationDate;
};
class UniveRecords
{
public:
Student currentStudents[10];
int n;
UniveRecords()
{
}
void readFile()
{
ifstream infile("students.txt");
infile>>n;
int counter=0;
while(!infile.eof())
{
infile>>currentStudents[counter].firstName
>> currentStudents[counter].lastName
>> currentStudents[counter].ID
>> currentStudents[counter].major
>> currentStudents[counter].GPA
>> currentStudents[counter].graduationStatus;
counter++;
}
}
void print()
{
cout<<endl<<"Student Details";
for(int counter=0;counter<n;counter++)
{
cout<<endl<<currentStudents[counter].firstName<<" "<<currentStudents[counter].lastName<<" ";
cout<<currentStudents[counter].ID<<" ";
cout<<currentStudents[counter].major<<" ";
cout<<setprecision(2)<<currentStudents[counter].GPA<<" ";
cout<<currentStudents[counter].graduationStatus<<" ";
}
cout<<endl;
}
};
/*----------------------------------------------------------------------------*/
/*University.cpp*/
#include <iostream>
#include "universityRecords.cpp";
using namespace std;
void graduated(Student currentStudents[],int n);
void aboveGPA(Student currentStudents[],float GPA,int n);
void haveMajor(Student currentStudents[],string major,int n);
int main()
{
UniveRecords Univ;
Univ.readFile();
int choice;
while(true)
{
cout<<endl<<"0. Printing all students";
cout<<endl<<"1. Printing only students who have graduated";
cout<<endl<<"2. Printing only students who have GPA >= a given GPA";
cout<<endl<<"3. Printing only students of a given major";
cout<<endl<<"4. Quit";
cout<<endl<<"Enter your choice: ";
cin>>choice;
if(choice==4)
exit(0);
switch(choice)
{
case 0: Univ.print();break;
case 1: graduated(Univ.currentStudents,Univ.n);break;
case 2:
float gpa;
cout<<endl<<"Enter GPA:";
cin>>gpa;
aboveGPA(Univ.currentStudents,gpa,Univ.n);break;
case 3: string major;cout<<endl<<"Enter major:";
cin>>major;haveMajor(Univ.currentStudents,major,Univ.n);break;
}
}
return 0;
}
void graduated(Student currentStudents[], int n)
{
cout<<endl<<"Graduated students details";
for(int i=0;i<n;i++)
{
if(currentStudents[i].graduationStatus=='Y')
{
cout<<endl<<currentStudents[i].firstName<<" "<<currentStudents[i].lastName<<" ";
cout<<currentStudents[i].ID<<" ";
cout<<currentStudents[i].major<<" ";
cout<<setprecision(2)<<currentStudents[i].GPA<<" ";
cout<<currentStudents[i].graduationStatus<<" ";
}
}
cout<<endl;
}
void aboveGPA(Student currentStudents[],float gpa,int n)
{
cout<<endl<<"The students above the gpa "<<gpa;
for(int i=0;i<n;i++)
{
if(currentStudents[i].GPA>=gpa)
{
cout<<endl<<currentStudents[i].firstName<<" "<<currentStudents[i].lastName<<" ";
cout<<currentStudents[i].ID<<" ";
cout<<currentStudents[i].major<<" ";
cout<<setprecision(2)<<currentStudents[i].GPA<<" ";
cout<<currentStudents[i].graduationStatus<<" ";
}
}
cout<<endl;
}
void haveMajor(Student currentStudents[],string major,int n)
{
cout<<endl<<"The students having major "<<major;
for(int i=0;i<n;i++)
{
if(currentStudents[i].major.compare(major)==0)
{
cout<<endl<<currentStudents[i].firstName<<" "<<currentStudents[i].lastName<<" ";
cout<<currentStudents[i].ID<<" ";
cout<<currentStudents[i].major<<" ";
cout<<setprecision(2)<<currentStudents[i].GPA<<" ";
cout<<currentStudents[i].graduationStatus<<" ";
}
}
cout<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.