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

Write a program in C++ with a function to read in the file parts.txt into either

ID: 3906434 • Letter: W

Question

Write a program in C++ with a function to read in the file parts.txt into either parallel vectors or a vector of structs.

The first few lines of the file look like:

P-13725 A 23 61.46

P-13726 B 12 51.08

P-13754 D 27 4.56

P-13947 C 34 27.71

Representing part number, Class, On hand balance, cost. After the vector(s) has/have been filled, display a menu which allows the user to request the following reports:

Total cost of inventory.

A count of parts of each class.

Cost of inventory for a class.

Part with the highest cost of inventory.

Part with lowest cost of inventory.

Exit.

Each of these should be a function. I’ve posted a typical run on the next page.

Hints:

          Use system(“cls”); to clear the screen after the user makes a choice.

There is a neat little trick you can use to count the classes based on the fact that the ascii value of ‘A’ is 65, ‘B’ is 66, etc. Not necessary but very efficient!
Here is the struct and the prototypes of the functions I used:
struct Parts

{

          string number;

          char cls;

          int ohb;

          double cost;

};

bool readFile(vector <Parts> &pVector);

int displayMenu();

double totalCost(const vector <Parts> &pVector);

void countByClass(const vector <Parts> & pVector,
                  vector <int> &classCounts);

double costForClass(char classIn, const vector <Parts> & pVector);

string highestCost(const vector <Parts> &pVector);

string lowestCost(const vector <Parts> &pVector);

void displayCounts(const vector <int> & classCounts);

   R E P O R T S M E N U

1. Total cost of inventory.

2. A count of parts of each class.

3. Cost of inventory for a class.

4. Part with the highest cost of inventory.

5. Part with lowest cost of inventory.

6. Exit.

1

Your choice was 1

Total cost of inventory is $343136.99

   R E P O R T S M E N U

1. Total cost of inventory.

2. A count of parts of each class.

3. Cost of inventory for a class.

4. Part with the highest cost of inventory.

5. Part with lowest cost of inventory.

6. Exit.

Your choice was 2

Count of parts by class

A    85

B    70

C    76

D    61

E     7

F     6

   R E P O R T S M E N U

1. Total cost of inventory.

2. A count of parts of each class.

3. Cost of inventory for a class.

4. Part with the highest cost of inventory.

5. Part with lowest cost of inventory.

6. Exit.

Your choice was 3

Which class?

A

Cost of inventory for class A is $191519.80

Your choice was 4

The part with the highest cost of inventory is P-27850

Your choice was 5

The part with the lowest cost of inventory is P-30538

The parts.txt file contains the following:

