You are the owner of a new store and need to keep track of your inventory. Since
ID: 3648193 • Letter: Y
Question
You are the owner of a new store and need to keep track of your inventory. Since you are a seasoned C programmer, you are going to write a C program to perform this function. First, define a structure that will hold a description of the item (C string), a quantity (integer), and price per item (float), and a total cost. Your program is to ask the user for the description, quantity, and price per item and calculate the total cost. You will ask the user after each record if another record is to be entered and quit when the answer isExplanation / Answer
please rate - thanks
you need a while not an if, a } was missplaced, and you must clear the input buffer
#include <stdio.h>
//#include <conio.h> for getch at end
struct Description
{
char item_name[50];
int item_qty;
float item_price;
float total_cost;
}Descrpt;
int main( )
{
int y;
int n;
char response = 'y';
float total_cost_for_all_items;
FILE *fptr;
fptr = fopen("inventory.txt","w");
while (response == 'y')
{
printf("Enter the Item name:");
scanf("%s",Descrpt.item_name);
fprintf(fptr, "Item name: %s ", Descrpt.item_name);
printf("Enter the Quantity:");
scanf("%d",&Descrpt.item_qty);
fprintf(fptr, "Quantity: %d ", Descrpt.item_qty);
printf("Enter the Price per Item:");
scanf("%f",&Descrpt.item_price);
fprintf(fptr, "Price per Item: %f ", Descrpt.item_price);
printf("Total Cost:");
Descrpt.total_cost = (Descrpt.item_qty * Descrpt.item_price);
printf("%f ", Descrpt.total_cost);
fprintf(fptr, "Total Cost: %f ", Descrpt.total_cost);
total_cost_for_all_items += Descrpt.total_cost;
while (getchar() != ' ' );
printf("Add another item to the inventory? (y/n): ");
scanf("%c", &response);
}
printf("Total cost of all the items: %f ", total_cost_for_all_items);
//getch(); I needed to keep window open, you didn't have it
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.