Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ please. I am having trouble getting my code to actually display something else besides just my menu I think it might be stuck in

C++ please.

I am having trouble getting my code to actually display something else besides just my menu I think it might be stuck in a infinite loop, but I can not seem to figure it out.

These are the instructions:

Create two string array variables to store a person's first name and last name. Set the array allocation to 100 for each

Use two separate input files to read in an unknown number of first and last names from each input file (one input file for first names and one input file for last names) - use a generic function to read in this data

Ask the user in a simple menu if they want to sort the superhero names based on first name or last name

Use a generic function to sort either the first name or last name using bubble sort. The sort code should be written only to make it efficient!

Output the sorted information to the screen

I expect to see functions for steps 2 - 5 above.

Input File Data Example:

Input File Data for First Names (you are free to use your own first name values, these are just examples to show you what I used in my program):

Tony

Bruce

Peter

Input File Data for Last Name (you are free to use your own last name values, these are just examples to show you what I used in my program) :

Stark

Banner

Parker

Example Menu:

1. Sort on First Name

2. Sort on Last Name

Enter Selection:

Examples of Output:

Output: Sort on First Name

Bruce Banner

Peter Parker

Tony Stark

Output: Sort on Last Name

Banner Bruce

Parker Peter

Stark Tony

Assignment Notes:

The bulk of the points for this assignment will come from properly using functions for reading in the names and also for sorting them efficiently (no redundant code!). For additional information about bubble sort, please visit: https://en.wikipedia.org/wiki/Bubble_sort There is a nice graphic there that shows how bubble sort works.

*NOTE: This program uses two input files because a course objective requires us to use multiple input files for at least one programming project. This assignment meets that requirement.

**NOTE: The term generic function is something I made up. It is not an official title from any C++ textbook for what we are doing. Essentially, these are just functions that are being used more efficiently. I call them generic because that is really what you are doing - making them general in nature so they can handle different situations depending on what data is sent as parameters.

***NOTE: Efficency of your program is very important in this assignment. If you are writing the same code more than once, then you are not doing this right and you will lose points! I will help you if you cannot get this part to work.

General Notes:

Be sure to use comments in your program: Name, Program Description, Date and anywhere else in the program you deem necessary.

Here is my code:

#include #include #include

using namespace std;

void fileCheck(ifstream& iFile, string text) { if (!iFile.is_open()) { cout << " input file not found" << endl; exit(1); } } void output(string fName[], string lName[]) {

for (int a = 0; a <= 2; a++) { cout << fName[a] << " " << lName[a] << endl << endl; } cout << endl; }

void dataSorting(string fName[], string lName[], int counter) {}

int main() { ifstream first, last;

first.open("first.txt"); last.open("last.txt");

fileCheck(first, "first.txt"); fileCheck(last, "last.txt");

string fName[100], lName[100]; int counter = 0; string temp;

for (int a = 0; a <= 2; a++) { for (int b = 0; b <= 1; b++) { if (lName[b] > lName[b + 1]) { temp = lName[b]; lName[b] = lName[b + 1]; lName[b + 1] = temp;

temp = fName[b]; fName[b] = fName[b + 1]; fName[b + 1] = temp; }

} } cout << endl;

int choice = 0; while (choice != 100) { cout << "Sort by first name enter 1: " << endl; cout << "Sort by last name enter 2: " << endl; cout << "Press (3 to exit): "; cin >> choice; if (choice == 1) dataSorting(fName, lName, counter); else if (choice == 2) dataSorting(lName, fName, counter); } for (int a = 0; a <= 2; a++) cout << "Name: " << lName[a] << ", " << fName[a] << endl;

output(fName, lName); output(lName, fName);

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions