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
How do you get a code in C to count down from 5??
Valentin [98]

Answer:

This is what the code should do:

“Lift off in T minus

5

4

3

2

1

Blast-off!”

When I run it, it just keeps printing ''Sum = 5'' forever.

Explanation:

Code:

int main(void) {

int sum = 5;  

int i;      

printf("Lift off in T minus\n");

for (i = 0; i < 5; i=i+i) {

   sum = sum - i;  

   printf("sum = %d\n",sum);  

}  

printf("Blast-off",sum);  

return 0;

5 0
3 years ago
The common channel signaling (CCS) system provides a separate network dedicated to control and signaling over the PSTN. This ena
dezoksy [38]
The correct answer is A.True
8 0
2 years ago
What is a computer that provides services and connections to other computers on a network is called a ________ ?
melisa1 [442]
They are called servers .. you can also call them hosts
6 0
3 years ago
You want to decide whether you should drive your car to work or take the train. You know the one-way distance from your home to
tester [92]

<u>Explanation:</u>

Remember, an algorithm in simple words means a set of instructions or steps to be followed in other to solve a problem.

Note, to decide which commute is cheaper, it means the output of the algorithm should be the cheaper way to commute.

Using pseudocode do the following;

  • determine the inputs and outputs of the problem
  • arrange the identified problem into micro-tasks
  • describe each micro-tasks in the pseudocode
  • Test the pseudocode by solving the problem.

                       

3 0
2 years ago
question 2 you are using a spreadsheet to organize a list of upcoming home repairs. column a contains the list of repairs, and c
Helga [31]

Since you are using a spreadsheet to organize a list of upcoming home repairs. The spreadsheet tool that can you use to create a drop-down list of priorities for each cell in column b is  Data validation.

<h3>What is the purpose of data validation?</h3>

The use of rows and columns of data, a spreadsheet is a type of computer program that can store, display, and manipulate data. One of the most used tools that can be used with personal computers is a spreadsheet. A spreadsheet is typically made to store numerical data and brief text passages.

Therefore, in the context of the above, before using data for a business operation, it is best to validate it to ensure its accuracy, integrity, and structure. The output of a data validation operation can be used to generate data for business intelligence, data analytics, or training a machine learning model.

Learn more about spreadsheet from

brainly.com/question/4965119
#SPJ1

6 0
8 months ago
Other questions:
  • in what way do rules and laws created to address public problems affect individuals groups and business
    13·1 answer
  • In the year of our Lord 66, the Emperor Nero, being at that time in the twenty-ninth year of his life and the thirteenth of his
    10·1 answer
  • Because health and safety are important in all workplaces, not just hazardous ones, what working conditions must ALL employers p
    10·2 answers
  • "Create a Python program named detect_column_level_data_entry_errors. When complete, you will run this program to produce a diag
    11·1 answer
  • (Complete the Problem-Solving discussion in Word for Programming Challenge 2 on page 404. Your Problem-Solving discussion should
    13·1 answer
  • If you see an advertisement for a 3TB portable drive, this is most likely a reference to the device having a capacity of three
    10·1 answer
  • Select a classification for File2 so that: Alice can read and write to File2 Bob and Charlie can write to File2, but can't read
    6·1 answer
  • When might be the best time to start saving for retirement?
    11·2 answers
  • What can u access various sites on
    12·1 answer
  • Which of the following could be part of an algorithm?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!