Thread: Need help with the result

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    2

    Need help with the result

    i have a problem with my last case in the switch statement where you will see the result
    Need help with the result-1-jpg
    in my codes i can`t copy the first order number that i type like the picture above, it always shows 8
    Need help with the result-2-jpg
    Can anyone of you guys help me here is my codes
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
     int password, a, b;
      float p;
        do{  
          system("cls");
          printf("pin:");
          scanf("%d",&password);
      }
      while(password!=12345);
      printf("Successfully logged in!");
      system("cls");
      printf("===========================================\n");
      printf("             JOLLIBEE POS\n");
      printf("===========================================\n");
      printf("    [1] Burger Reg.              P35.00 \n");
      printf("    [2] Burger W/Cheese          P40.00 \n");
      printf("    [3] Spaghetti                P50.00 \n");
      printf("    [4] 1pc Chicken W/rice       P89.00 \n");
      printf("    [5] Soda <S>                 P20.00 \n");
      printf("    [6] Soda <L>                 P30.00 \n");
      printf("    [7] Sundae                   P28.00 \n");
      printf("\n");
      printf("    [8] Total\n");
      char loop='y';
      while(loop == 'y') {
      printf("===========================================\n");
      printf("Enter Order No:");
      scanf("%d",&a);
          switch (a) 
           {
        case 1: printf("Enter Quantity:");
        scanf("%d",&b);
        p=b * 35.00;
                break;
         
        case 2: printf("Enter Quantity:");
        scanf("%d",&b);
        p=b * 40.00;
                break;
         
     
        case 3: printf("Enter Quantity:");
        scanf("%d",&b);
        p=b * 50.00;
                break;
         
     
        case 4: printf("Enter Quantity:");
        scanf("%d",&b);
        p=b * 89.00;
                break;
         
     
        case 5: printf("Enter Quantity:");
        scanf("%d",&b);
        p=b * 20.00;
                break;
         
     
        case 6: printf("Enter Quantity:");
        scanf("%d",&b);
        p=b * 30.00;
                break;
         
     
        case 7: printf("Enter Quantity:");
        scanf("%d",&b);
        p=b * 28.00;
                break;
         
     
        case 8: loop='n';
        printf("===========================================\n");
        printf("\n");
        printf("ORDER NO.           QTY         SUBTOTAL\n");
        printf("   %d                 %d            %.2f\n", a, b, p);
        
        
        
                break;
                  
        }
        
        scanf("", &loop);
        
    
    
    }
    
    
        
         system("PAUSE");
         return 0;
    }

  2. #2
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Hello, welcome to the C boards! So here's the deal... You are overwriting p every single time the user makes a selection. What I mean is that your code should not be p = b*28... It should instead be p+=b*28 or p = p + b*28. That way, you keep a running total. Right now what's going on is that every time you make another order selection, you are resetting p to whatever the cost of that one order selection is rather than keeping a running total. I'm going to fiddle with this a little more and update my response if needed but that's your problem.

    Update all 7 of the cases to something like this:

    Code:
     case 7: printf("Enter Quantity:");
        scanf("%d",&b);
        p+=b * 28.00;
                break;
    Also, you are adding an int entered by the user into math involving a float : p=b * 50.00; //Might not work out how you expect because p is a float whereas b is an int. And what is this for:

    Code:
    scanf("", &loop);
    ??????

    Last but not least, and this is just me griping, but please don't use names like "p" and "b" because they are confusing and cryptic.


    Update: You mention the 8.... What's the problem with the 8? The reason the 8 is being displayed is because you prompt the user for the number 8 in order to total, so the order number (which is also being overwritten as 8 kind of like the above issue) is always 8 when the total is displayed. Watch carefully:

    When the user types 8, the following code gets executed as you coded:
    Code:
    case 8: loop='n';
        printf("===========================================\n");
        printf("\n");
        printf("ORDER NO.           QTY         SUBTOTAL\n");
        printf("   %d                 %d            %.2f\n", a, b, p);
    At this point, the variable 'a' contains the value 8. The reason why is because this switch-case statement requires that value in order for it to even run this code, thus it will always be 8 every time the code is executed. If you don't want that to be the case (no pun intended), you will need to use a different variable to display all of the order numbers or something. Again, please don't use such frivolous variable names.
    Last edited by Asymptotic; 12-10-2016 at 01:16 AM. Reason: Fixed formatting

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    2
    Hi first thank you for your response i really appreciate it. I already change the letters, i`m new in this language so i`m sorry about that. I solved this problem using a little arrays and strings i don`t have much knowledge in arrays because it is not covered yet in our school (maybe next week we will learn it) so i need to learn it to be able to finish this.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by clasterquinn View Post
    I solved this problem ...
    I assume that means you don't need any more help.

    Quote Originally Posted by clasterquinn View Post
    i don`t have much knowledge in arrays because it is not covered yet in our school
    You can't solve this problem without at least one array since your task (as I understand it) is to accept an arbitrary number of inputs and then, when requested, to display all of those inputs back to the user. So obviouly you need to store them in the interim.

    Since you're already forced to use an array to store the inputs, you may as well use an array to hold the prices, too. That way you can get rid of your very repetitive switch statement by simply indexing the array with the product code.

    I wouldn't bother keeping a running total while the user is still entering inputs. You can add up the total at the end when you're displaying the results.

  5. #5
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Quote Originally Posted by clasterquinn View Post
    Hi first thank you for your response i really appreciate it. I already change the letters, i`m new in this language so i`m sorry about that. I solved this problem using a little arrays and strings i don`t have much knowledge in arrays because it is not covered yet in our school (maybe next week we will learn it) so i need to learn it to be able to finish this.
    Awesome, glad you solved it. No need to apologize! Always happy to help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. End Result exe
    By c_lover in forum Game Programming
    Replies: 1
    Last Post: 07-30-2015, 08:50 PM
  2. I test for a result, but any result over 1 fails
    By jeremy duncan in forum C++ Programming
    Replies: 10
    Last Post: 05-23-2012, 01:23 PM
  3. No result out for this CP
    By leevenchoon in forum C Programming
    Replies: 12
    Last Post: 07-10-2011, 11:36 AM
  4. Help with result only...
    By Ervilha in forum C Programming
    Replies: 5
    Last Post: 08-19-2008, 11:29 AM
  5. What would be the result...
    By sreeramu in forum C Programming
    Replies: 8
    Last Post: 03-25-2008, 04:43 AM

Tags for this Thread