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
Viefleur [7K]
1 year ago
6

C++ code only

Computers and Technology
1 answer:
nordsb [41]1 year ago
5 0

Using the computational language C++ it is possible to write a code is possible to create a function that stores items like this:

<h3>Writting the code in C++: </h3>

<em>//Header files</em>

<em>#include <iostream></em>

<em>#include <string></em>

<em>#include <fstream></em>

<em>using namespace std;</em>

<em>//Class inventoryItem</em>

<em>class inventoryItem</em>

<em>{</em>

<em>public:</em>

<em>//Variable declaration</em>

<em>string itemName;</em>

<em>string itemNumber;</em>

<em>double itemPrice;</em>

<em>int itemQuantity;</em>

<em>};</em>

<em>//implementation of menuItems() function.</em>

<em>void menuItems()</em>

<em>{</em>

<em>// Display the Menu</em>

<em>cout << "=============================\n";</em>

<em>cout << " 1) Add an item to memory\n"</em>

<em><< " 2) Search memory for an item \n"</em>

<em><< " 3) List what's in memory\n"</em>

<em><< " 4) Total value on hand\n"</em>

<em><< " 5) Save to a file\n"</em>

<em><< " 6) Read from a file\n"</em>

<em><< " 0) Exit Program" << endl;</em>

<em>}</em>

<em>//main method.</em>

<em>int main()</em>

<em>{</em>

<em>//variable declaration.</em>

<em>int mnuChoice, cn = 0, d;</em>

<em>string n;</em>

<em>double sum = 0, c;</em>

<em>int temp;</em>

<em>string a, b;</em>

<em>string inputBuffer;</em>

<em>inventoryItem items[100];</em>

<em>ifstream inventoryFile("inventory.txt");</em>

<em>if (inventoryFile)</em>

<em>{</em>

<em>//store the data from the file to memory.</em>

<em>while (!(inventoryFile.eof()))</em>

<em>{</em>

<em>inventoryFile >> items[cn].itemName;</em>

<em>inventoryFile >> items[cn].itemNumber;</em>

<em>inventoryFile >> inputBuffer;</em>

<em>items[cn].itemPrice = atof(inputBuffer.c_str());</em>

<em>inventoryFile >> inputBuffer;</em>

<em>items[cn].itemQuantity = atoi(inputBuffer.c_str());</em>

<em>cn++;</em>

<em>}</em>

<em>inventoryFile.close();</em>

<em>ifstream inventoryFile("inventory.txt");</em>

<em>ofstream fout;</em>

<em>fout.open("inventory.txt");</em>

<em>do</em>

<em>{</em>

<em>menuItems();</em>

<em>cout << cn << " items stored." << endl;</em>

<em>// Prompt and read choice form the user</em>

<em>cout << "Enter your choice: ";</em>

<em>cin >> mnuChoice;</em>

<em />

<em>switch (mnuChoice)</em>

<em>{</em>

<em>//Add a new inventory item to the data in memory.</em>

<em>case 1:</em>

<em>// Prompt and read name of the item from the user</em>

<em>cout << "Enter name of the item: ";</em>

<em>cin >> inputBuffer;</em>

<em>items[cn].itemName = inputBuffer;</em>

<em>// Prompt and read itemNumber from the user</em>

<em>cout << "Enter the itemNumber: ";</em>

<em>cin >> inputBuffer;</em>

<em>items[cn].itemNumber = inputBuffer;</em>

<em>// Prompt and read itemPrice from the user</em>

<em>cout << "Enter the itemPrice: ";</em>

<em>cin >> inputBuffer;</em>

<em>items[cn].itemPrice = atof(inputBuffer.c_str());</em>

<em>// Prompt and read itemQuantity from the user</em>

<em>cout << "Enter the itemQuantity: ";</em>

<em>cin >> inputBuffer;</em>

<em>items[cn].itemQuantity = atoi(inputBuffer.c_str());</em>

<em>cn++;</em>

<em>break;</em>

<em>//Search the inventory for an item by inventory number.</em>

<em>case 2:</em>

<em>temp = 0;</em>

<em>cout << "Enter inventory itemNumber: ";</em>

<em>cin >> n;</em>

<em>for (int i = 0; i < cn&&temp != 1; i++)</em>

<em>{</em>

<em>// If found, print the stored info</em>

<em>if (items[i].itemNumber.compare(n) == 0)</em>

<em>{</em>

<em>// Display the stored information</em>

<em>cout << "Item Name" << "\t" << "Item Number" << "\t" << " Item Price" << "\t" << " Item Quantity" << endl;</em>

<em>cout << items[i].itemName << "\t " << items[i].itemNumber << " \t" << items[i].itemPrice << "\t " << items[i].itemQuantity << endl;</em>

<em>temp = 1;</em>

<em>}</em>

<em>}</em>

<em>// otherwise print 'not found'.</em>

<em>if (temp == 0)</em>

<em>{</em>

<em>cout << "Not found!!!" << endl;</em>

<em>}</em>

<em>break;</em>

<em>//List the inventory on the screen</em>

<em>case 3:</em>

<em>cout << "The data in the memory is:" << endl;</em>

<em>cout << "Item Name" << "\t" << "Item Number" << "\t" << " Item Price" << "\t" << " Item Quantity" << endl;</em>

<em>for (int i = 0; i < cn; i++)</em>

<em>{</em>

<em>cout << items[i].itemName << "\t " << items[i].itemNumber << "\t " << items[i].itemPrice << " \t" << items[i].itemQuantity << endl;</em>

<em>}</em>

<em>break;</em>

<em>// Calculate the total value on hand.</em>

<em>case 4:</em>

<em>for (int i = 0; i < cn; i++)</em>

<em>{</em>

<em>// The sum is the accumulation of all the (quantities * prices).</em>

<em>sum += items[i].itemPrice*items[i].itemQuantity;</em>

<em>}</em>

<em>cout << "sum value on hand: " << sum << endl;</em>

<em>break;</em>

<em>// Save the inventory data in the given file</em>

<em>case 5:</em>

<em>for (int i = 0; i < cn; i++)</em>

<em>{</em>

<em>if (i == cn - 1)</em>

<em>{</em>

<em />

<em>fout << items[i].itemName << " \t" << items[i].itemNumber << " \t" << items[i].itemPrice << " \t" << items[i].itemQuantity;</em>

<em>}</em>

<em>else</em>

<em>{</em>

<em />

<em>fout << items[i].itemName << " \t" << items[i].itemNumber << " \t" << items[i].itemPrice << " \t" << items[i].itemQuantity << endl;</em>

<em>}</em>

<em>}</em>

<em>break;</em>

<em>// Read the data from the file</em>

<em>case 6:</em>

<em>while (!(inventoryFile.eof()))</em>

<em>{</em>

<em>inventoryFile >> a;</em>

<em>inventoryFile >> b;</em>

<em>inventoryFile >> inputBuffer;</em>

<em>c = atof(inputBuffer.c_str());</em>

<em>inventoryFile >> inputBuffer;</em>

<em>d = atoi(inputBuffer.c_str());</em>

<em>// Dsiplays the data in the file</em>

<em />

<em>cout << a << " " << b << " " << c << " " << d << endl;</em>

<em>}</em>

<em>break;</em>

<em>}</em>

<em>// Check the menu choice is not equal to 0,</em>

<em>//if it is equal exit from the loop</em>

<em>} while (mnuChoice != 0);</em>

<em>// File closed</em>

<em>fout.close();</em>

<em>inventoryFile.close();</em>

<em>}</em>

<em>// If the inventory file does not exist</em>

<em>// and then display the error message.</em>

<em>else</em>

<em>{</em>

<em>cout << "File does not exist!" << endl;</em>

<em>}</em>

<em />

<em>return 0;</em>

<em>}</em>

See more about C++ code at brainly.com/question/17544466

#SPJ1

You might be interested in
your computer running Windows 10 is doing some very strange things with operating system you are fairly certain it is not a hard
AlekseyPX
It might be a virus, when my computer starts going crazy i just bought a fixme stick and they work amazing or trying getting an expert to fix it if it worse
6 0
3 years ago
Read 2 more answers
If wire rope guard rails are used what must also be done?
zzz [600]
A Deceleration device.
A rope, wire rope, or strap with connectors at each end for connecting the 5 point body harness to a lifeline, or anchorage.

Look under OSHA Flashcards on Quizlet and..
OSHA: Fall Protection in Construction, Subpart M and just past that area in the document.

This is from that document on page 13 and 14:  
Fall Restraint Systems:  While fall restraint systems are not mentioned in Subpart M, OSHA recognizes a fall restraint system as a means of prevention. The system, if properly used, tethers a worker in a manner that will not allow a fall of any distance. This system is comprised of a body belt or body harness, an anchorage, connectors, and other necessary equipment. Other components typically include Photo: Skip Pennington OCCUPATIONAL SAFETY AND HEALTH ADMINISTRATION 1 4 a lanyard, a lifeline, and other devices. For a restraint system to work, the anchorage must be strong enough to prevent the worker from moving past the point where the system is fully extended, including an appropriate safety factor. *In a November 2, 1995 interpretation letter to Mr. Dennis Gilmore, OSHA suggested that, at a minimum, a fall restraint system must have the capacity to withstand at least 3,000 pounds or twice the maximum expected force that is needed to restrain the person from exposure to the fall hazard. In determining this force, consideration should be given to site-specific factors such as the force generated by a person (including his/her tools, equipment, and materials) walking, slipping, tripping, leaning, or sliding along the work surface.

8 0
3 years ago
You type a complex formula in cell A20. You would like to make the same calculation in cells B20 through H20. To be efficient, y
fomenos

Answer:

Use autofill

Explanation:

In excel "autofill" is used to fill data in cells, which follows the pattern or logic applied in other cells. To apply same calculation of A20 in B20 to H20, we use autofill technique. Instead of copy pasting, or manually entering the complex formula this method is more suitable.  

4 0
3 years ago
How are the stop lamp bulbs connected in relation to each other? a. In parallel. b. In series. c. In series/parallel. d. None of
laiz [17]

In Parallel option A is true

Because In parallel circuit, the voltage across each components is same and the total current is sum of the currents through each component.

6 0
3 years ago
When configuring an adsl installation where should you to install the dsl filters?
xxTIMURxx [149]

<span>Filter installation in ADSL setup, the DSL filters should on connections leading to an analog phone. The DSL modem and phone were share on the same jack. ADSL is a type of digital subscriber line machinery, a data communication that enables faster data transfer over copper telephone lines.</span>

5 0
3 years ago
Other questions:
  • Which routine best ensures email safety?
    7·2 answers
  • Imported data that maintains a refreshable link to its external source is called ____ data.
    7·1 answer
  • Angela recently purchased a new Android smartphone. While purchasing the phone, Angela was told that she would need to set up a
    11·1 answer
  • The way that text and images are arranged on a slide is called _________. A. Layout B. Background C. Style D. Charting
    10·2 answers
  • 10.7 LAB: Fat-burning heart rate Write a program that calculates an adult's fat-burning heart rate, which is 70% of 220 minus th
    8·1 answer
  • A microsoft windows os component responsible for representing graphical objects and transmitting them to output devices such as
    15·1 answer
  • Adam is an Engineering student but has decided that he does not want to work in that field, but in the Manufacturing career clus
    11·2 answers
  • Reesa works for a large real estate company. She is responsible for installing and supporting the company's internet systems, in
    5·1 answer
  • How do you get The gold chip on Megaman x3 snes I want To know
    11·1 answer
  • What is software engineering? What are the objectives of software engineering?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!