In C, Compute Total Retail Price Of Products Sold
Download
#include<stdio.h>
void main(){
const float prices[]={2.95f, 4.99f, 5.49f, 7.80f, 8.85f};
const int len = sizeof(prices)/sizeof(float);
float total = 0.0f;
int no, i, qty;
do{
printf("Product No. Price\n");
printf("------------------\n");
for(i = 0; len > i; i ++)
printf("%d)%10s$%.2f\n", (1+i), "", prices[i]);
fflush(stdin);
printf("\nEnter product no# (0 to quit): ");
if(!scanf("%d", &no)){
printf("Error: Invalid input!\n");
continue;
}
if(0 == no) break;
if(1 > no || len <= no){
printf("Error: Invalid product no#!\n");
continue;
}
fflush(stdin);
printf("\nEnter quantity: ");
if(!scanf("%d", &qty)){
printf("Error: Invalid input!\n");
continue;
}
if(1 > qty){
printf("Error: Invalid quantity!\n");
continue;
}
total += (prices[no-1] * qty);
}while(1);
if(total){
printf("Total retail: $%.2f\n", total);
} else {
printf("Warn: No retail data found!");
}
}
Comments
Post a Comment