Homework Help C Program Loop & Switch

This is a discussion on Homework Help C Program Loop & Switch within the C Programming forums, part of the General Programming Boards category; Hi i need help, like i dont understand this at all :/ this is the question: A mail-order house sells ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    39

    Angry 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.
    Attached Images Attached Images  

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Where is your code?

  3. #3
    Registered User krishnapollu's Avatar
    Join Date
    Dec 2010
    Location
    KERALA
    Posts
    29
    Post ur code. Can't help without knowing how far u've reached.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    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;
      
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
        while(custnum == 1) {  
          totala += (kwh * 2.98);  
          
        }
    If 'custnum' is ever 1, this loop will run forever.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    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;
      
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    yaaaaa we havent learned that yet :'/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop Entire Program?
    By seanminator in forum C Programming
    Replies: 25
    Last Post: 07-22-2009, 01:23 PM
  2. Program hangs when encounters switch
    By maverix in forum C Programming
    Replies: 6
    Last Post: 12-21-2007, 03:17 AM
  3. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. switch statement that uses loop
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 04:47 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21