Help me in while loop with case menu

This is a discussion on Help me in while loop with case menu within the C Programming forums, part of the General Programming Boards category; Below is my program... the whole purpose of this prog. is to ask user to enter product no. and then ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    13

    Exclamation Help me in while loop with case menu

    Below is my program... the whole purpose of this prog. is to ask user to enter product no. and then enter number of quantity he wants. When user input those two, he may want to order another product_no without having to terminate the program, so i used while loop != 'q' || !='Q'. The 'q' || 'Q' is for user who wants to end the loop and see the total price. When i run the program, and press q to end loop, the program has gone kindof bezerk. Kindly, plz help me out here with my prog.

    #include<stdio.h>
    main()
    {
    int Quantity, Product_No;
    float Price, Total;
    printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@\n");
    printf(" CareKidz \n");
    printf(" Carrefour Subang Jaya \n");
    printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@\n");
    printf("Welcome To CareKidz System!!\n");
    printf("Make Your Selection from the menu below:\n\n");
    printf("Product No.\tProduct\t\t\t\t\tPrice\n\n");
    printf("\t1.\tPampers Baby Dry (Super Value Pack)\tRM 47.90\n");
    printf("\t2.\tSumo Confort Super Value\t\tRM 28.90\n");
    printf("\t3.\tPet-Pet Baby dry\t\t\tRM 17.90\n");
    printf("\t4.\tChicolastic Ultra trim Jumbo\t\tRM 19.90\n");
    printf("\t5.\tMamy Poko Disney\t\t\tRM 39.50\n");
    printf("\t6.\tDryperss Wee-Wee Mega Pack\t\tRM 34.90\n");
    printf("\t7.\tRoyale Kids Grace Kiki Lala Shoes\tRM 19.90\n");
    printf("\t8.\tOK Baby Wear\t\t\t\tRM 25.90\n");
    printf("\t9.\tWorld Of Cartoon Kids Wear\t\tRM 29.90\n");
    printf("\t10.\tChristmas Teddy Bear\t\t\tRM 19.90\n");
    printf("\n Type Q when you are finished with your selections.\n");
    printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@\n");
    while ( Product_No != 'q'){
    printf("Please enter a Product No. : ");
    scanf("%d", &Product_No);
    printf("Now enter the quantity you would like to purchase : ");
    scanf("%d", &Quantity);
    switch (Product_No) {
    case 1:
    Total = Quantity * 47.90;
    break;
    case 2:
    Total = Quantity * 28.90;
    break;
    case 3:
    Total = Quantity * 17.90;
    break;
    case 4:
    Total = Quantity * 19.90;
    break;
    case 5:
    Total = Quantity * 39.50;
    break;
    case 6:
    Total = Quantity * 34.90;
    break;
    case 7:
    Total = Quantity * 19.90;
    break;
    case 8:
    Total = Quantity * 25.90;
    break;
    case 9:
    Total = Quantity * 29.90;
    break;
    case 10:
    Total = Quantity * 19.90;
    break;
    default:
    printf("\nSorry, the Product No. you entered was not on the menu. Please try again!");
    }
    Price = Price + Total;
    }
    printf("\nTotal price for your order is %.2f", Price);
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    Here is the code fixed up some. This compiled and ran fine on my machine.

    You were passing 'Q' to a variable that expected an integer. You were also reassigning the value every time instead of keeping a running total( use += instead of just = ). If you have any other specific questions on why I changed things, just ask.

    BTW, if a user enters the same product code on two or more occasions, it will add up all instances.





    #include<stdio.h>
    int main()
    {
    int Quantity[10], Product_No;
    float Price, Total;

    printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@\n");
    printf(" CareKidz \n");
    printf(" Carrefour Subang Jaya \n");
    printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@\n");
    printf("Welcome To CareKidz System!!\n");
    printf("Make Your Selection from the menu below:\n\n");
    printf("Product No.\tProduct\t\t\t\t\tPrice\n\n");
    printf("\t1.\tPampers Baby Dry (Super Value Pack)\tRM 47.90\n");
    printf("\t2.\tSumo Confort Super Value\t\tRM 28.90\n");
    printf("\t3.\tPet-Pet Baby dry\t\t\tRM 17.90\n");
    printf("\t4.\tChicolastic Ultra trim Jumbo\t\tRM 19.90\n");
    printf("\t5.\tMamy Poko Disney\t\t\tRM 39.50\n");
    printf("\t6.\tDryperss Wee-Wee Mega Pack\t\tRM 34.90\n");
    printf("\t7.\tRoyale Kids Grace Kiki Lala Shoes\tRM 19.90\n");
    printf("\t8.\tOK Baby Wear\t\t\t\tRM 25.90\n");
    printf("\t9.\tWorld Of Cartoon Kids Wear\t\tRM 29.90\n");
    printf("\t10.\tChristmas Teddy Bear\t\t\tRM 19.90\n");
    printf("\n\t11.\tExit System\n");
    printf("\n Type 11 when you are finished with your selections.\n");
    printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@\n");

    while ( Product_No != 11)
    {
    printf("Please enter a Product No. : ");
    scanf("%d", &Product_No);

    if (Product_No != 11)
    {
    printf("Now enter the quantity you would like to purchase : ");
    scanf("%d", &Quantity[Product_No - 1]);
    }


    switch (Product_No)
    {
    case 1:
    Total += Quantity[0] * 47.90; /****notice the += instead of =****/
    break;
    case 2:
    Total += Quantity[1] * 28.90;
    break;
    case 3:
    Total += Quantity[2] * 17.90;
    break;
    case 4:
    Total += Quantity[3] * 19.90;
    break;
    case 5:
    Total += Quantity[4] * 39.50;
    break;
    case 6:
    Total += Quantity[5] * 34.90;
    break;
    case 7:
    Total += Quantity[6] * 19.90;
    break;
    case 8:
    Total += Quantity[7] * 25.90;
    break;
    case 9:
    Total += Quantity[8] * 29.90;
    break;
    case 10:
    Total += Quantity[9] * 19.90;
    break;
    case 11:
    Price = Price + Total;
    printf("\nTotal price for your order is %.2f", Price);
    return 0;
    default:
    printf("\nSorry, the Product No. you entered was not on the menu. Please try again!");
    }
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 09:55 AM
  2. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 03:22 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. strange
    By SAMSAM in forum Windows Programming
    Replies: 0
    Last Post: 01-31-2003, 06:48 PM

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