Thread: Loop and Switch Problem Help

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

    Question Loop and Switch Problem Help

    There is my code. I'm running into problems in which the program will run, but the results are... bad. Not even close. All products except 5 show up as 0, and 5 ends up showing up as 70.xxxxxxxxxxx or something similar.

    I'm very new to C programming so bear with me as I'm aware I may not even be close to what I actually need to do. Does anyone here happen to have any ideas to get this working?

    Problem:Loop and Switch

    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.



    Code:
    #include <stdio.h>
    #define SENTINEL 0
    
    int
    
    main(void)
    
    {
    
        int prodnum = 0;
    
        int quant = 0;
    
        double prodtot1 = 0, prodtot2 = 0, prodtot3 = 0, prodtot4 = 0, prodtot5 = 0;
    
        
    
        printf("Enter product number (1-5) (%d to stop): ", SENTINEL);
    
        scanf("%d", &prodnum);
    
        if (prodnum != SENTINEL) {
    
        printf("Enter quantity sold: ");
    
        scanf ("%d", &quant); }
    
        while (prodnum != SENTINEL) {
    
            printf("Enter product number (1-5) (%d to stop): ", SENTINEL);
    
            scanf("%d", &prodnum);
    
            printf("Enter quantity sold: ");
    
            scanf ("%d", &quant);
    
        }
    
        if ( prodnum==1 || prodnum==2 || prodnum==3 || prodnum==4 || prodnum==5 )
    
        {
    
        switch (prodnum) {
    
            case 1: prodtot1 = 2.98 * quant;
    
            break;
    
            case 2: prodtot2 = (4.50 * quant);
    
            break;
    
            case 3: prodtot3 = (9.98 * quant);
    
            break;
    
     
    
            case 4: prodtot4 = (4.49 * quant);
    
            break;
    
            case 5: prodtot5 = (6.87 * quant);
    
            break;
    
     
    
            default: printf("Invalid number selected, please enter a number 1-5.\n");
    
        }
    
        }
    
        printf("Product 1: %d\n", prodtot1);
    
        printf("Product 2: %lf\n", prodtot2);
    
        printf("Product 3: %lf\n", prodtot3);
    
        printf("Product 4: %lf\n", prodtot4);
    
        printf("Product 5: %lf\n", prodtot5);
        
        system ("pause"); 
        return(0);
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You are continually overwriting prodnum and quant in your while loop, as well as, the previous amount that you entered in the if clause.

    prodnum can only have 1 value, it's a single int, and your switch/case will only execute 1 case so all other cases will not be executed and the variables in them will keep their initial value of 0.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Develop algorithm first before coding...
    1) Read prod no:
    2) if prod no = END . exit
    3) Read Qty sold
    4) add to that prod no:

    Code:
    prod1 = prod2 = prod3 = ... = 0
    do 
     prod_no = get_prod_no()
     if prod_no = SENTINEL 
       END
     qty_sold = get_qty_sold()
     switch  prod_no 
       case 1: prod1 += qty_sold * 2.98
       case 2:  ....
    IMO, If you have learnt array, the code needs not be that long....
    Last edited by Bayint Naung; 03-11-2011 at 09:44 PM.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    39
    kk so this is what i got it works and all but when i enter multiple product #1 it just takes the last one i enter and it doesnt add them
    Code:
    #include <stdio.h> 
    
    int main() 
    {
      int swt=0, num1=0, num2=0, num3=0, num4=0, num5=0;
      float awn1=0, awn2=0, awn3=0, awn4=0, awn5=0;
     
      for(;;)  
      { 
        printf ("\nEnter the product number <1-5> <0 to end>: "); 
        scanf ("%d", &swt );
        if(swt==0)
          break;
      
        switch ( swt )
        { 
          case 1: 
            printf ("Enter Quantity Sold: "); 
            scanf ("%d",&num1 ); 
            awn1 = num1 * 2.98;
            break; 
    
          case 2: 
            printf ("Enter Quantity Sold: "); 
            scanf ("%d", &num2 ); 
            awn2 = num2 * 4.50; 
            break; 
    
          case 3: 
            printf ("Enter Quantity Sold: "); 
            scanf ("%d", &num3 ); 
            awn3 = num3 * 9.98; 
            break; 
    
          case 4: 
            printf ("Enter Quantity Sold: "); 
            scanf ("%d", &num4 ); 
            awn4 = num4 * 4.49; 
            break; 
          
          case 5: 
            printf ("Enter Quantity Sold: "); 
            scanf ("%d", &num5 ); 
            awn5 = num5 * 6.87; 
            break; 
    
          default: 
            printf ("Incorrect number\n"); 
            printf ("Enter your product number:"); 
        } 
      } 
    
       
        printf("\nProduct 1 : $%.2f\n",
        awn1);
        printf("\nProduct 2 : $%.2f\n",
        awn2);
        printf("\nProduct 3 : $%.2f\n",
        awn3);
        printf("\nProduct 4 : $%.2f\n",
        awn4);
        printf("\nProduct 5 : $%.2f\n",
        awn5);
      getch(); 
      system ("pause");  
      return 0; 
    }
    nvm i was just missing += lol :P
    Last edited by 123456tacos; 03-11-2011 at 11:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C tutorial Switch..case loop
    By dunsta in forum C Programming
    Replies: 8
    Last Post: 04-18-2010, 04:55 AM
  2. Switch problem
    By Know_Your_Role in forum C++ Programming
    Replies: 1
    Last Post: 09-22-2009, 02:47 AM
  3. Problem with switch loop
    By sweetorangepie in forum C Programming
    Replies: 3
    Last Post: 03-20-2008, 10:26 AM
  4. switch statement that uses loop
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 05:47 PM
  5. can (switch) be apart of a loop?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 06:29 PM

Tags for this Thread