Question

Locked

C Program Homework Help

By avireu ·
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 <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>

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 conversation is currently closed to new comments.

4 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Collapse -

Try

by Tony Hopkinson In reply to C Program Homework Help

scanf ("%d,%d", &product, &quantity);

More Info

http://irc.essex.ac.uk/www.iota-six.co.uk/c/c2_printf_and_scanf.asp

NB
just adding a tempiray printf statement straight after the scanf would have told you sumfing was up.

It's messy, but when what you think should be happening isn't, check what is...

Happy coding

Collapse -

Programming

by Drehsighden In reply to C Program Homework Help

Try taking the floated numbers that are defined out and make them case specific inside the while.


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(void)
{
int product; /* initializes product */
int quantity; /* initializes quantity */
float total; /* initializes number with decimals */
total = 0.00; /* sets the total to 0 */

printf( "Enter prodcut number 1-5 or -1 to end.\n");
scanf( "%d", &product); /* prompts user for product number and puts program into a while loop */
while (product != -1) {

switch (product){

case 1: printf( "Enter quantity of product 1.\n");
scanf( "%d", &quantity); /* each individual case takes the quantity and of certain product and finds the product of quantity and the amoiunt of the product */
total = (float) total + ( 2.98 * quantity );
printf( "Total retail value is $%.2f.\n", total); /* prints the total and prompts for a different product number and restarts the whole thing */
break;

case 2: printf( "Enter quantity of product 2.\n");
scanf( "%d", &quantity);
total = (float) total + ( 4.50 * quantity );
printf( "Total retail value is $%.2f.\n", total);
break;

case 3: printf( "Enter quantity of product 3.\n");
scanf( "%d", &quantity);
total =(float) total + ( 9.98 * quantity );
printf( "Total retail value is $%.2f.\n", total);
break;

case 4: printf( "Enter quantity of product 4.\n");
scanf( "%d", &quantity);
total =(float) total + ( 4.49 * quantity );
printf( "Total retail value is $%.2f.\n", total);
break;

case 5: printf( "Enter quantity of product 5.\n");
scanf( "%d", &quantity);
total =(float) total + ( 6.87 * quantity );
printf( "Total retail value is $%.2f.\n", total);
break;

default: system ("cls");
printf( "Sorry, invalid product number entered.\n");
}
printf( "Enter product number (1-5) or -1 to exit.\n");
scanf( "%d", &product);
}


if (product == -1) { /* ends the prompt for asking for products and ends with the toal sales of the week */

printf("The total sales for the past week are $%.2f\n", total);
getch ();
}

return 0;
} /* end program */

Collapse -

Reponse To Answer

by PurpleSkys In reply to Programming

It's nice that you want to help, but I hope they got it by now considering the post is two years old.

Back to Software Forum
4 total posts (Page 1 of 1)  

Related Discussions

Related Forums