# Homework Help C Program Loop & Switch

Printable View

• 03-11-2011
123456tacos
Homework Help C Program Loop & Switch
Hi i need help, like i dont understand this at all :/
this is the question:

A mail-order house sells five different products whose retail prices are as follows: product 1, \$2.98; product 2, \$4.50; product 3, \$9.98; product 4, \$4.49; and product 5, \$6.87. Write an application that reads a series of pairs of numbers as follows:

a) Product number
b) Quantity sold for one day

Your program must use a switch structure to help determine the retail price for each product. It should calculate and display the total retail value of each product sold last week. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.
• 03-11-2011
Mr.777
Where is your code?
• 03-11-2011
krishnapollu
Post ur code. Can't help without knowing how far u've reached.
• 03-11-2011
123456tacos
Opps sorry i thought i did :P
The problem is that when i enter 1 is that it doesnt work and it doesnt ask for multiple inputs

Code:

``` #include <stdio.h> int main (void) {   int custnum;   int  kwh, copy;   double totald=0.0;   double totala=0.0;   double totalb=0.0;   double totalc=0.0;   double totale=0.0;     while(1) {     printf("\nEnter Product Number or enter 0 to quit>");     scanf("%d", &custnum);     if (custnum == 0)       break;     printf("Enter Quality Sold >", custnum);     scanf("%d", &kwh);     copy = kwh;     while(custnum == 1) {        totala += (kwh * 2.98);            }     if(custnum == 2) {        totalb = (kwh * 4.50);           }     if(custnum == 3) {        totalc = (kwh * 9.98);       }     if(custnum == 4) {        totald = (kwh * 4.49);           }     if(custnum == 5) {        totale = (kwh * 6.87);           }         kwh = copy;     printf("\nProduct 1 : \$%.2f\n",     totala);     printf("\nProduct 2 : \$%.2f\n",     totalb);     printf("\nProduct 3 : \$%.2f\n",     totalc);     printf("\nProduct 4 : \$%.2f\n",     totald);     printf("\nProduct 5 : \$%.2f\n",     totale);                         totala = totalb = totalc = totald = totale = 0;   }   (void) getchar();   system ("pause");    return 0;   }```
• 03-11-2011
quzah
Code:

```    while(custnum == 1) {        totala += (kwh * 2.98);            }```
If 'custnum' is ever 1, this loop will run forever.

Quzah.
• 03-11-2011
123456tacos
ahahaha thanks it works now but if i enter multiple entries like "1" twice for the product number it stops automaticaly -.-'

Code:

``` #include <stdio.h> int main (void) {   int custnum;   int  kwh, copy;   double totald=0.0;   double totala=0.0;   double totalb=0.0;   double totalc=0.0;   double totale=0.0;       while(1) {     printf("\nEnter Product Number or enter 0 to quit>");     scanf("%d", &custnum);     if (custnum == 0)       break;     printf("Enter Quality Sold >", custnum);     scanf("%d", &kwh);     copy = kwh;         if(custnum == 1) {        totala += (kwh * 2.98);         printf("\nEnter Product Number or enter 0 to quit>");     scanf("%d", &custnum);     if (custnum == 0)       break;     printf("Enter Quality Sold >", custnum);     scanf("%d", &kwh);     copy = kwh;           }     if(custnum == 2) {        totalb = (kwh * 4.50);           printf("\nEnter Product Number or enter 0 to quit>");     scanf("%d", &custnum);     if (custnum == 0)       break;     printf("Enter Quality Sold >", custnum);     scanf("%d", &kwh);     copy = kwh;           }     if(custnum == 3) {        totalc = (kwh * 9.98);           printf("\nEnter Product Number or enter 0 to quit>");     scanf("%d", &custnum);     if (custnum == 0)       break;     printf("Enter Quality Sold >", custnum);     scanf("%d", &kwh);     copy = kwh;       }     if(custnum == 4) {        totald = (kwh * 4.49);           printf("\nEnter Product Number or enter 0 to quit>");     scanf("%d", &custnum);     if (custnum == 0)       break;     printf("Enter Quality Sold >", custnum);     scanf("%d", &kwh);     copy = kwh;           }     if(custnum == 5) {        totale = (kwh * 6.87);           printf("\nEnter Product Number or enter 0 to quit>");     scanf("%d", &custnum);     if (custnum == 0)       break;     printf("Enter Quality Sold >", custnum);     scanf("%d", &kwh);     copy = kwh;           }         kwh = copy;     printf("\nProduct 1 : \$%.2f\n",     totala);     printf("\nProduct 2 : \$%.2f\n",     totalb);     printf("\nProduct 3 : \$%.2f\n",     totalc);     printf("\nProduct 4 : \$%.2f\n",     totald);     printf("\nProduct 5 : \$%.2f\n",     totale);                         totala = totalb = totalc = totald = totale = 0;   }   (void) getchar();   system ("pause");    return 0;   }```
• 03-11-2011
quzah
You seem to be repeating a lot of the same lines over and over. Most of the time you are doing that, you can instead use a loop. Consider:
Code:

```for( x = 0; x < 5; x++ ) {     if( scanf( "%d", &num ) == 1 )     {         if( num != 0 )         {             do stuff with the number         }         else break;     }     else break; }```
This might also be a good time to learn about arrays.

Quzah.
• 03-11-2011
123456tacos
yaaaaa we havent learned that yet :'/