I HAVE A C PROGRAMMING QUESTION. I AM WRITING THE QUESTION AND THE SOLUTION I WR
ID: 3937162 • Letter: I
Question
I HAVE A C PROGRAMMING QUESTION. I AM WRITING THE QUESTION AND THE SOLUTION I WROTE BELOW (MY SOLUTION HAS SOME ERROR & NOT RUNNING ON MATRIX ,PLEASE HELP ME TO FIX IT)
MY SOLUTION:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct inventory
{
int sku;
int quantity;
double price;
}st[100];
int main()
{
int ch;
int i=0;
printf(" Welcome to the Shop
==================
Please select from the following options:
1) Display the inventory.
2) Add to the inventory.
3) Check price.
4) Clear Screen.
0) Exit.
Select: ");
scanf("%d",&ch);
while(ch!=0)
{
switch(ch)
{
case 1:
printf(" sku price quantity");
for(int j=0;j<=i;j++)
{
printf(" %d %lf %d",st[i].sku,st[i].price,st[i].quantity);
}
break;
case 2:
printf(" enter sku :");
int sku;
scanf("%d",&sku);
int q;
printf(" enter quantity :");
scanf("%d",&q);
double p;
printf(" enter price :");
scanf("%lf",&p);
boolean flag=false;
int in;
in=++i;
for(int j=0;j<=i;j++)
{
if(st[j]==sku)
{
flag=true;
in=j;
break;
}
}
st[in].price=p;
st[in].quantity=q
st[in].sku=sku;
if(flag==true)
{
printf(" item already exists ,quantityupdated");
}
else
printf(" item added");
break;
case 3:
printf(" enter sku :");
int sku;
scanf("%d",&sku);
boolean flag=false;
double p;
for(int j=0;j<=i;j++)
{
if(st[j].sku==sku)
{
flag=true;
p=st[j].price;
break;
}
}
if(flag==false)
printf(" item not found");
else
printf(" price is%lf",p);
break;
case 4:
clrscr();
break;
case 0:
printf(" good bye");
exit(0);
break;
default:
printf(" invalid option,try again!");
break;
}
printf(" Welcome to the Shop
==================
Please select from the following options:
1) Display the inventory.
2) Add to the inventory.
3) Check price.
4) Clear Screen.
0) Exit.
Select: ");
scanf("%d",&ch);
}
return 0;
EXPECTED OUTPUT:
Program completion
Your program is complete if your output matches the following output. bold numbers show the user’s input.
Start OUPUT
><
><
><
><
><
><
><
><
><
><
><
><
><
><
><
><
><
><
><
><
>Welcome to the Shop
>===================
>Please select from the following options:
>1) Display the inventory.
>2) Add to the inventory.
>3) Check price.
>4) Clear Screen.
>0) Exit.
>Select: 5
>Invalid input, try again: 2
>Please input a SKU number: 1234
>Quantity: 2
>Price: 45.63
>The item is successfully added to the inventory.
>Please select from the following options:
>1) Display the inventory.
>2) Add to the inventory.
>3) Check price.
>4) Clear Screen.
>0) Exit.
>Select: 2
>Please input a SKU number: 9010
>Quantity: 5
>Price: 23.50
>The item is successfully added to the inventory.
>Please select from the following options:
>1) Display the inventory.
>2) Add to the inventory.
>3) Check price.
>4) Clear Screen.
>0) Exit.
>Select: 2
>Please input a SKU number: 1234
>Quantity: 5
>The item exists in the repository, quantity is updated.
>Please select from the following options:
>1) Display the inventory.
>2) Add to the inventory.
>3) Check price.
>4) Clear Screen.
>0) Exit.
>Select: 1
><
><
>Inventory
>=========================================
>Sku Price Quantity
>1234 45.63 7
>9010 23.50 5
>=========================================
>Please select from the following options:
>1) Display the inventory.
>2) Add to the inventory.
>3) Check price.
>4) Clear Screen.
>0) Exit.
>Select: 0
>Goodbye!
><
Explanation / Answer
// C code inventory
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct inventory
{
int sku;
int quantity;
double price;
}st[100];
int main()
{
int ch;
int i=0;
printf(" Welcome to the Shop ================== Please select from the following options: 1) Display the inventory. 2) Add to the inventory. 3) Check price. 4) Clear Screen. 0) Exit. Select: ");
scanf("%d",&ch);
int sk;
while(ch!=0)
{
switch(ch)
{
case 1:
printf(" sku price quantity");
for(int j=0;j<i;j++)
{
printf(" %d %lf %d",st[j].sku,st[j].price,st[j].quantity);
}
break;
case 2:
printf(" Enter sku : ");
scanf("%d",&sk);
int q;
printf("enter quantity : ");
scanf("%d",&q);
double p;
bool flag=false;
int in;
for(int j=0;j<i;j++)
{
if(st[j].sku==sk)
{
flag=true;
in=j;
break;
}
}
if(flag == true)
{
st[in].quantity = st[in].quantity + q;
printf(" item already exists ,quantityupdated");
}
else
{
printf("enter price : ");
scanf("%lf",&p);
st[i].price=p;
st[i].quantity=q;
st[i].sku=sk;
i++;
printf(" item added");
}
break;
case 3:
printf(" Enter sku :");
int sk;
scanf("%d",&sk);
bool flg=false;
for(int j=0;j<=i;j++)
{
if(st[j].sku==sk)
{
flg=true;
p=st[j].price;
break;
}
}
if(flg==false)
printf(" item not found");
else
printf(" price is %lf",p);
break;
case 4:
system("clear");
break;
case 0:
printf(" good bye");
exit(0);
break;
default:
printf(" invalid option,try again!");
break;
}
printf(" Welcome to the Shop ================== Please select from the following options: 1) Display the inventory. 2) Add to the inventory. 3) Check price. 4) Clear Screen. 0) Exit. Select: ");
scanf("%d",&ch);
}
return 0;
}
/*
output:
Welcome to the Shop
==================
Please select from the following options:
1) Display the inventory.
2) Add to the inventory.
3) Check price.
4) Clear Screen.
0) Exit.
Select: 2
Enter sku : 1234
enter quantity : 2
enter price : 23
item added
Welcome to the Shop
==================
Please select from the following options:
1) Display the inventory.
2) Add to the inventory.
3) Check price.
4) Clear Screen.
0) Exit.
Select: 2
Enter sku : 12331
enter quantity : 3
enter price : 56
item added
Welcome to the Shop
==================
Please select from the following options:
1) Display the inventory.
2) Add to the inventory.
3) Check price.
4) Clear Screen.
0) Exit.
Select: 2
Enter sku : 1234
enter quantity : 4
item already exists ,quantityupdated
Welcome to the Shop
==================
Please select from the following options:
1) Display the inventory.
2) Add to the inventory.
3) Check price.
4) Clear Screen.
0) Exit.
Select: 1
sku price quantity
1234 23.000000 6
12331 56.000000 3
Welcome to the Shop
==================
Please select from the following options:
1) Display the inventory.
2) Add to the inventory.
3) Check price.
4) Clear Screen.
0) Exit.
Select: 3
Enter sku :12
item not found
Welcome to the Shop
==================
Please select from the following options:
1) Display the inventory.
2) Add to the inventory.
3) Check price.
4) Clear Screen.
0) Exit.
Select: 3
Enter sku :1234
price is 23.000000
Welcome to the Shop
==================
Please select from the following options:
1) Display the inventory.
2) Add to the inventory.
3) Check price.
4) Clear Screen.
0) Exit.
Select: 0
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.