P-39457 A 16 102.14
P-11702 B 21 24.74
P-11754 B 27 15.23
P-12009 D 3 12.80
P-12131 A 48 24.17
P-12385 B 41 27.33
P-12652 E 18 28.33
P-12677 D 15 26.59
P-12746 A 6 111.57
P-12746 C 25 14.83
P-12797 D 18 29.07
P-12856 A 33 120.96
P-13192 C 65 76.98
P-13289 C 45 36.61
P-13682 D 14 25.56
P-13713 A 10 35.12
P-14175 B 41 9.21
P-14226 D 14 28.95
P-14455 D 26 26.39
P-14493 A 29 22.09
P-14518 D 9 5.56
P-14613 A 49 74.66
P-14702 A 38 92.06
P-14748 C 24 24.80
P-38806 C 42 31.39
P-38870 A 29 127.19
P-38880 B 26 110.57
P-39014 A 30 111.33
P-39319 B 26 3.38
P-39499 B 32 37.50
P-39819 B 13 18.71
P-40002 B 9 86.81
P-40021 C 45 27.28
P-40100 B 9 40.04
P-40426 D 32 11.67
P-40784 B 4 106.76
P-41213 C 23 16.53
P-41254 D 5 11.13
P-41316 C 17 33.99
P-41359 A 44 132.94
P-43521 C 25 26.27
P-14976 C 45 22.16
P-15122 B 47 17.96
P-15825 E 27 25.76
P-15902 C 22 26.10
P-15944 D 33 15.94
P-16100 A 40 94.38
P-16283 A 44 108.99
P-16414 C 43 5.12
P-16420 D 12 25.72
P-16467 B 28 72.51
P-16803 D 44 13.61
P-17160 D 23 11.61
P-17243 A 7 69.19
P-17248 D 42 10.50
P-17295 C 2 9.26
P-17344 C 42 36.05
P-17345 B 3 33.14
P-17550 B 23 6.75
P-17644 B 24 2.62
P-17668 D 15 8.86
P-17788 F 34 9.84
P-17833 B 14 25.95
P-17871 C 4 11.70
P-18158 D 25 22.26
P-18553 A 39 5.31
P-18638 C 35 31.64
P-18827 A 26 99.28
P-19041 B 45 63.99
P-19152 A 28 133.91
P-19378 C 44 26.01
P-19505 B 7 54.98
P-19780 A 33 44.66
P-19798 D 17 22.09
P-19842 D 38 5.01
P-19943 D 30 17.46
P-19965 C 34 17.07
P-20097 A 2 26.46
P-20120 B 2 72.45
P-20126 D 6 7.17
P-20143 B 43 38.61
P-20206 B 17 39.00
P-20353 D 8 8.40
P-20486 A 20 186.81
P-20491 B 32 43.20
P-20864 C 27 33.88
P-20884 C 33 27.90
P-21071 A 28 146.96
P-21092 A 43 83.53
P-21282 C 37 10.50
P-21326 F 18 12.09
P-21499 D 30 20.19
P-21599 B 42 41.83
P-22018 B 47 46.58
P-22025 E 25 18.95
P-22083 A 45 3.64
P-22123 F 14 11.51
P-22204 A 46 100.85
P-22254 C 2 7.98
P-22391 B 47 6.69
P-22526 C 37 9.14
P-22547 C 15 32.88
P-22554 A 50 41.31
P-22604 A 10 125.74
P-22757 B 12 47.19
P-22844 D 10 0.50
P-23038 A 29 2.33
P-23102 A 11 56.07
P-23146 D 6 16.89
P-23379 D 43 19.29
P-23506 C 40 14.41
P-23975 D 5 6.84
P-24183 D 13 10.63
P-24253 A 40 120.22
P-24286 B 23 54.52
P-24383 C 44 9.01
P-24613 B 50 57.98
P-24675 E 21 14.22
P-24692 D 33 29.45
P-24724 B 45 6.45
P-24910 A 23 14.27
P-24991 D 34 28.48
P-35349 A 46 80.95
P-35432 A 36 69.73
P-35797 A 4 94.76
P-35809 B 30 30.69
P-35867 D 35 20.86
P-35936 B 22 31.21
P-35961 F 32 0.57
P-36143 D 29 9.53
P-36145 A 5 83.35
P-11196 B 39 59.04
P-11226 C 29 24.46
P-11245 D 45 0.93
P-11273 C 38 21.48
P-11281 B 40 66.47
P-11376 D 50 27.03
P-24992 C 123 45.00
P-13725 A 23 61.46
P-13726 B 12 51.08
P-13754 D 27 4.56
P-13947 C 34 27.71
P-13982 C 17 10.47
P-13987 D 17 27.24
P-13999 B 12 33.01
P-14036 A 41 67.24
P-14124 B 7 69.20
P-25157 A 32 84.01
P-25366 A 37 32.37
P-25405 D 2 8.61
P-25482 A 13 71.09
P-25513 B 15 76.05
P-25549 C 8 4.78
P-25564 D 24 13.30
P-25594 C 16 32.59
P-25602 B 39 15.05
P-25606 C 42 16.95
P-25648 C 3 25.89
P-25746 A 35 90.57
P-25895 D 39 26.89
P-25923 C 2 26.08
P-26188 C 48 32.37
P-26298 A 40 76.50
P-26377 C 21 21.78
P-26382 C 39 15.99
P-26408 F 9 0.37
P-26574 A 38 7.96
P-26576 C 13 23.59
P-26853 A 10 118.43
P-26868 A 2 125.05
P-27191 C 34 20.39
P-27225 C 34 14.28
P-27345 A 28 195.43
P-27417 C 42 1.61
P-27586 B 16 24.21
P-27648 D 10 1.39
P-27785 B 46 26.75
P-27812 C 5 18.37
P-27850 A 46 181.07
P-27873 B 13 102.37
P-27945 D 42 10.44
P-27969 C 2 12.52
P-28009 A 2 60.77
P-27987 C 49 5.37
P-28061 A 35 117.29
P-28309 B 28 34.06
P-28509 D 26 12.95
P-28510 D 13 16.13
P-28518 C 1 22.53
P-28690 A 45 19.34
P-28943 A 21 121.80
P-29044 D 14 6.17
P-29059 D 9 29.82
P-29096 A 49 106.39
P-11191 A 10 34.29
P-29182 A 11 26.83
P-29183 E 46 29.59
P-29185 A 28 73.08
P-29279 C 42 12.55
P-29518 A 17 147.52
P-29549 F 10 7.95
P-29637 B 18 87.18
P-29722 C 25 20.74
P-30037 C 37 22.19
P-30383 A 31 124.74
P-30538 B 1 2.86
P-30591 A 9 40.47
P-30643 A 7 81.94
P-10659 B 17 88.97
P-31007 A 3 11.77
P-31071 B 46 0.36
P-31136 A 29 89.83
P-31429 C 43 5.85
P-31497 A 38 28.12
P-31585 C 8 44.39
P-31701 B 35 51.74
P-31722 A 31 191.32
P-31739 B 24 71.54
P-31744 C 45 15.03
P-31763 A 13 130.56
P-31859 D 24 29.54
P-31969 C 20 17.44
P-32291 A 37 188.37
P-32382 C 44 29.54
P-32417 C 11 27.06
P-32467 C 15 2.73
P-32556 A 44 31.13
P-32599 B 43 66.23
P-32890 A 11 95.99
P-32901 D 32 4.44
P-33017 D 33 5.70
P-33066 C 25 30.27
P-33140 A 32 26.49
P-33143 B 45 44.69
P-33155 D 10 15.25
P-33195 C 11 24.63
P-33224 D 15 8.96
P-33382 C 46 3.44
P-33481 D 20 17.22
P-33491 C 32 24.65
P-33593 C 3 3.52
P-33597 A 2 28.85
P-33643 C 21 5.09
P-34050 B 16 18.99
P-34099 D 49 0.70
P-34106 A 24 63.59
P-34490 F 4 1.65
P-34503 C 7 8.64
P-34512 B 2 69.17
P-34554 A 33 11.27
P-34627 B 22 73.36
P-34670 B 39 112.66
P-34689 B 29 16.06
P-34711 C 1 23.09
P-34743 D 22 25.21
P-34826 B 16 111.01
P-34883 B 49 45.64
P-34991 B 10 41.79
P-34997 A 12 156.00
P-43582 B 3 13.18
P-41383 C 2 2.62
P-41644 D 43 3.40
P-41740 A 25 186.21
P-41754 D 4 20.07
P-41828 D 45 26.95
P-41936 A 11 59.17
P-42059 A 12 78.86
P-42117 C 19 3.07
P-42217 D 45 16.88
P-42259 B 29 73.98
P-42329 A 43 93.94
P-42381 A 37 188.53
P-42634 F 16 7.67
P-42788 C 38 21.88
P-14786 D 22 5.55
P-35146 B 6 62.55
P-11646 D 42 23.58
P-36728 A 15 120.47
P-36826 C 19 18.52
P-36830 B 41 25.29
P-36895 C 11 31.64
P-37009 D 8 23.64
P-37402 C 49 26.02
P-37494 C 1 28.88
P-37504 C 11 13.09
P-37542 B 39 26.43
P-37674 B 17 31.02
P-38033 A 12 70.61
P-38087 B 29 37.76
P-38108 C 44 23.04
P-38247 A 15 128.48
P-38388 C 37 20.84
P-38434 D 49 7.31
P-38460 C 47 28.68
P-38684 B 13 20.30
P-38757 A 30 191.64
P-42792 C 36 17.68
P-42936 A 18 129.79
P-43179 A 47 35.60
P-43264 B 31 103.81
P-43367 B 5 32.39
P-43378 A 46 6.38
P-43622 A 10 155.36
P-43672 A 38 17.97

