In: Computer Science
Hello, I need to divide my code in this different parts.
Use a standard approach to source files in this assignment: a .cpp for each function (you have at least two – main and ageCalc) a header file for the student struct, properly guarded
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
#define nullptr NULL
#define MAX_SIZE 100
struct Student
{
string firstName;
char middleName;
string lastName;
char collegeCode;
int locCode;
int seqCode;
int age;
};
struct sort_by_age
{
inline bool operator()(const Student *s1, const Student *s2)
{
return (s1->age < s2->age);
}
};
int ageCalc(Student *studs[], Student *&youngest);
int main()
{
Student *students[MAX_SIZE] = {nullptr};
ifstream Myfile;
Myfile.open("a2data.txt");
if (!Myfile.is_open())
{
cout << "Could not open the file!";
return 1;
}
vector<string> words;
string line;
while (!Myfile.eof())
{
getline(Myfile, line);
line.c_str();
int i = 0;
string word = "";
while (line[i] != '\0')
{
if (line[i] != ' ')
{
word = word + line[i];
i++;
}
else
{
if (word != "")
words.push_back(word);
word = "";
i++;
}
}
words.push_back(word);
}
Myfile.close();
int count = 0;
string fname = words.at(count);
count++;
int n = 0;
while (count < words.size() - 2)
{
Student *s = new Student;
s->firstName = fname;
string mname = words.at(count);
s->middleName = mname[0];
count++;
s->lastName = words.at(count);
if (words.at(count).size() >= 12)
{
if (words.at(count)[1] >= '0' && words.at(count)[1] <= '9')
{
count--;
s->middleName = ' ';
s->lastName = words.at(count);
}
}
count++;
string id = words.at(count);
count++;
s->collegeCode = id[0];
string loc = "";
loc = loc + id[1];
loc = loc + id[2];
s->locCode = stoi(loc);
string seq = "";
for (int j = 3; j < 9; j++)
{
seq = seq + id[j];
}
s->seqCode = stoi(seq);
string age = "";
age = age + id[9];
age = age + id[10];
age = age + id[11];
s->age = stoi(age);
fname = id.erase(0, 12);
students[n] = s;
n++;
}
words.clear();
sort(students, students + n, sort_by_age());
cout << setw(15) << left << "Last Name";
cout << setw(15) << left << "Midlle Name";
cout << setw(15) << left << "First Name";
cout << setw(15) << left << "College Code";
cout << setw(12) << left << "Location";
cout << setw(12) << left << "Sequence";
cout << setw(12) << left << "Age" << endl;
cout << "=======================================================================================" << endl;
for (int i = 0; i < n; i++)
{
cout << setw(15) << left << students[i]->lastName;
cout << setw(15) << left << students[i]->middleName;
cout << setw(20) << left << students[i]->firstName;
cout << setw(13) << left << students[i]->collegeCode;
cout << setw(10) << left << students[i]->locCode;
cout << setw(12) << left << students[i]->seqCode;
cout << students[i]->age << endl;
}
cout << endl;
Student *youngest = NULL;
int avg_age = ageCalc(students, youngest);
cout << "Average age is: " << avg_age << endl;
cout << "Youngest student is " << youngest->firstName << " " << youngest->middleName << " " << youngest->lastName << endl;
return 0;
}
int ageCalc(Student *studs[], Student *&youngest)
{
youngest = studs[0];
int i = 0;
int avg_age = 0;
while (studs[i] != NULL)
{
avg_age += studs[i]->age;
if (youngest->age > studs[i]->age)
youngest = studs[i];
i++;
}
return avg_age / i;
}
File needed:
https://drive.google.com/file/d/15_CxuGnFdnyIj6zhSC11oSgKEYrosHck/view?usp=sharing
If you have any doubts, please give me comment...
The program divided into three files:
1. ageCalc.h //which contains constants, structure and function prototypes
2. ageCalc.cpp //which contains function definitions
3. main.cpp
ageCalc.h
#include<iostream>
#include<string>
using namespace std;
#define MAX_SIZE 100
struct Student
{
string firstName;
char middleName;
string lastName;
char collegeCode;
int locCode;
int seqCode;
int age;
};
bool sort_by_age(const Student *s1, const Student *s2);
int ageCalc(Student *studs[], Student *&youngest);
ageCalc.cpp
#include "ageCalc.h"
bool sort_by_age(const Student *s1, const Student *s2){
return (s1->age < s2->age);
}
int ageCalc(Student *studs[], Student *&youngest)
{
youngest = studs[0];
int i = 0;
int avg_age = 0;
while (studs[i] != nullptr)
{
avg_age += studs[i]->age;
if (youngest->age > studs[i]->age)
youngest = studs[i];
i++;
}
return avg_age / i;
}
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include "ageCalc.h"
using namespace std;
int main()
{
Student *students[MAX_SIZE] = {nullptr};
ifstream Myfile;
Myfile.open("a2data.txt");
if (!Myfile.is_open())
{
cout << "Could not open the file!";
return 1;
}
vector<string> words;
string line;
while (!Myfile.eof())
{
getline(Myfile, line);
line.c_str();
int i = 0;
string word = "";
while (line[i] != '\0')
{
if (line[i] != ' ')
{
word = word + line[i];
i++;
}
else
{
if (word != "")
words.push_back(word);
word = "";
i++;
}
}
words.push_back(word);
}
Myfile.close();
int count = 0;
string fname = words.at(count);
count++;
int n = 0;
while (count < words.size() - 2)
{
Student *s = new Student;
s->firstName = fname;
string mname = words.at(count);
s->middleName = mname[0];
count++;
s->lastName = words.at(count);
if (words.at(count).size() >= 12)
{
if (words.at(count)[1] >= '0' && words.at(count)[1] <= '9')
{
count--;
s->middleName = ' ';
s->lastName = words.at(count);
}
}
count++;
string id = words.at(count);
count++;
s->collegeCode = id[0];
string loc = "";
loc = loc + id[1];
loc = loc + id[2];
s->locCode = stoi(loc);
string seq = "";
for (int j = 3; j < 9; j++)
{
seq = seq + id[j];
}
s->seqCode = stoi(seq);
string age = "";
age = age + id[9];
age = age + id[10];
age = age + id[11];
s->age = stoi(age);
fname = id.erase(0, 12);
students[n] = s;
n++;
}
words.clear();
sort(students, students + n, sort_by_age);
cout << setw(15) << left << "Last Name";
cout << setw(15) << left << "Midlle Name";
cout << setw(15) << left << "First Name";
cout << setw(15) << left << "College Code";
cout << setw(12) << left << "Location";
cout << setw(12) << left << "Sequence";
cout << setw(12) << left << "Age" << endl;
cout << "=======================================================================================" << endl;
for (int i = 0; i < n; i++)
{
cout << setw(15) << left << students[i]->lastName;
cout << setw(15) << left << students[i]->middleName;
cout << setw(20) << left << students[i]->firstName;
cout << setw(13) << left << students[i]->collegeCode;
cout << setw(10) << left << students[i]->locCode;
cout << setw(12) << left << students[i]->seqCode;
cout << students[i]->age << endl;
}
cout << endl;
Student *youngest = NULL;
int avg_age = ageCalc(students, youngest);
cout << "Average age is: " << avg_age << endl;
cout << "Youngest student is " << youngest->firstName << " " << youngest->middleName << " " << youngest->lastName << endl;
return 0;
}