Thread: A little help on this (C programming)

  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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    I guess you could use an assignment.

    double tp1 = 0, tp2 = 0, tp3 = 0, tp4 = 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,875
    Just having a quick look, I can't see where you declare "choice".

    Just adding on to what Salem has said, at the end of your switch statement you have the following code...
    Code:
    totalbill = tp1 + tp2 + tp3 + tp4;
    If the user has endered choice 1 for the switch statement, tp1 gets a value but tp2..4 haven't got a value yet.
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    2
    any idea how i can accumulate the data (storing it) as i want it so if an user have the option to add on their items, the total price would change accordingly to what the user added on

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,875
    It looks like all you'd need to do is add a loop around your switch statement.

    The normal thing in this sort of set up is a do/while loop - i.e. Do while user input isn't a certain number. You tell the user what number that is in the menu.

    You are extremely close - Give it a go and see if you can get it
    Fact - Beethoven wrote his first symphony in C

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