ADD PRICE CHECK OPTION TO THE FOLLOWING CODE BELOW AND DO NOT USE BREAK TO CONTR
ID: 3582380 • Letter: A
Question
ADD PRICE CHECK OPTION TO THE FOLLOWING CODE BELOW AND DO NOT USE BREAK TO CONTROL THE LOOP
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.
my code:
#include <stdio.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;
int tempSku, tempQuantity;
int i;
int c;
float tempPrice;
printf(" Welcome to the Shop =================== ");
do
{
printf(" Please select from the following options: "
" 1) Display the inventory. "
" 2) Add to shop. "
" 3) Price Check. "
" 0)Exit. Select: ");
scanf("%d", &c);
switch(c){
case 1:
printf(" Inventory ========================================= ");
printf(" Sku Price Quantity ");
for(i = 0; i < size; ++i){
printf(" %d %f %d ",item[i].sku_, item[i].price_, item[i].quantity_);
}
printf(" ========================================= ");
break;
case 2:
printf(" Please input a SKU number: ");
scanf("%d", &tempSku);
printf(" Quantity: ");
scanf("%d",&tempQuantity);
printf(" Price: ");
scanf("%f", &tempPrice);
if(size >= MAX_ITEMS){
printf(" Item can't be added, Inventory is full ");
break;
}
item[size].sku_ = tempSku;
item[size].quantity_ = tempQuantity;
item[size].price_ = tempPrice;
size++;
printf(" Item is successfully added to the inventory ");
break;
case 0:
exit(0);
default:
printf(" Invalid Input, try again ");
printf("Good Bye");
}
}
while (c>=0 && c<4);
return 0;
}
EXPECTED OUTPUT
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!
Explanation / Answer
#include <stdio.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;
int tempSku, tempQuantity,sku,count = 0;
int i;
int c;
float tempPrice;
printf(" Welcome to the Shop =================== ");
do
{
printf(" Please select from the following options: "
" 1) Display the inventory. "
" 2) Add to shop. "
" 3) Price Check. "
" 0)Exit. Select: ");
scanf("%d", &c);
switch(c){
case 1:
printf(" Inventory ========================================= ");
printf(" Sku Price Quantity ");
for(i = 0; i < size; ++i){
printf(" %d %f %d ",item[i].sku_, item[i].price_, item[i].quantity_);
}
printf(" ========================================= ");
break;
case 2:
printf(" Please input a SKU number: ");
scanf("%d", &tempSku);
printf(" Quantity: ");
scanf("%d",&tempQuantity);
printf(" Price: ");
scanf("%f", &tempPrice);
if(size >= MAX_ITEMS){
printf(" Item can't be added, Inventory is full ");
break;
}
item[size].sku_ = tempSku;
item[size].quantity_ = tempQuantity;
item[size].price_ = tempPrice;
size++;
printf(" Item is successfully added to the inventory ");
break;
case 0:
exit(0);
//you didnt write the case for 3 click
case 3:
printf(" Please input the sku number of the item:");
scanf("%d", &sku);
count = 0;
for(i = 0; i < size; ++i){
if(sku == item[i].sku_)
{
count = i+1;
}
}
if(count > 0)
printf(" Item %d costs %f",item[count-1].quantity_, item[count-1].price_);
else
printf(" Item does not exist in the shop! Please try again.");
break;
default:
printf(" Invalid Input, try again ");
printf("Good Bye");
}
}
while (c>=0 && c<4);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.