Question

Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

Hello, I need to implement these small things in my C++ code. Thanks.

1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits)

2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1.

3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99 input records, so the size of the student variable pointer array will be 100. Use a “global” variable to define the sizeof the pointer array.

4- Initialize each element in the array of pointers to nullptr

Code:

#include

#include

#include

#include

#include

using namespace std;

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); // sort according to age of student

}

};

int ageCalc(Student *studs[], Student *&youngest);

int main()

{

vector students; // create vector object to hold students data

ifstream Myfile; // create input file stream object

Myfile.open("a2data.txt"); // open file

if (!Myfile.is_open())

{

cout << "Could not open the file!";

return 0; //terminate

}

vector words; // create vector object to store words from input file

string line;

while (!Myfile.eof())

{

getline(Myfile, line); // readline from input file stream

line.c_str(); // convert string in to char array

int i = 0; // iterator to iterate over line

string word = "";

while (line[i] != '\0')

{

if (line[i] != ' ')

{

word = word + line[i]; // build word with char

i++;

}

else

{

if (word != "")

words.push_back(word); // add word to vector

word = ""; //reset word to empty string

i++; //ignore white space

}

} //end of line

words.push_back(word); // add word to vector

} //end of file

Myfile.close();

int count = 0; // counts the words proceesed from vector object words

string fname = words.at(count); // variable to store first name of student

count++; //move to next element in the vector

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) // college code + locCode + seq + age

{

if (words.at(count)[1] >= '0' && words.at(count)[1] <= '9') // if second character is a digit

{

// this is the student id field, and there is no middle name

count--; // move one step back for next iteration

s->middleName = ' '; //blank middle name

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); // convert string to integer

string seq = "";

for (int j = 3; j < 9; j++)

{

seq = seq + id[j];

}

s->seqCode = stoi(seq); //convert string to int

string age = "";

age = age + id[9];

age = age + id[10];

age = age + id[11];

s->age = stoi(age); // convert string to int

fname = id.erase(0, 12); // delete first 12 char from id and assign remaining to fname

students.push_back(s);

}

words.clear();

sort(students.begin(), students.end(), 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 < students.size(); i++)

{

cout << setw(15) << left << students.at(i)->lastName;

cout << setw(15) << left << students.at(i)->middleName;

cout << setw(20) << left << students.at(i)->firstName;

cout << setw(13) << left << students.at(i)->collegeCode;

cout << setw(10) << left << students.at(i)->locCode;

cout << setw(12) << left << students.at(i)->seqCode;

cout << students.at(i)->age << endl;

}

cout << endl;

int size = students.size();

Student **stud_arr = new Student*[size];

for(int i=0; i

stud_arr[i] = students[i];

Student *youngest = NULL;

int avg_age = ageCalc(stud_arr, youngest);

cout << "Average age is: " << avg_age << endl;

cout << "Youngest student is " << youngest->firstName << " " << youngest->middleName << " " << youngest->lastName << endl;

for(int i=0; i

delete stud_arr[i];

delete []stud_arr;

students.clear();

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;

}

txt file needed for the program:

https://drive.google.com/file/d/15_CxuGnFdnyIj6zhSC11oSgKEYrosHck/view?usp=sharing

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#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;

}

========= nagarajuanagaraju-Vostro-3550:28092019$ g++ ageCalcv5.cpp -std=C++11 nagarajuanagaraju-Vostro-3550:28092019$ ./a.ou

Add a comment
Know the answer?
Add Answer to:
Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; 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); // sort...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • Hello, I am trying to get this Array to show the summary of NETPAY but have...

    Hello, I am trying to get this Array to show the summary of NETPAY but have been failing. What is wrong? #include <iostream> #include <iomanip> using namespace std; main(){ char empid[ 100 ][ 12 ];    char fname[ 100 ][ 14 ], lastname[ 100 ][ 15 ];    int sum; int hw[ 100 ]; double gp[ 100 ], np[ 100 ], hr[ 100 ], taxrate[100], taxamt[ 100 ]; int counter = 0; int i; cout<<"ENTER EMP ID, FNAME, LNAME, HRS...

  • Expand the payroll program to combine two sorting techniques (Selection and Exchange sorts) for better efficiency...

    Expand the payroll program to combine two sorting techniques (Selection and Exchange sorts) for better efficiency in sorting the employee’s net pay. //I need to use an EXSEL sort in order to combine the selection and Exchange sorts for my program. I currently have a selection sort in there. Please help me with replacing this with the EXSEL sort. //My input files: //My current code with the sel sort. Please help me replace this with an EXSEL sort. #include <fstream>...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • How could I separate the following code to where I have a gradebook.cpp and gradebook.h file?...

    How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...

  • This is C++. The task is to convert the program to make use of fucntions, but...

    This is C++. The task is to convert the program to make use of fucntions, but I am am struggling to figure out how to do so. #include <iostream> #include <iomanip> using namespace std; main(){ char empid[ 100 ][ 12 ];    char fname[ 100 ][ 14 ], lastname[ 100 ][ 15 ]; int hw[ 100 ]; double gp[ 100 ], np[ 100 ], hr[ 100 ], taxrate[100], taxamt[ 100 ]; int counter = 0; int i; cout<<"ENTER EMP ID,...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • Rework this project to include a class. As explained in class, your project should have its...

    Rework this project to include a class. As explained in class, your project should have its functionalities moved to a class, and then create each course as an object to a class that inherits all the different functionalities of the class. You createclass function should be used as a constructor that takes in the name of the file containing the student list. (This way different objects are created with different class list files.) Here is the code I need you...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT