FOR THIS C PROGRAMMING QUESTION BELOW I HAD THIS SOLUTION B
FOR THIS C PROGRAMMING QUESTION BELOW I HAD THIS SOLUTION , BUT IT IS NOT RUNNING ON MATRIX SAYING THAT FATAL ERROR.... PLEASE HELP:
QUESTION:
In thisworkshop, you are going to use a C structuretype to represent anItemof an inventory. Apersonis able to viewthe inventory, add an item to the inventory, and check prices for items in the inventory. Here is the C structure type that should be used in this workshop:
structItem{
intsku_;
floatprice_;
intquantity_;
};
sku_: Stock Keeping Unit number for an item.
price_: Price for an item.
quantity_: Quantity of an item.
Also, use constor #define directivesto define the following number:
MAX_ITEMS: the maximum number of items that exists in an inventory. Assume MAX_ITEMSis 10.
In your main function, implement the following steps:
1. Define the following variables in your main program:
structItem item[MAX_ITEMS]; //An array of Item representing the inventory
intsize=0; //Number of items in the inventory. The inventory is initially empty.
2. Display awelcome massage:
Welcome to the Shop
===================
3. Display the following menu, and prompt the user to select anoption from the menu.
Please select from the following options:
1) Display the inventory.
2) Add to the inventory.
0)Exit.
4.You must verify that the input integer is between zero and two inclusive.If the input number is not between zero and two, you must display a warning and prompt the user again. You can assume the user will only enter numbers.The program only exits when the user selects 0 (Exit) on the menu screen (looping required).
5. Depending on the user’s input, one of the following scenarios happens.
Add to the inventory:
Prompt the user to input the SKU number and the quantity of an item that one wants to add to the inventory.
Search through the inventory (the item array) to find if the item exists in the array.
If the item is found in the inventory, do the following:
Update the quantity of the item by adding the quantity read to the quantity of the item in array.
Display a message that the item quantity is successfully updated.
If the item is not found in the inventory, do the following:
If the inventory is full (size== MAX_ITEMS), display the inventory is full.
If the inventory is not full, the function prompts the user to input item price. Then, update the inventory. Hint: Use the inventory array, and size as its index, and assign sku, price and quantity. Increment size.
Display the inventory:
Display the inventory in an informative format. See sample output.
Exit:
Display a goodbye message:
Goodbye!
Your program is complete if your output matches the following output. Red numbers show the user’s input.
Welcome to the Shop
===================
Please select from the following options:
1) Display the inventory.
2) Add to shop.
0) Exit.
Select:8
Invalid input, try again: Please select from the following options:
1) Display the inventory.
2) Add to shop.
0) Exit.
Select:2
Please input a SKU number:2341
Quantity:3
Price:12.78
The item is successfully added to the inventory.
Please select from the following options:
1) Display the inventory.
2) Add to shop.
0) Exit.
Select:2
Please input a SKU number:4567
Quantity:9
Price:98.2
The item is successfully added to the inventory.
Please select from the following options:
1) Display the inventory.
2) Add to shop.
0) Exit.
Select:1
Inventory
=========================================
Sku Price Quanity
2341 12.78 3
4567 98.20 9
=========================================
Please select from the following options:
1) Display the inventory.
2) Add to shop.
0) Exit.
Select:2
Please input a SKU number:2341
Quantity:5
The item exists in the repository, quanity is updated.
Please select from the following options:
1) Display the inventory.
2) Add to shop.
0) Exit.
Select:1
Inventory
=========================================
Sku Price Quanity
2341 12.78 8
4567 98.20 9
=========================================
Please select from the following options:
1) Display the inventory.
2) Add to shop.
0) Exit.
Select:0
Good bye!
Part 2 (20%) – add price check option
Add the following option to the menu:
3) Price check.
Make sure thattheentry validation (step 4) in menu entry is updated to include 3 as a valid entry.
Then add the following logic:
Price check
Prompt the user to input the SKU number for the item they are looking for.
Search through the inventory and display the cost of the item.
Note that if the item does not exist in the inventory, the program displays an informative message.
Program completion
Your program is complete if your output matches the following output. Red numbers show the user’s input.
Welcome to the Shop
===================
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:2
Please input a SKU number:2341
Quantity:4
Price:12.78
The item is successfully added to the inventory.
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:2
Please input a SKU number:4567
Quantity:9
Price:98.20
The item is successfully added to the inventory.
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:1
Inventory
=========================================
Sku Price Quanity
2341 12.78 4
4567 98.20 9
=========================================
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:3
Please input the sku number of the item:
2322
Item does not exist in the shop! Please try again.
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:3
Please input the sku number of the item:
2341
Item 2341 costs $12.78
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:0
Good bye!
SOLUTION I DID: (IT IS NOT RUNNING PLEASE EDIT THIS )
#include \"stdafx.h\"
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include <stdlib.h>
#define MAX_ITEMS 10
struct Item{
int sku_;
float price_;
int quantity_;
};
int _tmain(int argc, _TCHAR* argv[])
{
struct Item item[MAX_ITEMS];
int size=0,choice;
int sku,quantity,found=0;
float price;
int n=0,i;
printf(\"Welcome to the Shop\ ===================\ \");
while(1)
{
printf(\"Please select from the following options:\ 1) Display the inventory.\ 2) Add to the inventory.\ 3) Price check.\ 0) Exit.\ Select:\");
scanf(\"%d\",&choice);
switch(choice)
{
case 0:
printf(\"Goodbye!\");
getch();
exit(0);
break;
case 1:
printf(\"Inventory\ =========================================\ Sku\\tPrice\\tQuanity\ \");
for(i=0;i<n;i++)
{
printf(\"%d\\t%.2f\\t%d\ \",item[i].sku_,item[i].price_,item[i].quantity_);
}
printf(\"=========================================\ \");
break;
case 2:
printf(\"Please input a SKU number:\");
scanf(\"%d\",&sku);
for(i=0;i<n;i++)
{
if(item[i].sku_==sku)
{
printf(\"Quantity:\");
scanf(\"%d\",&quantity);
item[i].quantity_=item[i].quantity_+quantity;
found=1;
printf(\"The item exists in the repository, quanity is updated.\ \");
}
}
if(found==0 && i==10)
{
printf(\"The inventory is full\ \");
}
else if(found==0)
{
printf(\"Quantity:\");
scanf(\"%d\",&quantity);
printf(\"Price:\");
scanf(\"%f\",&price);
item[i].sku_=sku;
item[i].quantity_=quantity;
item[i].price_=price;
n++;
printf(\"The item is successfully added to the inventory.\ \");
}
break;
case 3:
printf(\"Please input the sku number of the item:\ \");
scanf(\"%d\",&sku);
found=0;
for(i=0;i<n;i++)
{
if(item[i].sku_==sku)
{
printf(\"Item %d costs $%.2f\ \",item[i].sku_,item[i].price_);
found=1;
break;
}
}
if(found==0)
{
printf(\"Item does not exist in the shop! Please try again.\ \");
}
break;
default:
printf(\"Invalid input, try again: \");
}
fflush(stdin);
}
return 0;
}
Solution
solution
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include <stdlib.h>
#define MAX_ITEMS 10
struct Item{
int sku_;
float price_;
int quantity_;
};
int main( int argc, char *argv[] )
{
struct Item item[MAX_ITEMS];
int size=0,choice;
int sku,quantity,found=0;
float price;
int n=0,i;
printf(\"Welcome to the Shop\ ===================\ \");
while(1)
{
printf(\"Please select from the following options:\ 1) Display the inventory.\ 2) Add to the inventory.\ 0) Exit.\ Select:\");
scanf(\"%d\",&choice);
switch(choice)
{
case 0:
printf(\"Goodbye!\");
getch();
exit(0);
break;
case 1:
printf(\"Inventory\ =========================================\ Sku\\tPrice\\tQuanity\ \");
for(i=0;i<n;i++)
{
printf(\"%d\\t%.2f\\t%d\ \",item[i].sku_,item[i].price_,item[i].quantity_);
}
printf(\"=========================================\ \");
break;
case 2:
printf(\"Please input a SKU number:\");
scanf(\"%d\",&sku);
for(i=0;i<n;i++)
{
if(item[i].sku_==sku)
{
printf(\"Quantity:\");
scanf(\"%d\",&quantity);
item[i].quantity_=item[i].quantity_+quantity;
found=1;
printf(\"The item exists in the repository, quanity is updated.\ \");
}
}
if(found==0 && i==10)
{
printf(\"The inventory is full\ \");
}
else if(found==0)
{
printf(\"Quantity:\");
scanf(\"%d\",&quantity);
printf(\"Price:\");
scanf(\"%f\",&price);
item[i].sku_=sku;
item[i].quantity_=quantity;
item[i].price_=price;
n++;
printf(\"The item is successfully added to the inventory.\ \");
}
break;
default:
printf(\"Invalid input, try again: \");
}
fflush(stdin);
}
return 0;
}
output
Please select from the following options:
1) Display the inventory.
2) Add to shop.
0) Exit.
Select:8
Invalid input, try again: Please select from the following options:
1) Display the inventory.
2) Add to shop.
0) Exit.
Select:2
Please input a SKU number:2341
Quantity:3
Price:12.78
The item is successfully added to the inventory.
Please select from the following options:
1) Display the inventory.
2) Add to shop.
0) Exit.
Select:2
Please input a SKU number:4567
Quantity:9
Price:98.2
The item is successfully added to the inventory.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include <stdlib.h>
#define MAX_ITEMS 10
struct Item{
int sku_;
float price_;
int quantity_;
};
int main()
{
struct Item item[MAX_ITEMS];
int size=0,choice;
int sku,quantity,found=0;
float price;
int n=0,i;
printf(\"Welcome to the Shop\ ===================\ \");
while(1)
{
printf(\"Please select from the following options:\ 1) Display the inventory.\ 2) Add to the inventory.\ 3) Price check.\ 0) Exit.\ Select:\");
scanf(\"%d\",&choice);
switch(choice)
{
case 0:
printf(\"Goodbye!\");
getch();
exit(0);
break;
case 1:
printf(\"Inventory\ =========================================\ Sku\\tPrice\\tQuanity\ \");
for(i=0;i<n;i++)
{
printf(\"%d\\t%.2f\\t%d\ \",item[i].sku_,item[i].price_,item[i].quantity_);
}
printf(\"=========================================\ \");
break;
case 2:
printf(\"Please input a SKU number:\");
scanf(\"%d\",&sku);
for(i=0;i<n;i++)
{
if(item[i].sku_==sku)
{
printf(\"Quantity:\");
scanf(\"%d\",&quantity);
item[i].quantity_=item[i].quantity_+quantity;
found=1;
printf(\"The item exists in the repository, quanity is updated.\ \");
}
}
if(found==0 && i==10)
{
printf(\"The inventory is full\ \");
}
else if(found==0)
{
printf(\"Quantity:\");
scanf(\"%d\",&quantity);
printf(\"Price:\");
scanf(\"%f\",&price);
item[i].sku_=sku;
item[i].quantity_=quantity;
item[i].price_=price;
n++;
printf(\"The item is successfully added to the inventory.\ \");
}
break;
case 3:
printf(\"Please input the sku number of the item:\ \");
scanf(\"%d\",&sku);
found=0;
for(i=0;i<n;i++)
{
if(item[i].sku_==sku)
{
printf(\"Item %d costs $%.2f\ \",item[i].sku_,item[i].price_);
found=1;
break;
}
}
if(found==0)
{
printf(\"Item does not exist in the shop! Please try again.\ \");
}
break;
default:
printf(\"Invalid input, try again: \");
}
fflush(stdin);
}
return 0;
}
output
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:2
Please input a SKU number:2341
Quantity:4
Price:12.78
The item is successfully added to the inventory.
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:2
Please input a SKU number:4567
Quantity:9
Price:98.20
The item is successfully added to the inventory.
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:1
Inventory
=========================================
Sku Price Quanity
2341 12.78 4
4567 98.20 9
=========================================
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:3
Please input the sku number of the item:
2322
Item does not exist in the shop! Please try again.
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:3
Please input the sku number of the item:
2341
Item 2341 costs $12.78
Please select from the following options:
1) Display the inventory.
2) Add to shop.
3) Price check.
0) Exit.
Select:0
Good bye!