Explanation / Answer

Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;

struct Parts
{

string number;
char cls;
int ohb;
double cost;
};

bool readFile(vector <Parts> &pVector);
int displayMenu();
double totalCost(const vector <Parts> &pVector);
void countByClass(const vector <Parts> & pVector,
vector <int> &classCounts);

double costForClass(char classIn, const vector <Parts> & pVector);
string highestCost(const vector <Parts> &pVector);
string lowestCost(const vector <Parts> &pVector);
void displayCounts(const vector <int> & classCounts);
int main()
{
vector<Parts> pVector;
vector<int> classCounts;
char c = 'A';

if(!readFile(pVector))
{
cout << "ERROR: could not read input file parts.txt" << endl;
return 1;
}

int choice = 0;

cout << fixed << setprecision(2);

while(choice != 6)
{
choice = displayMenu();
cout << "Your choice was " << choice << endl;
switch(choice)
{
case 1:
cout << "Total cost of inventory is $" << totalCost(pVector) << endl;
break;
case 2:
countByClass(pVector, classCounts);
displayCounts(classCounts);
break;
case 3:

cout << "Which class? ";
cin >> c;
cout << "Cost of inventory for class "<< c << " is $" << costForClass(c, pVector) << endl;
break;
case 4:
cout << "The part with the highest cost of inventory is " << highestCost(pVector) << endl;
break;
case 5:
cout << "The part with the lowest cost of inventory is " << lowestCost(pVector) << endl;
break;
case 6:
break;
default:
cout << "Invalid choice!" << endl;
}

}
}


