Here is the basic idea. We collect the product number and the quantity and use them in an if statement to determine the unit price and calculate the total value for that item. We then add the total to a running accumulator value (called total) until they either enter an invalid product number or enter zero.
Once they do that we spit out the total value.
To make this even better and something you can do to improve it, check to make sure that they can't enter a negative quantity and if they do, have it ignore the item they entered. Unless the teacher wants you to accept negative quantities as a sign of the product being returned.
CODE
#include <stdio.h>
#include <math.h>
int main()
{
int quantity = 0;
int result = 0;
int product = 0;
double unitPrice = 0.0;
double total = 0.0;
// Collect the product number
printf("Enter product number (1-5) or 0 to quit: ");
scanf("%d",&product);
// While the product number is between 1 and 5 execute
// our prompt for quantity and keep track of total.
while ((product > 0) && (product <= 5)) {
printf("Enter quantity: ");
scanf("%d",&quantity);
// Based on product number, set unitPrice
switch(product) {
case 1:
unitPrice = 2.98;
break;
case 2:
unitPrice = 4.50;
break;
case 3:
unitPrice = 9.95;
break;
case 4:
unitPrice = 3.89;
break;
case 5:
unitPrice = 9.98;
break;
default:
break;
}
// Keep track of total
total += (unitPrice * quantity);
// Prompt for next item and repeat process
printf( "Enter product number (1-5) or 0 to quit: ");
scanf("%d",&product);
}
// When they enter invalid product number or 0, spit out result.
printf("\nTotal is: $%.2f\n\n",total);
}
Read the in code comments to see how this goes and looks. Enjoy!
"At DIC we be price and quantity calculating code ninjas!"