139k views
2 votes
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or all code is in main() will result in a grade submission of 0. List of names in Names.txt: Jackie Sam Bill Tom Mary Paul Zev Barb John Sharon Dana Dominic Steven Padro Katey Kathy Darius Angela Mimi Jesse Kenny Lynn Hector Brittany Jenn Joe Chloe Geena Sylvia Dean

2 Answers

4 votes

Answer: This doesn't work fully, but it's a start. Good Luck

#include <iostream>

#include <fstream>

#include <string>

#include <cstdlib>

using namespace std;

class People

{

private:

const static int SIZE = 30;

string names[SIZE];

int birth_years[SIZE];

int count;

void sort();

void display();

public:

People();

void simulate();

};

People::People()

{

count = 0;

// open both files

ifstream namesFile, birthyearsFile;

namesFile.open("Names.txt");

birthyearsFile.open("BirthYear.txt");

while (!namesFile.eof() && !birthyearsFile.eof() && count < SIZE)

{

getline(namesFile, names[count]);

birthyearsFile >> birth_years[count];

count++;

}

// files open failed, exit the program

if (namesFile.fail() || birthyearsFile.fail())

{

cout << "Unable to open input file(s). Terminating" << endl;

exit(1);

}

//close the files

namesFile.close();

birthyearsFile.close();

sort();

display();

}

void People::sort()

{

for (int i = 0; i < count - 1; i++)

{

for (int j = 0; j < count - 1 - i; j++)

{

if (names[j] > names[j + 1])

{

string tempName = names[j];

names[j] = names[j + 1];

names[j + 1] = tempName;

int tempYear = birth_years[j];

birth_years[j] = birth_years[j + 1];

birth_years[j + 1] = tempYear;

}

}

}

}

void People::display()

{

cout << "Alphabetical Roster of Names: " << endl;

for (int i = 0; i < count; i++)

{

cout << names[i] << "\t" << birth_years[i] << endl;

}

cout << endl;

}

void People::simulate()

{

int year;

cout << endl << "Names by Birth Year" << endl;

// input the birth year

cout << "Please enter the birth year: ";

cin >> year;

// loop that continues until valid input has been read

while (cin.fail() || year < 1995 || year > 2005)

{

cin.clear();

cin.ignore(100, '\\');

cout << "Invalid birth year entered, try again: ";

cin >> year;

}

bool found = false;

for (int i = 0; i < count; i++)

{

if (birth_years[i] == year)

{

if (!found)

{

cout << endl << "For the birth year of " << year << ":" << endl;

found = true;

}

// display the name

cout << names[i] << endl;

}

}

// no name with birth year found

if (!found)

cout << endl << "No names with the birth year " << year << "." << endl;

cout << "End of results" << endl;

}

int main()

{

People people;

people.simulate();

return 0;

}

Step-by-step explanation:

User Avi Harush
by
4.2k points
1 vote
Not really sure sorry for not being able to help
User Dan Inactive
by
4.0k points