Question

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

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

The data file is on ANGEL in zipped format. 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:

  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.

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 $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.

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.

Your choice was 3

Which class?

A

Cost of inventory for class A is $191180.07

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

Part.txt zip:

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

REQUIREMENTS:

- C++

- must include comments

- must explain how to get program to read parts.txt file

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

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 << "\tR 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

Add a comment
Know the answer?
Add Answer to:
Write a C++ program with a function to read in the file parts.txt into either parallel...
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
  • These are my instructions: Your data should have been read in from the data file and stored into ...

    These are my instructions: Your data should have been read in from the data file and stored into an array. Next you need to calculate the following and display in a single Message box: Average score Highest score Lowest score Mode of the scores Your program should be written using methods and should be well documented internally and externally. Your output should be displayed using Message boxes. This is the .text file to use with the instructions: 20 21 22...

  • In C++: PART 2 -- Write a program that reads the following data file of NFL...

    In C++: PART 2 -- Write a program that reads the following data file of NFL quarterbacks, and... create a data structure to represent the data (dob, name, wins, losses, and salary) create an analysis class that will contain the list or vector of quarterbacks, and will analyze it as follows: print out the list of quarterbacks (from highest to lowest salary, AND from most wins to least wins). NOTE, there may be more than one least or one most....

  • Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays...

    Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays for the wind speed and for the temperature. You will then use the stored data to calculate and output the average wind speed and the average temperature. Create a constant with a value of 30 and use that to declare and loop through your arrays and as a divisor to produce your averages. Here is the data file (below). 1. 14 25 2. 12...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • Using the data in the file named “Dataset 1,” to determine is there was a change...

    Using the data in the file named “Dataset 1,” to determine is there was a change in the number of tons of paper used after the implementation of a recycling program in 25 different districts. a. Is this study using dependent or independent samples? b. What is your research hypothesis? c. Using Excel, calculate your test statistic and p-value. Paste the output here. d. What do you conclude? Below is the data set that is mentioned above Before Recycling 20...

  • A survey of 25 randomly selected customers found the ages shown (in years). The mean is...

    A survey of 25 randomly selected customers found the ages shown (in years). The mean is 32.28 years and the standard deviation is 9.35 31 21 44 32 34 11 48 42 23 45 39 35 31 28 29 43 34 37 20 44 33 27 24 a) Construct a 80% confidence interval for the mean age of all customers, assuming that the assumptions and conditions for the confidence interval have been met. b) How large is the margin of...

  • Student Name Student Number 18. Describe the center and spread of the data using either the...

    Student Name Student Number 18. Describe the center and spread of the data using either the mean and standard deviation or the five-number summary. Justify your choice by constructing a box-and-whisker plot for the data. Be sure to include the scale. A. 47, 16, 70, 80, 28, 33, 91, 55, 60, 45, 86, 54, 30, 98, 34, 87, 44, 35,64,58, 27,67, 72,68, 31, 95, 37, 41, 97,56, 49, 71, 84, 66, 45, 93 B. 40, 39, 37, 26, 25, 40,...

  • MtM204004 SP-2020 MEDGAR. EVERS C PILGRIM LEDGMR EVERS COLLEGE HOME WORKE Calculate the requested statistics for...

    MtM204004 SP-2020 MEDGAR. EVERS C PILGRIM LEDGMR EVERS COLLEGE HOME WORKE Calculate the requested statistics for the given m Sample Data: 68, B4 93, 64, e data 70 (b) median (c) mode (d) range (e) sample variance: (1) sample standard deviation: (2) Give the score 2 decimal place for each test score with the given class mean and standard deviation Assume the test scores are normally distributed (a) For a 92 on a test with a class mean of 78...

  • Janefrances I Onyenwe | RN47 TRANSITIONS TEST V2 22 Question: 37 of 50 23 OP22 [1...

    Janefrances I Onyenwe | RN47 TRANSITIONS TEST V2 22 Question: 37 of 50 23 OP22 [1 Mark) 24 Select an "S" for subjective information or an "O" for objective information Blood Pressure 25 26 27 28 29 30 31 A. O 32 33 34 35 36 37 38 39 40 41 42 43 44 45 1 Previous Next P Type here to search

  • 2. An experiment about a person’s ability to perform some task before and after taking one...

    2. An experiment about a person’s ability to perform some task before and after taking one of the two drugs was conducted. The subjects performed the task that involved mental addition. The subjects were randomly divided into two groups. Each group drank a beverage containing one of two drugs, labeled A, B (placebo). After a period of time for the drugs to take effect, each subject repeated the mental addition test. We want to relate the after test score to...

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
ADVERTISEMENT