Thread: A little help on this (C programming)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    2

    A little help on this (C programming)

    I'm getting this error when calculating the total bill.
    A little help on this (C programming)-error-png

    I kinda know what is the issue, it means if an user does not select one of the options, the value of that said "tp" would be undefined because the user did not select any quantity.

    Problem is, i do not know how to fix this. How do i make it so that "tp1,2,3,4" will be defined no matter if the user chooses it or not?



    Code:
        
    #include <stdio.h>
    
    
    
    int main(void) {                        
                    int quantity1, quantity2, quantity3, quantity4;
                    double tp1, tp2, tp3, tp4;
    
    
                    printf("\n\n    Product Code        Price(USD)  \n");
                    printf("        [1]              20.20     \n");
                    printf("        [2]              14.50     \n");
                    printf("        [3]              5.45      \n");
                    printf("        [4]              2.80      \n");
                    printf("Enter the product code you wish to purchase : ");
                    scanf("%d", &choice);
                    switch (choice)
                    {
                    case 1:
    
    
                    {
                        printf("You have selected product code 1.");
                        printf("\nPlease Enter your Quantity :");
                        scanf("%d", &quantity1);
    
    
                        tp1 = quantity1 * 20.20;
    
    
                    }
                    break;
    
    
                    case 2:
                    {
                        printf("You have selected product code 2.");
                        printf("\nPlease Enter your Quantity :");
                        scanf("%d", &quantity2);
    
    
                        tp2 = quantity2 * 14.50;
    
    
                    }
                    break;
    
    
                    case 3:
                    {
                        printf("You have selected product code 3.");
                        printf("\nPlease Enter your Quantity :");
                        scanf("%d", &quantity3);
    
    
                        tp3 = quantity3 * 5.45;
                    }
                    break;
    
    
                    case 4:
                    {
                        printf("You have selected product code 4.");
                        printf("\nPlease Enter your Quantity :");
                        scanf("%d", &quantity4);
    
    
                        tp4 = quantity4 * 2.80;
    
    
                    }
                    break;
    
    
                    default:
                    {
                        printf("\nInvalid Choice");
                        break;
                    }
    
    
                    }
    
    
                    double totalbill;
                    totalbill = tp1 + tp2 + tp3 + tp4;
                    printf("totalbill = %.2f", totalbill);
    
        return 0;
    }
    Last edited by death1213; 02-26-2019 at 09:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 02-01-2019, 12:27 PM
  2. Replies: 0
    Last Post: 02-01-2019, 12:22 PM
  3. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread