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
1). What is the proper name of the healing agent?
VLD [36.1K]

Answer:

1. Hydroxychloroquine is the healing agent

2. Chloroquine does not have a hydroxyl group attached    

Explanation:              

Chloroquine and  Hydroxy chloroquine have been in use for many decades for malaria treatment and prophylaxis.    

It was found that there was a high possibility of being poisoned when treatment of chloroquine was administered. In 1946 the hydroxyl group replaced the CH3₃  (methyl) group with an ⁻OH (hydroxyl) group as indicated by the attachment. Toxicity by chloroquine was reduced to 40% when the ⁻OH group was attached.

Later chloroquine was added to formulas that are used for cleaning pools and fish tanks.

Both of these chemicals should be used by following the guide lines and under medical supervision if taken for any illness.

3 0
3 years ago
Ayuda no encuentro la información de estas tres preguntas:
Morgarella [4.7K]

Answer:

Sorry i don't know.........!!!!!!!!!!

Explanation:

3 0
3 years ago
A rang check that could be use to validate... Date and time
lisov135 [29]

A range check is often used when a person works with data consisting of numbers, dates and times, or currency, ensuring that a number is within a specific range.

Explanation:

  • Range checks allow a person to set suitable boundaries.
  • An example of a range check is checking to see if the value of a 16-bit integer is within the capacity of a 16-bit integer. When a range check is performed, upper and lower boundaries are specified.
  • Range check is a validation check which can be applied to numeric fields. This is done to ensure that only numbers within a certain domain can be entered into a field. Remember that this does not necessarily mean that the data entered will be correct. But it will certainly lie within reasonable limits.
  • A method of validation which checks that data falls between an upper and lower acceptable value.
  • This validation check which can be applied to numeric fields. This is done to ensure that only numbers within a certain domain can be entered into a field. Remember that this does not necessarily mean that the data entered will be correct. But it will certainly lie within reasonable limits.
5 0
3 years ago
Recursion occurs when a function (calls itself/ calls another function/ calls a function from the python standard library)
lesantik [10]

Answer:

Recursion is calling of a function from within that same function

5 0
3 years ago
In javascript, what can I put after player. to make my character go in the center of the screen? like example:
melamori03 [73]

Answer:

0,0

Explanation:

you could try to use the x-axis and y-axis to put your character in a certain place. I'm not totally sure if that is how javascript works, but for the x-axis and y-axis, the center is always(0,0).

6 0
3 years ago
Other questions:
  • Perhaps programs used for business purposes ought to conform to higher standards of quality than games. With respect to software
    7·1 answer
  • What is the difference between Data and information?​
    7·1 answer
  • Which of the following statements about Linux is not​ true? A. Linux works on all the major hardware platforms. B. It plays a ma
    5·1 answer
  • In a bubble sort, you use a(n) ____ loop to make pair comparisons.
    5·1 answer
  • Describe one example of how technology assits people with data and sample collection
    14·2 answers
  • Next, Leah wants to add a content slide that allows her to insert a table.
    8·2 answers
  • Which type of loan is based on financial need
    6·1 answer
  • what social media application that affect our day to day activities, and how did it improve our skill
    9·1 answer
  • A(n) __________ structure is a structure that causes a statement or a set of statements to execute repeatedly.
    13·1 answer
  • Below you will find the requirements to identify the Account Diversity Grade of a user. Read the requirements carefully and iden
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!