Question

Using c++ Write a program that reads in a list of up to 25 first names...

Using c++ Write a program that reads in a list of up to 25 first names from an input file nameData.txt and
allows the user to display the name data, sort it in ascending or descending order, count the
number of occurrences of a given name, or exit the program. The input file consists of a single
names per line with each line terminated with an end of line (i.e. using Enter key to end the line).
Your program should present the user with a menu that provides the options show below in
INPUT/OUTPUT.
You must use the following enum for the menu selection:
enum MenuPick {READ_DATA, // Read names from file
DISPLAY, // Display names
SORT_ASC, // Sort names ascending
SORT_DEC, // Sort names descending
COUNT, // Count name
EXIT // Exit program
};
You must write a function with header
MenuPick getMenuPick()
to display the menu, validate the menu pick, and return the enumerator associated with the
selected menu item. Your main function must then use the returned enumerator to perform
the appropriate operation.
You must write a single bubble sort function with header
void bubbleSort(string strAr[], const int SIZE, const bool
ascending)
to perform the sorting and you must use a string method to perform the array element swap
within your bubbleSort function (hint:
http://www.cplusplus.com/reference/string/string/swap/) .
You must write a counting function with header
int count(const string strAr[], const string key,const int SIZE)to count the occurrences of a given name.

Both the sort and counting functions should be in a separate source file functions.cpp with a
separate header file functions.h.
Test your program thoroughly, including using an input file with no names and one with greater
than 25 names. The output below is for the sample input file included.
Screen INPUT/OUTPUT - should be formatted as follows :
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:2
No names to display!
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:1
10 names read from file e:\nameData.txt
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:2
1: Joe
2: Sally
3: Joe
4: Sue
5: Sally
6: Adam
7: Joe
8: Adam
9: Adam
10: Joe
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:3
Sorting in ascending order...
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:2
1: Adam
2: Adam
3: Adam
4: Joe
5: Joe
6: Joe
7: Joe
8: Sally
9: Sally
10: Sue
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:4
Sorting in descending order...
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:2
1: Sue
2: Sally
3: Sally
4: Joe
5: Joe
6: Joe
7: Joe
8: Adam
9: Adam
10: Adam
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:1
10 names read from file e:\nameData.txt
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:2
1: Joe
2: Sally
3: Joe
4: Sue
5: Sally
6: Adam
7: Joe
8: Adam
9: Adam
10: Joe
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:5
Enter a name to count: Joe
Name Joe occurs 4 times
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:5
Enter a name to count: Sue
Name Sue occurs 1 time
Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:7

Please enter a number between 1 and 6!


Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:exit


Please enter an integer value!


Please select from one of the following options:
1. Read names from file
2. Display names
3. Sort names ascending
4. Sort names descending
5. Count name
6. Exit program
Selection:6
Thank you for using my name program.

nameData.txt file :

Joe

Sally

Joe

Sue

Sally

Adam

Joe

Adam

Adam

Joe

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

#include<iostream>
#include"Functions.h"
#include<fstream>
#include<string>
using namespace std;

enum MenuPick
{
   READ_DATA, // Read names from file
   DISPLAY, // Display names
   SORT_ASC, // Sort names ascending
   SORT_DEC, // Sort names descending
   COUNT, // Count name
   EXIT // Exit program
};

MenuPick getMenyPick(int choice)
{
   if (choice == 1)
   {
       return MenuPick::READ_DATA;
   }
   else if (choice == 2)
   {
       return MenuPick::DISPLAY;
   }
   else if (choice == 3)
   {
       return MenuPick::SORT_ASC;
   }
   else if (choice == 4)
   {
       return MenuPick::SORT_DEC;
   }
   else if (choice == 5)
   {
       return MenuPick::COUNT;
   }
   else if (choice == 6)
   {
       return MenuPick::EXIT;
   }
   else
   {
       cout << "Wrong Choice, Exiting!\n";
       exit(1);
   }
}

void readFile(string strArr[100], int &size, string file)
{
   ifstream fin(file);
   while (!fin.eof())
   {
       fin >> strArr[size];
       size++;
   }
   cout << size << " Names Read From File nameData.txt\n";
}

void printNames(string strArr[100], int size)
{
   if (size == 0) cout << "No Names To Display!\n";
   for (int i = 0; i < size; i++)
   {
       cout << i + 1<< ".) " << strArr[i] << endl;
   }

}

int main()
{
   string strArr[100];
   int size = 0;
   int choice = 1;
   MenuPick selection = getMenyPick(choice);

   while (true)
   {
       cout << "\n1. Read names from file\n"
           << "2. Display names\n"
           << "3. Sort names ascending\n"
           << "4. Sort names descending\n"
           << "5. Count name\n"
           << "6. Exit program\n";
       cout << "Enter Your Choice: ";
       cin >> choice;
       selection = getMenyPick(choice);
       if (selection == READ_DATA)
       {
           readFile(strArr, size, "nameData.txt");
       }
       else if (selection == DISPLAY)
       {
           printNames(strArr, size);
       }
       else if (selection == SORT_ASC)
       {
           Functions::bubbleSort(strArr, size, Functions::CompareAscendingName);
       }
       else if (selection == SORT_DEC)
       {
           Functions::bubbleSort(strArr, size, Functions::CompareDescendingName);
       }
       else if (selection == COUNT)
       {
           string key;
           cout << "Enter a name to count: ";
           cin >> key;
           Functions::count(strArr, key, size);
       }
       else if (selection == EXIT)
       {
           cout << "Exiting!\n";
           break;
       }
   }

   return 0;
}

#pragma once
#include <string>
#include <iostream>
using namespace std;

class Functions
{
private:
   static bool Pass(string strArr[100], int Size, bool compare(string Name1, string Name2));
public:
   static bool CompareAscendingName(string Name1, string Name2)
   {
       if (Name1 > Name2)
           return true;
       return false;
   }
   static bool CompareDescendingName(string Name1, string Name2)
   {
       if (Name1 < Name2)
           return true;
       return false;
   }
   Functions();
   static void bubbleSort(string strArr[100], int size, bool compare(string Name1, string Name2));

   static void count(const string strAr[100], string key, int size);

   ~Functions();
};

#include "Functions.h"


Functions::Functions()
{
}

bool Functions::Pass(string strArr[100], int size, bool compare(string Name1, string Name2))
{
   bool SwapH = 0;
   for (int i = 0; i < size - 1; i++)
   {
       if (compare(strArr[i], strArr[i + 1]))
       {
           swap(strArr[i], strArr[i + 1]);
           SwapH = 1;
       }
   }
   return SwapH;
}

void Functions::bubbleSort(string strArr[100], int size, bool compare(string Name1, string Name2))
{
   while (Pass(strArr, size, compare));
}

void Functions::count(const string strAr[100], string key, int size)
{
   int count = 0;
   for (int i = 0; i < size; i++)
   {
       if (key == strAr[i])
           count++;
   }
   cout << key << " Was Found '" << count << "' Times(s)\n.";
}

Functions::~Functions()
{
}

IF THERE IS ANY PROBLEM OR YOU HAVE ANY QUERY REGARDING THE SOLUTION KINDLY FEEL FREE TO ASK, AND LEAVE A THUMBS UP WHEN YOU ARE SATISFIED. THANKS!.

Add a comment
Know the answer?
Add Answer to:
Using c++ Write a program that reads in a list of up to 25 first names...
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
  • Help in JAVA please Write a program that reads a list of students (first names only)...

    Help in JAVA please Write a program that reads a list of students (first names only) from a file. It is possible for the names to be in unsorted order in the file but they have to be placed in sorted order within the linked list. The program should use a doubly linked list. Each node in the doubly linked list should have the student’s name, a pointer to the next student, and a pointer to the previous student. Here...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • .Using OOP, write a C++ program that will read in a file of names. The file...

    .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 not using a class and object will result in...

  • Using Java Create an application that creates and displays a list of names using an array....

    Using Java Create an application that creates and displays a list of names using an array. Then modify the application to create and use a list of numbers. Console for the names application Please enter a name or type “exit” to quit: Diane Please enter a name or type “exit” to quit: Joe Please enter a name or type “exit” to quit: Greta Please enter a name or type “exit” to quit: Henry Please enter a name or type “exit”...

  • Must be written in C++ Display results and comments Write a program that sorts a vector...

    Must be written in C++ Display results and comments Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions: 1. void selSort (vector <string> & v): sorts the vector using selection sort 2. void display (const vector <string> & v): displays the vector contents 3. int binSearch (const vector <string> & v, string key): searches...

  • Written in Java Your job is to produce a program that sorts a list of numbers...

    Written in Java Your job is to produce a program that sorts a list of numbers in ascending order. Your program will need to read-in, from a file, a list of integers – at which point you should allow the user an option to choose to sort the numbers in ascending order via one of the three Sorting algorithms that we have explored. Your program should use the concept of Polymorphism to provide this sorting feature. As output, you will...

  • Write a C program that reads a sequence of numbers and display a message 1. The...

    Write a C program that reads a sequence of numbers and display a message 1. The numbers a re red from the standard input. 2. The first number is the length of the sequence (n) followed by n numbers. 3. If n is 0 or negative, the program displays the message "Error_1" followed 4. If the length is shorter than n, it displays "Error_2" followed by a new line 5. The program inspects the list and display one of the...

  • C++ programming language Write a program that asks the user for a file name. The file...

    C++ programming language Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in...

  • -I need to write a program in C to store a list of names (the last...

    -I need to write a program in C to store a list of names (the last name first) and age in parallel arrays , and then later to sort them into alphabetical order, keeping the age with the correct names. - Could you please fix this program for me! #include <stdio.h> #include <stdlib.h> #include <string.h> void data_sort(char name[100],int age[100],int size){     int i = 0;     while (i < size){         int j = i+1;         while (j < size){...

  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

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