THIS IS A C PROGRAMMING QUESTION: MY OUTPUT SHOULD BE LIKE THE IMAGE BELOW. I DI
ID: 3927887 • Letter: T
Question
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(" Welcome to the Shop =================== ");
while(1){
printf(" Please select from the following options: "
" 1) Display the inventory. "
" 2) Add to shop. "
" 3) Price Check. "
" 0)Exit. Select: ");
int c;
scanf("%d", &c);
switch(c){
case 1:
printf(" Inventory ========================================= ");
printf(" Sku Price Quantity ");
int i;
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:
return 0;
default:
printf(" Invalid Input, try again ");
}
}
return 0;
}
EXPECTED OUTPUT:
Explanation / Answer
#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(" Welcome to the Shop =================== ");
while(1)
{
printf(" Please select from the following options: "
" 1) Display the inventory. "
" 2) Add to shop. "
" 3) Price Check. "
" 0)Exit. Select: ");
int c;
scanf("%d", &c);
switch(c)
{
case 1:
printf(" Inventory ========================================= ");
printf(" Sku Price Quantity ");
int i;
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:
printf(" ");
printf(" Good bye!");
printf(" ");
return 0;
case 3:
printf(" Please input a SKU number: ");
scanf("%d", &tempSku);
for(int i=0;i<size;++i)
{
if(item[i].sku_==tempSku)
{
printf(" item %d costs $%f",tempSku,item[i].price_);
//f=1;
printf(" ");
printf(" ");
break;
}
}
if(i==size)
{
printf(" item does not exist in the shop! please "
" try again");
printf(" ");
}
break;
default:
printf(" Invalid Input, try again ");
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.