Alrite, now how to end with 'q'

This is a discussion on Alrite, now how to end with 'q' within the C Programming forums, part of the General Programming Boards category; I understand that ending the loop for this menu, you have to type in 11. It works well w/ numbers ...

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

    Question Alrite, now how to end with 'q'

    I understand that ending the loop for this menu, you have to type in 11. It works well w/ numbers to end loop. Though, what i am really after now, is i want to learn how to end the menu loop using 'q'. I have tried many ways, and so far I'm still unable to get it. I understand that, if you if you program like this ((Product_No=getchar())!= 81), that 81 is converted to q, in ascii form. But still i dont understand because even though i typed it out there, does not work. Anyone, plz help me here. Help me by fixing my program lines below to end menu loop with 'q' and also explain it so that i understand it. Thx.

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

    while ((Product_No =getchar())!= 'q')
    {
    printf("\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@\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("\t1. Pampers Baby Dry (Super Value Pack)\tRM 47.90\n");
    printf("\t2. Sumo Confort Super Value\t\tRM 28.90\n");
    printf("\t3. Pet-Pet Baby dry\t\t\tRM 17.90\n");
    printf("\t4. Chicolastic Ultra trim Jumbo\t\tRM 19.90\n");
    printf("\t5. Mamy Poko Disney\t\t\tRM 39.50\n");
    printf("\t6. Drypers Wee-Wee Mega Pack\t\tRM 34.90\n");
    printf("\t7. Royale Kids Grace Kiki Lala Shoes\tRM 19.90\n");
    printf("\t8. OK Baby Wear\t\t\t\tRM 25.90\n");
    printf("\t9. World Of Cartoon Kids Wear\t\tRM 29.90\n");
    printf("\t10. Christmas Teddy Bear\t\tRM 19.90\n");
    printf("\n\t11.\tExit System\n");
    printf("\nType Q when you are finished with your selections.\n");
    printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@\n");
    printf("Select 1,2,3,4,5,6,7,8,9 or 10--> ");
    scanf("%d", &Product_No);

    if ((Product_No=getchar()) != 'q')
    {
    printf("How many orders of item number %d would you like? ", Product_No);
    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 += Total;
    printf("\n Please pay RM %.2f\n", Tax_Price = Price * 1.05);
    printf("Thank you. Please come again!!");
    return 0;
    default:
    printf("\nSorry, the Product No. you entered was not on the menu. Please try again!");
    }
    }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    Code:
    if ( scanf("%d",&Product_No) == 1 ) {
       // do the number stuff
    } else {
       // it's not a number, must be a char
       if ( getchar() == 'q' ) {
          // it's q, time to quit
       } else {
          // some other non-numeric char
       }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Modify to make Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 10
    Last Post: 11-03-2008, 06:25 PM
  3. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 06:47 PM
  4. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  5. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM

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