Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wrote the C language code for the question below but my solution has some issu

ID: 3581890 • Letter: I

Question

I wrote the C language code for the question below but my solution has some issues. My teacher said to fix these issues .Please help me to fix the errors. I attached the question.solution and error.

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!

        

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!


MY SOLUTION:


MY ERROR:


1. use define statement instead of const as it does not allocate any memory.

2. avoid using while(1) and break to control the loop. You need to construct an appropriate condition instead.

3. all variables should be declared at the beginning of the function otherwise they have limited scope
" 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_);
}

4. only one return statement should be used within any function.
item[size].price_ = tempPrice;
size++;
printf(" Item is successfully added to the inventory ");
break;
case 0:
return 0;
default:
printf(" Invalid Input, try again ");
printf("Good Bye");
}
}

return 0;
}

Explanation / Answer

So, here are the solutions I would like to suggest as per mentioned errors.

1. Replace the line

2. rewrite your code like this: ( in the place of while(1) )

do

{

....

...

}

while(c>=0 && c<4); // this is your loop termination condition. write this line after last line of loop in above // mentioned code.

3. As this is C language, all the variables need to be decalred in the starting of the function. So, remove the two declarations of "int c" and "int i" and write it in the first line of main function as:

4. Rewrite case 0 as follows:

case 0:

exit(0);

Now, include <stdlib.h> header file that contains exit() prototype.

These changes are exact solutions to your problem. Comment if you do not get something. I will help for sure.