Rewrite C++ Program #5 to use the C++ string class vs. C strings . Your solution
ID: 3764752 • Letter: R
Question
Rewrite C++ Program #5 to use the C++ string class vs. C strings.
Your solution must be an object-oriented C++ program (i.e. a class and a main function to use it).
You must use files for both your input and output as stated in the textbook.
Execute your C++ program first for a client sort (to be indicated by option 1) and then for a business sort (to be indicated by option 2).
Acme Construction
Johnson Electrical
Brown Heating and Cooling
Smith Switches
Jones Computers
Williams Cleaning Equipment
Machinery design
Switch manufacturing
Boiler design
Switch manufacturing
Computer sales
Machinery sales
Original question:
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 bubble sort. The input specifications are to:
Read the client and business from a file.
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.
Client Business TypeAcme Construction
Johnson Electrical
Brown Heating and Cooling
Smith Switches
Jones Computers
Williams Cleaning Equipment
Machinery design
Switch manufacturing
Boiler design
Switch manufacturing
Computer sales
Machinery sales
Explanation / Answer
#include <iostream> #include <fstream> #include <vector> #include <algorithm> #include "print.h" using namespace std; int main (int argc, char *argv[]) { vector<string> client,business,list; vector<pair<int, int>> v client_bussiness; ifstream in_stream; string client,bussiness; in_stream.open(“Client.txt”); while(!in_stream.eof()) { in_stream >> line; split(line,list); client.push_back(list[0]); business.push_back(list [1]); v[list[0]]= list[1]; } sort(v.begin(), v.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs) { return lhs.second < rhs.second; } ); in_stream.close(); string sort=””; cout<<”Enter sort based on client or bussiness”; cout<<”1 for client and 2 for business”; cin >> input; if(“1”.compare(input) == 0) { Copy(lhs); } else { Copy(rhs); } } void split(const std::string& str, std::vector<std::string>& v) { std::stringstream ss(str); ss >> std::noskipws; std::string field; char ws_delim; while(1) { if( ss >> field ) v.push_back(field); else if (ss.eof()) break; else v.push_back(std::string()); ss.clear(); ss >> ws_delim; } } Void copy(pair<String,String> points) { String file=”out.txt”; if(file != "") { stringstream ss; for(i=points.begin(); i != points.end();++i ) { ss << i->first << " " << i->second << " "; } ofstream out(file.c_str()); if(out.fail()) { out.close(); return false; } out << ss.str(); out.close(); } return true; }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.