1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
creativ13 [48]
4 years ago
12

Write a program that reads raw data from a file that contains an inventory of items. The items should be sorted by name, and the

total value of the inventory calculated. A nice report is printed to a data file including the sorted inventory data.

Computers and Technology
1 answer:
Anika [276]4 years ago
4 0

Answer:

//C++ code

#include<iostream>

#include<fstream>

#include<iomanip>

using namespace std;

/*===============================================*/

// a structure called prod

struct prod

{

string name;// – a string, assumed to have no spaces

double price; // –adollaramount

int inStock; // – the number of items currently in stock.

};

//Fucntion Prototypes

void readInventory(prod products[], int& size);

/*===============================================*/

/*returns: a dollar amount, the total value of the inventory*/

double totalValue(prod products[], int size);

/*===============================================*/

// Sorts the given array by name

void sort(prod products[], int size);

/*===============================================*/

/*a report to a file called “report.txt”.

When the file fails to open, print

“Unable to open output file.” and end.*/

void writeReport(prod products[], int size);

/*===============================================*/

//Function definitions

/*===============================================*/

void writeReport(prod products[], int size)

{

ofstream outfile("report.txt");

outfile << fixed << setprecision(2);

if (!outfile)

{

cout << "Unable to open output file.\n";

return;

}

outfile << "+------------------------------+\n | Current Inventory |\n\

\n+------------------------------+\nNAME PRICE #\n\

\n-------------------- ------- ---\n";

for (int i = 0; i < size; i++)

{

outfile << products[i].name << " " << products[i].price << " " << products[i].inStock << endl;

}

outfile << "+------------------------------+\n";

outfile << "Number of products: " << size << endl;

outfile << "Inventory total value: " << totalValue(products, size) << endl;

cout << "Report written to file\n";

//close the output stream

outfile.close();

}

/*===============================================*/

void sort(prod products[], int size)

{

for (int i = 0; i < size; i++)

{

for (int j = i+1; j < size; j++)

{

if (products[i].name > products[j].name)

{

//swapping

prod temp = products[i];

products[i] = products[j];

products[j] = temp;

}

}

}

}

/*===============================================*/

double totalValue(prod products[], int size)

{

double total = 0;

for (int i = 0; i < size; i++)

{

total += products[i].inStock * products[i].price;

}

return total;

}

/*===============================================*/

void readInventory(prod products[], int& size)

{

// Open a file called “inventory.txt”

//and read the data from the file into the partial array

//.If the file fails to open,

//print “Unable to open input file”

//and set the number of products to 0.

size = 0;

ifstream inFile("inventory.txt");

if (!inFile)

{

cout << "Unable to open input file.\n";

return;

}

string name;

double price;

int qunatity;

while (inFile >> price >>qunatity >>name)

{

if (price != -1 && qunatity != -1 && name != "endofdata")

{

products[size].name = name;

products[size].inStock = qunatity;

products[size].price = price;

size++;

}

}

cout << "inventory read\n";

//close the stream

inFile.close();

}

/*===============================================*/

int main()

{

//declares a partial array of products

prod products[100];

int size;

// invokes readInventory()

readInventory(products, size);

// invokes sort()

sort(products, size);

// invokes writeReport()

writeReport(products, size);

// ends with the standard system pause.

system("pause");

return 0;

}

please see attachment for inventory text and output.

You might be interested in
What does Mishler mean by the foreground-background interaction? How can this affect the photographs, based on the examples in t
soldier1979 [14.2K]
He ends up cropping his photos too closely and that is because he chooses what he wants people to see and hides what he does not want them to see. But cropping too much can make the photo can be a bit extreme. Hopefully someone will find this helpful!
5 0
3 years ago
Read 2 more answers
write the few lines of code that prompts the user to enter a series of exam scores, stopping when the user has entered an exam s
dmitriy555 [2]

Answer: 5b + 3.79

Explanation:

8 0
1 year ago
What is a fragmented disk?
jeka94
Disk fragmentation occurs when a file is broken up into pieces to fit on the disk. Because files are constantly being written, deleted and resized, fragmentation is a natural occurrence. When a file is spread out over several locations, it takes longer to read and write.
7 0
3 years ago
HELP WILL GIVE BRAINLIEST
VladimirAG [237]

lol this question is wild. But no once you delete the convo it is gone. Sorry person.

5 0
3 years ago
Starting a computer after it has been turned off is term as​
kifflom [539]

Answer:

Booting

Explanation:

7 0
3 years ago
Other questions:
  • Which of the following can indicate what careers will be enjoyable ?
    5·1 answer
  • Which tab allows you to modify the width of a page margins in Microsoft word?
    7·2 answers
  • Which of the following is the general term used to describe software code that isdesigned to cause damage to a computer system?
    10·1 answer
  • Assume you need to declare a type parameter that can represent any type that implements the interface Comparable. Which type par
    15·1 answer
  • The use of computer in a modern world is as a result of complexity. true or false
    6·1 answer
  • You are configuring two PCs for your network, PC-A is given an IP address of 192.168.1.18 and PC-B is given an IP address of 192
    15·1 answer
  • When defining a variable to be used to hold textual information containing multiple characters (letters, numbers, and/or symbols
    5·1 answer
  • Wassup anybody tryna play 2k
    6·2 answers
  • Based on Microsoft's Component Object Model (COM), _____ is database middleware that adds object-oriented functionality for acce
    11·1 answer
  • An array called numbers contains 35 valid integer numbers. Determine and display how many of these values are greater than the a
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!