THIS IS A C PROGRAMMING QUESTION MY OUTPUT SHOULD BE LIKE TH
THIS IS A C PROGRAMMING QUESTION:
MY OUTPUT SHOULD BE LIKE THE IMAGE BELOW. I DID HALF CODING (EXCEPT THE YELLOW MARKED PART, WHERE I HAVE TO SELECT \"3\")
***PLEASE COMPLETE MY PROGRAM SO THAT I CAN GET THE SAME OUTPUT BY SELECTING \"3\"
MY CODE: (THIS RUNS BEFORE YELLOW MARKED LINE)
#include <stdio.h>
#include <stdlib.h>
const MAX_ITEMS = 10;
struct Item{
int sku_;
float price_;
int quantity_;
};
int main(){
struct Item item[MAX_ITEMS];
int size = 0;
int tempSku, tempQuantity;
float tempPrice;
printf(\"\ \\t\\tWelcome to the Shop\ \\t\\t===================\ \ \");
while(1){
printf(\"\\t\\tPlease select from the following options:\ \"
\"\\t\\t1) Display the inventory.\ \"
\"\\t\\t2) Add to shop.\ \"
\"\\t\\t3) Price Check.\ \"
\"\\t\\t0)Exit.\ \\t\\tSelect: \");
int c;
scanf(\"%d\", &c);
switch(c){
case 1:
printf(\"\ \\t\\tInventory\ \\t\\t=========================================\ \");
printf(\"\\t\\tSku\\t\\tPrice\\t\\tQuantity\ \ \");
int i;
for(i = 0; i < size; ++i){
printf(\"\\t\\t%d\\t\\t%f\\t\\t%d\ \ \",item[i].sku_, item[i].price_, item[i].quantity_);
}
printf(\"\\t\\t=========================================\ \ \");
break;
case 2:
printf(\"\ \\t\\tPlease input a SKU number: \");
scanf(\"%d\", &tempSku);
printf(\"\\t\\tQuantity: \");
scanf(\"%d\",&tempQuantity);
printf(\"\\t\\tPrice: \");
scanf(\"%f\", &tempPrice);
if(size >= MAX_ITEMS){
printf(\"\ \\t\\tItem can\'t be added, Inventory is full\ \ \");
break;
}
item[size].sku_ = tempSku;
item[size].quantity_ = tempQuantity;
item[size].price_ = tempPrice;
size++;
printf(\"\ \\t\\tItem is successfully added to the inventory\ \ \");
break;
case 0:
return 0;
default:
printf(\"\ \\t\\tInvalid Input, try again\ \ \");
}
}
return 0;
}
EXPECTED OUTPUT:
Solution
#include <stdio.h>
#include <stdlib.h>
int const MAX_ITEMS = 10;
//int f=0;
struct Item{
int sku_;
float price_;
int quantity_;
};
int main()
{
struct Item item[MAX_ITEMS];
int size = 0;
int tempSku, tempQuantity;
float tempPrice;
printf(\"\ \\t\\tWelcome to the Shop\ \\t\\t===================\ \ \");
while(1)
{
printf(\"\\t\\tPlease select from the following options:\ \"
\"\\t\\t1) Display the inventory.\ \"
\"\\t\\t2) Add to shop.\ \"
\"\\t\\t3) Price Check.\ \"
\"\\t\\t0)Exit.\ \\t\\tSelect: \");
int c;
scanf(\"%d\", &c);
switch(c)
{
case 1:
printf(\"\ \\t\\tInventory\ \\t\\t=========================================\ \");
printf(\"\\t\\tSku\\t\\tPrice\\t\\tQuantity\ \ \");
int i;
for(i = 0; i < size; ++i)
{
printf(\"\\t\\t%d\\t\\t%f\\t\\t%d\ \ \",item[i].sku_, item[i].price_, item[i].quantity_);
}
printf(\"\\t\\t=========================================\ \ \");
break;
case 2:
printf(\"\ \\t\\tPlease input a SKU number: \");
scanf(\"%d\", &tempSku);
printf(\"\\t\\tQuantity: \");
scanf(\"%d\",&tempQuantity);
printf(\"\\t\\tPrice: \");
scanf(\"%f\", &tempPrice);
if(size >= MAX_ITEMS)
{
printf(\"\ \\t\\tItem can\'t be added, Inventory is full\ \ \");
break;
}
item[size].sku_ = tempSku;
item[size].quantity_ = tempQuantity;
item[size].price_ = tempPrice;
size++;
printf(\"\ \\t\\tItem is successfully added to the inventory\ \ \");
break;
case 0:
printf(\"\ \ \");
printf(\"\\t\\tGood bye!\");
printf(\"\ \ \");
return 0;
case 3:
printf(\"\ \\t\\tPlease input a SKU number: \");
scanf(\"%d\", &tempSku);
for(int i=0;i<size;++i)
{
if(item[i].sku_==tempSku)
{
printf(\"\\t\\titem %d costs $%f\",tempSku,item[i].price_);
//f=1;
printf(\"\ \");
printf(\"\ \");
break;
}
}
if(i==size)
{
printf(\"\ \\t\\titem does not exist in the shop! please \ \"
\"\\t\\t try again\");
printf(\"\ \ \");
}
break;
default:
printf(\"\ \\t\\tInvalid Input, try again\ \ \");
}
}
return 0;
}


