I've been trying to modify the code to be able to add a description to each item by adding a new string array called "description" and modifying the code a bit. However, its not as easy as I originally thought. Go figures... hehe
Note: the new code is in red
The first red line is the declaration of the string array for description.
The third red line asks the user to enter the description for items 1-8
This follows with the "gets" statement to get the value of the description. I used gets instead of "scanf" because the description might have spaces in there and "scanf" doesnt do spaces.
On the same line I also have [choice-1] - the reason for this is because I want to match the name and its description...
However, the problem is, that the gets() statement doesnt do what it supposed to do... The program skips it. I mean it does display "Description", but doesnt give me the chance to enter the description and goes straight to "Price"...
What am i doing wrong?
Code:
#include <stdio.h>
#define MENU 11
#define ITEMS 8
int main ( ) {
char item[MENU][20] = {" Bread", " Butt.", " Conf.",
" Fruit", " Meat", " Milk", " Veg.",
" Other", " Sub-total", "Total", "Quit"};
char description[ITEMS][20];
int i;
int choice;
float cost;
float sum, tendered, change;
float subtotals[ITEMS] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
while (choice != 11)
{
printf("\n Enter your choice:\n");
for (i = 0; i < MENU; i++)
{
printf("\t%d. %s\n", (i + 1), item[i]);
}
printf("\t\t--> ");
scanf("%d", &choice);
getchar();
if(choice > 0 && choice < 9)
{
printf(" Description:\t\t");
gets(description[choice-1]);
printf(" Price:\t\t");
scanf("%f", &cost);
subtotals[choice-1] += cost;
}
else if(choice == 9)
{
sum = 0;
for(i = 0; i < ITEMS; i++)
{
if(subtotals[i] > 0)
{
printf("\n%s: %s\t\t\t%9.2f", item[i], description[i], subtotals[i]);
sum += subtotals[i];
}
}
printf("\n----------------------------------\n");
printf(" Total:\t\t\t%9.2f\n", sum);
printf("----------------------------------\n");
}
else if(choice == 10)
{
sum = 0;
for(i = 0; i < ITEMS; i++)
{
if(subtotals[i] > 0 )
{
printf("\n%s: %s\t\t\t%9.2f", item[i], description[i], subtotals[i]);
sum += subtotals[i];
}
}
printf("\n----------------------------------\n");
printf(" Total:\t\t\t%9.2f\n", sum);
printf("----------------------------------\n");
printf(" Tendered:\t\t ");
scanf("%f", &tendered);
change = tendered - sum;
printf(" Change:\t\t%9.2f\n", change);
}
}
}