bool readFile(vector <Parts> &pVector)
{
ifstream infile("parts.txt");
if(infile.fail())
return false;
Parts p;
while(infile >> p.number)
{
infile >> p.cls >> p.ohb >> p.cost;
pVector.push_back(p);
}
infile.close();
return true;
}
int displayMenu()
{
int choice;
cout << " R E P O R T S M E N U" << endl;
cout << "1. Total cost of inventory." << endl;
cout << "2. A count of parts of each class." << endl;
cout << "3. Cost of inventory for a class." << endl;
cout << "4. Part with the highest cost of inventory." << endl;
cout << "5. Part with lowest cost of inventory." << endl;
cout << "6. Exit." << endl;
cout << "Enter your choice: ";
cin >> choice;
return choice;
}
double totalCost(const vector <Parts> &pVector)
{
double total = 0;
for(int i = 0; i < pVector.size(); i++)
{
total += pVector[i].ohb * pVector[i].cost;
}
return total;
}
void countByClass(const vector <Parts> & pVector,
vector <int> &classCounts)
{
classCounts.clear();
for(int i = 0; i < 6; i++)
classCounts.push_back(0);
for(int i = 0; i < pVector.size(); i++)
{
int index = pVector[i].cls - 'A'; //use ascii value to get index
classCounts[index]++;
}
}

double costForClass(char classIn, const vector <Parts> & pVector)
{
int cost = 0;
for(int i = 0; i < pVector.size(); i++)
{
if(pVector[i].cls == classIn)
cost += pVector[i].ohb * pVector[i].cost;
}
return cost;
}
string highestCost(const vector <Parts> &pVector)
{
int highIndex = 0;
for(int i= 1; i < pVector.size(); i++)
{
if(pVector[i].cost > pVector[highIndex].cost)
highIndex = i;
}
return pVector[highIndex].number;
}
string lowestCost(const vector <Parts> &pVector)
{
int lowIndex = 0;
for(int i= 1; i < pVector.size(); i++)
{
if(pVector[i].cost < pVector[lowIndex].cost)
lowIndex = i;
}
return pVector[lowIndex].number;
}
void displayCounts(const vector <int> & classCounts)
{
char c = 'A';
cout << "Count of parts by class" << endl;
for(int i = 0; i < 6; i++, c++)
{
cout << c << " " << classCounts[i] << endl;
}
}

output
-------
R E P O R T S M E N U
1. Total cost of inventory.
2. A count of parts of each class.
3. Cost of inventory for a class.
4. Part with the highest cost of inventory.
5. Part with lowest cost of inventory.
6. Exit.
Enter your choice: 1
Your choice was 1
Total cost of inventory is $342965.70
R E P O R T S M E N U
1. Total cost of inventory.
2. A count of parts of each class.
3. Cost of inventory for a class.
4. Part with the highest cost of inventory.
5. Part with lowest cost of inventory.
6. Exit.
Enter your choice: 2
Your choice was 2
Count of parts by class
A 85
B 69
C 77
D 60
E 5
F 8
R E P O R T S M E N U
1. Total cost of inventory.
2. A count of parts of each class.
3. Cost of inventory for a class.
4. Part with the highest cost of inventory.
5. Part with lowest cost of inventory.
6. Exit.
Enter your choice: 3
Your choice was 3
Which class? A
Cost of inventory for class A is $191141.00
R E P O R T S M E N U
1. Total cost of inventory.
2. A count of parts of each class.
3. Cost of inventory for a class.
4. Part with the highest cost of inventory.
5. Part with lowest cost of inventory.
6. Exit.
Enter your choice: 4
Your choice was 4
The part with the highest cost of inventory is P-27345
R E P O R T S M E N U
1. Total cost of inventory.
2. A count of parts of each class.
3. Cost of inventory for a class.
4. Part with the highest cost of inventory.
5. Part with lowest cost of inventory.
6. Exit.
Enter your choice: 5
Your choice was 5
The part with the lowest cost of inventory is P-31071
R E P O R T S M E N U
1. Total cost of inventory.
2. A count of parts of each class.
3. Cost of inventory for a class.
4. Part with the highest cost of inventory.
5. Part with lowest cost of inventory.
6. Exit.
Enter your choice: 6
Your choice was 6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote