ok then, just a tip for my 'value' problem

This is a discussion on ok then, just a tip for my 'value' problem within the C Programming forums, part of the General Programming Boards category; I know i have sent this message, but no-one seem to reply cuz of what i said for help. Oh ...

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

    Unhappy ok then, just a tip for my 'value' problem

    I know i have sent this message, but no-one seem to reply cuz of what i said for help. Oh well, i actually meant to say is what is the problem with my value in my program. Just point out to me which line in my program below is causing the problem and give me tips what should be done. I still want it to end loop with 'q' though, not with a number. The problem i have at the moment with the value is that it multiplies 2 the value and i dont want it to multiply 2.


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

    scanf("%d", &Product_No);

    while ( 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("\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);
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity[Product_No - 1]);

    if ( Product_No == 1)
    {
    Total += Quantity[0] * 47.90;
    }
    else if ( Product_No == 2)
    {
    Total += Quantity[1] * 28.90;
    }
    else if ( Product_No == 3)
    {
    Total += Quantity[2] * 17.90;
    }
    else if ( Product_No == 4)
    {
    Total += Quantity[3] * 19.90;
    }
    else if ( Product_No == 5)
    {
    Total += Quantity[4] * 39.50;
    }
    else if ( Product_No == 6)
    {
    Total += Quantity[5] * 34.90;
    }
    else if ( Product_No == 7)
    {
    Total += Quantity[6] * 19.90;
    }
    else if ( Product_No == 8)
    {
    Total += Quantity[7] * 25.90;
    }
    else if ( Product_No == 9)
    {
    Total += Quantity[8] * 29.90;
    }
    else if ( Product_No == 10)
    {
    Total += Quantity[9] * 19.90;
    }
    }
    Price += Total;
    printf("\n\n Please pay RM %.2f\n", Tax_Price = Price * 1.05);
    printf("Thank you. Please come again!!");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi,
    your problem is that you have no prompt for your getch....also all those if statements....arrrrggh. Try usin a switch instead.
    To make this work you will have to prompt the user for a choice of Q to Quit for every iteration of your loop. and it would be easier tomake the loop a do - while.

    Code:
     do
    {
        your code
        printf("Another selection 'Y' for yes, 'Q' to quit :");
     } while(toupper(getchar) != 'Q');
      your code
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Code:
    #include<stdio.h> 
    int main() 
    { 
    int Quantity[10], Product_No; 
    float Price = 0, Total = 0, Tax_Price = 0; 
    int number_read;
    
    while ( true) 
    { 
    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("\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--> "); 
    number_read = scanf("%d", &Product_No);
    if (number_read == 0)
       break;
    printf("How many orders of item number %d would you like? ", Product_No); 
    scanf("%d", &Quantity[Product_No - 1]); 
    
    if ( Product_No == 1) 
    { 
    Total += Quantity[0] * 47.90; 
    } 
    else if ( Product_No == 2) 
    { 
    Total += Quantity[1] * 28.90; 
    } 
    else if ( Product_No == 3) 
    { 
    Total += Quantity[2] * 17.90; 
    } 
    else if ( Product_No == 4) 
    { 
    Total += Quantity[3] * 19.90; 
    } 
    else if ( Product_No == 5) 
    { 
    Total += Quantity[4] * 39.50; 
    } 
    else if ( Product_No == 6) 
    { 
    Total += Quantity[5] * 34.90; 
    } 
    else if ( Product_No == 7) 
    { 
    Total += Quantity[6] * 19.90; 
    } 
    else if ( Product_No == 8) 
    { 
    Total += Quantity[7] * 25.90; 
    } 
    else if ( Product_No == 9) 
    { 
    Total += Quantity[8] * 29.90; 
    } 
    else if ( Product_No == 10) 
    { 
    Total += Quantity[9] * 19.90; 
    } 
    } 
    Price += Total; 
    printf("\n\n Please pay RM %.2f\n", Tax_Price = Price * 1.05); 
    printf("Thank you. Please come again!!"); 
    return 0; 
    }

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

    Unhappy *sigh*

    Thanks for replying, bigtamscot and swoopy.

    *Sigh* Bigtamscot, i used ur switchcase method, though ending 'Q' for one value does give me the right value but with more than one value (i mean to add up other values in menu), gives me same problem again. Though, i did not quite get what u meant by putting 'Q' in every iteration. I have updated my prog. below, maybe you could spot out for me which line is still causing the problem.

    *Sigh* Swoopy, i have used ur ifelse method, though it was incomplete of not having 'Q' (sorry to make you type up all that), so I added somemore codes to it, it does end in 'Q' BUT still the value is wrong.

    I went up to my professor and asked him my problem (Gosh man, the way he speaks is as if he is so nervous in speaking in English. Plus the whole doesn't understand what the hell he is trying to tell us, and his explanation sux), anywayz, from what i understand he said something about when you type in 81, it turns automatically into 'Q'. I know it it has something to do with this

    (Product_No=getchar()) != '81')

    or something like that. Could anyone of u drop an example of how this is done so that i can apply to my code and to my knowledge. Lastly, someone said that ending with 'Q', does some damage to the code. Is that true????? Certainly, there must be a way to end with 'Q'.

    #include<stdio.h>
    int main()
    {
    char End_Loop;
    int Quantity, Product_No;
    float Total = 0, Tax_Price = 0;

    do
    {

    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) RM 47.90\n");
    printf("\t2. Sumo Confort Super Value RM 28.90\n");
    printf("\t3. Pet-Pet Baby dry RM 17.90\n");
    printf("\t4. Chicolastic Ultra trim Jumbo RM 19.90\n");
    printf("\t5. Mamy Poko Disney RM 39.50\n");
    printf("\t6. Drypers Wee-Wee Mega Pack RM 34.90\n");
    printf("\t7. Royale Kids Grace Kiki Lala Shoes RM 19.90\n");
    printf("\t8. OK Baby Wear RM 25.90\n");
    printf("\t9. World Of Cartoon Kids Wear RM 29.90\n");
    printf("\t10. Christmas Teddy Bear RM 19.90\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);

    switch (Product_No)
    {
    case 1:
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity);
    Total = Total + (Quantity * 47.90);
    break;
    case 2:
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity);
    Total = Total + (Quantity * 28.90);
    break;
    case 3:
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity);
    Total = Total + (Quantity * 17.90);
    break;
    case 4:
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity);
    Total += Quantity * 19.90;
    break;
    case 5:
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity);
    Total += Quantity * 39.50;
    break;
    case 6:
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity);
    Total += Quantity * 34.90;
    break;
    case 7:
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity);
    Total += Quantity * 19.90;
    break;
    case 8:
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity);
    Total += Quantity * 25.90;
    break;
    case 9:
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity);
    Total += Quantity * 29.90;
    break;
    case 10:
    printf("How many orders of item number %d would you like? ", Product_No);
    scanf("%d", &Quantity);
    Total += Quantity * 19.90;
    break;
    default:
    printf("\nSorry, the Product No. you entered was not on the menu. Please try again!");
    }

    printf("Type letter [Q], if you are done with your selection and see the price");
    scanf("%c", &End_Loop);
    scanf("%c", &End_Loop);
    }

    while (End_Loop != 'Q');
    printf("\n\n\t\t\tPlease pay RM %.2f\n", Tax_Price = Total * 1.05);
    printf("Thank you. Please come again!!");
    return 0;
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    With my code, if you enter 'q', it exits the while() loop. The following code is what breaks it out of the loop.

    if (number_read == 0)
    break;

    Of course, use whatever method you feel most comfortable with.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Actually, if you enter anything besides a number, it will exit the while() loop. This may not be what you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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