67 060
Assignments Done
99,3%
Successfully Done
In November 2018

Answer to Question #12845 in C++ for Jillian

Question #12845
I have a list of clients for my consulting firm:

Client Business Type
Acme Construction Machinery design
Johnson Electrical Switch manufacturing
Brown Heating and cooling Boiler design
Smith Switches Switch manufacturing
Jones Computers Computer sales
Williams Cleaning Equipment Machinery sales


To keep track of your clients in an orderly manner, you need a program that can arrange them alphabetically by name or by business type.

Write such a program using a bubble sort.

The input specifications are to:
1) Read the client and business from a file
2) Read the requirement of sorting according to name or business from the keyboard.


The output specification is to put the arranged list into an output file.
Expert's answer
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

string** readList(string fileName, int &n);//read data from input text file
void writeList(string fileName, string** list, int n);//write list to output text file
void sortList(string** list, int n, int type);//sort list by name or by business type

int main() {
int selection = 0;
int type = 0;//type of sorting
bool read = false;//flag
string **list;
int number = 0;//size of list

while (selection != 4) {
& system("cls");//clear screen
& /*print table of menu*/
& cout << "----------------------------------------------" << endl;
& cout << " M E N U quot; << endl;
& cout << "----------------------------------------------" << endl;
& cout << " 1. Read the client and business from a file" << endl;
& cout << " 2. Read the requirement of sorting according" << endl;
& cout << " 3. Write sorted list in output file" << endl;
& cout << " 4. Exit" << endl;
& cout << "----------------------------------------------" << endl;
& cout << " Make your selection: ";
& cin >> selection;//read item
& switch (selection) {
& case 1: {
& string fileName;//name of file
& cout << "Enter input file name: ";
& cin >> fileName;//read file name
& list = readList(fileName, number);//read list from file
& read = true;//raise the flag
& break;

& }
& case 2: {
& system("cls");//clear screen
& cout << " 1. Sort list by name." << endl;
& cout << " 2. Sort list by business type" << endl << endl;
& cout << " Choose: ";
& cin >> type;//read type of sorting
& type--;
& break;
& }
& case 3: {
& if (read) {//if list is read
& sortList(list, number, type);//sort list
& string fileName;//name of file
& cout << "Enter output file name: ";
& cin >> fileName;//read file name
& writeList(fileName, list, number);//write sorted data to output file
& } else {//if list is not created
& cout << "At first read list from file" << endl;
& }
& break;
& }
& }
}

return 0;
}

string** readList(string fileName, int& n) {//read list from input file
fstream File;//object of class fstream
File.open(fileName.c_str(), ios::in);//open file
if (!File) {//if file isn't opened
& cerr << "File error!" << endl;//print error
& exit(0);//exit
} else {
& string** array;//create array
& //allocate memory
& array = new string*[50];
& for (int i = 0; i < 50; i++)
& array[i] = new string[2];
& char* s = new char[50];

& n = 0;
& while (!File.eof()) {//while not end of input file
& File >> array[n][0];//read name
& File.getline(s, 200);//read business type
& array[n][1] = s;
& n++;//increase number
& }
& File.close();//close file
& return array;//return pointer to array
}
}

void writeList(string fileName, string** list, int n) {//write list
fstream File;//object
File.open(fileName.c_str(), ios::out);//create new file
for (int i = 0; i < n; i++) {//for each employee
& File << list[i][0] << " " << list[i][1] << endl;//write name and business type
}
File.close();//close file

}

void sortList(string** list, int n, int type) {//sort list
for (int k = 0; k < n; k++) {
& for (int i = 0; i < n - 1; i++) {//for each record

& if (list[i][type] > list[i + 1][type]) {
& /*swap two neighbor records*/
& string temp0 = list[i][0];
& string temp1 = list[i][1];
& list[i][0] = list[i + 1][0];
& list[i][1] = list[i + 1][1];
& list[i + 1][0] = temp0;
& list[i + 1][1] = temp1;
& }
& }
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions