C Program Homework Help - TechRepublic
Question
February 23, 2010 at 08:22 PM
avireu

C Program Homework Help

by avireu . Updated 14 years, 3 months ago

I’ve been working on this question for the past few hours and can’t figure it out for the life of me. PLEASE HELP!

The Question reads as:
A mail order house sells 5 different products whose retail prices are shown in the following table.
1 $2.98
2 $4.50
3 $9.98
4 $4.49
5 $6.87
write a program that reads a series of pairs of numbers as follows:
a) product number
b) quantity sold
your program should use a switch statement to help determine the retail price for each product. Your program should calculate and display the total retail value of all products sold.

MY CODE:
#include
#include
#include
#include

using namespace std;

int main(void)
{
int product;
int quantity;
float s1, s2, s3, s4, s5;
float p1 = 2.98;
float p2 = 4.50;
float p3 = 9.98;
float p4 = 4.49;
float p5 = 6.87;
float total;

printf (“Enter Product Number and Quantity Sold\n(Enter EOF to end)\n”);
scanf (“%d%d”, &product, &quantity);

while (( product = getchar ()) != EOF ){
switch (product){
case 1:
s1 = (float) quantity*p1;
break;

case 2:
s2 = (float) quantity*p2;
break;

case 3:
s3 = (float) quantity*p3;
break;

case 4:
s4 = (float) quantity*p4;
break;

case 5:
s5 = (float) quantity*p5;
break;

default:
printf (“Invalid Input\n”);
break;
/*With this in my program I get Invalid Input running accross the screen*/

}
}
total = (float) s1 + s2 + s3 + s4 + s5;
printf (“Total Sales: %.2f\n”, total);

system(“PAUSE”);
return EXIT_SUCCESS;
}

This discussion is locked

All Comments