Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Pls help with this c++ coding. A data file will contain several lines of data. E

ID: 3560467 • Letter: P

Question

Pls help with this c++ coding.

A   data   file   will   contain   several   lines   of   data.   Each   line   will   contain   a shoe   category and brand (string  
- category,brand) and   the   number   of   shoes sold   per   month (integer).   The   brand string   and   the  
number   of   shoes sold will   be   separated   by   at   least   1   blank   space.   The   category   and   brand will   be  
in   a   mixture   of   upper   and   lower   case   letters.   The   brand string   will   not   include   any   blank   spaces.  
The   maximum   number   of   data   lines   will   be   50

Write   a   C++   program   that   (using   functions   and   passing   parameters):
1. prompts   the   user   interactively   for   the   name   of   an   input   file.
2. reads   the   name   of   the   input   file   and   opens   it   for   reading.
3. reads   the shoe   category   and   brand and   the   number   of   shoes sold into   2   arrays   (1   for   shoe  
category and   brand,   1   for   the   number   of   shoes sold)   counting   them   as   they   are   read.
(continue   reading   until   EOF encountered)
4. prompts   the   user   interactively   for   the   name   of   an   output   file.
5. reads   the   name   of   the   output   file   and   opens   it   for   writing   - all   remaining   output   should   be  
written   to   this   file.
6. reformats   the   shoe   category and   brand so that   the   first   letter   of   each   string is   capitalized  
and   the   remaining   letters   are   lower   case.
7. displays   your   name   and   the   assignment   #.
8. displays   a   message   stating   how   many   data   sets   were   read
9. sorts   the number   of   shoes sold in   descending   order (including   the   shoe   category   and  
brand),   see   Bubblesort   example   below
10. leaves   a   blank   line,   displays   a   table,   arranged   in   descending   order   by   the   number   of   shoes
sold,   with   2   columns   (number   of   shoes sold and   category and   brand). The   number   of  
shoes sold   should   be   displayed   as   integers   and   right   justified and   then   type and   brand are  
displayed   left   justified.
11. Displays   the   total   number   of   shoes   sold.
12. close   the   input   and   output   files   before   terminating   the   program

Explanation / Answer

#include <iostream>
#include <string>
#include <locale>
#include <fstream>
#include <sstream>
#include <cstdio>

using namespace std;


//Handles uppercase and lowercase logic
string reformat(string *cat_br){
   string str = *cat_br;
   int length = str.length();
   for(int i = 0; i < length;i++){
       str[i] = tolower(str[i]);
   }
   str[0] = toupper(str[0]);
   return str;

}

//separately handle type and brand from given string(split on comma)
string split(string *cat_br){
   string str = *cat_br;
   istringstream ss(str);
   string cat,brand;
   getline(ss,cat,',');
   getline(ss,brand,',');
   return (reformat(&cat) + "," + reformat(&brand));


}

//bubblesort - when we change sold array, also make same changes in brand array
void bsort(int *num,string *cats,int count){
   int temp1;
   string temp2;
   for(int i = 0; i < count -1; i++){
       for(int j = 0; j < count - (i+1);j++){
           if(num[j] < num[j+1]){
               temp1 = num[j];
               num[j] = num[j+1];
               num[j+1] = temp1;
               temp2 = cats[j];
               cats[j] = cats[j+1];
               cats[j+1] = temp2;
           }
       }
   }
}


//reads input file and put entries in two arrays
void reader(string input,string *cbs,int *nums,int *total){

   string line,cat,brand;
   freopen(input.c_str(),"r",stdin);
   int count = 0;
   while(getline(cin,line)){
       istringstream iss(line);
       iss >> cbs[count];
       iss >> nums[count];
       count += 1;
   }
   *total = count;
   fclose(stdin);
}


//to display the results
void display(string output,int *num,string *cats,int total){
   freopen(output.c_str(),"w",stdout);
   cout << "***********************************" << endl;
   cout << " Trung Nguyen Assignment 9 " << endl;
   cout << "***********************************" << endl;
   cout << endl;
   cout << "# data sets read: " << total << endl;
   cout << endl;
   cout.width(6);
   cout << right << "# sold" << " ";
   cout << left << "Type,Brand" << endl;

   for(int i = 0;i < total;i++){
       cout.width(6);
       cout << right << num[i] << " ";
       cout << left << split(&cats[i]) << endl;
   }
   fclose(stdout);
}


int main(){
   string cats[50];
   int num[50];
   int total;
   locale loc;
   string input_file,output_file;

   cout << "Enter the input file name" << endl;
   cin >> input_file;
   cout << "Enter the output file name" << endl;
   cin >> output_file;
   reader((input_file + ".txt"),cats,num,&total);
   bsort(num,cats,total);
   display((output_file + ".txt"),num,cats,total);
   return 0;
}