Thread: Value is wrong. Someone help

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

    Exclamation Value is wrong. Someone help

    Below, is my prog. i know how to end loop with numbers and it gives me the correct value. I just learned how to end loop with 'q', though the value is not what i expected, the value got doubled!!! So someone plz tell me where im wrong. I still want it to end loop with 'q' and with right value also. I pretty much prefer if you just fix my program lines and with explanation so that i can understand better that way. Thx.

    #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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I just learned how to end loop with 'q',
    Ah, but you didn't, and that's the problem.

    Sure, it detects a 'q' eventually, but not after doing some damage to your code.

    I suggest you read this AGAIN!!!
    http://www.cprogramming.com/cboard/s...threadid=12019

    And this time, pay attention to the if/else logic, and actually incorporate it into your code.

    > I pretty much prefer if you just fix my program
    Yeah, right

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

    Post chill Salem

    Dude, chill out, Salem. I know u were ........ed with my last post for help with value. Besides, you were the one who helped me understood the function of ending loop with characters and that I thank you for helping me understand it and where my problem was. Also, after you helped me, I made many other programs to see that I understand it or not. And I did. If you look at the end of this post, its another of my prog., it’s a proof that I understood with your help and it works, thanks to you.

    Don’t think that I am doing the easy way out by sending posts for help, I am sending posts because I have many help problems with my professor. One thing for sure, as a student and a class rep. I should be complaning to this to the university administrator, and I will be doing that soon. Gosh, the way my professor talks, its like he is so nervous in speaking English. So I tried to ignore that and went up to him for help, and still the same problem when I speak to him one on one with my problem. He cant explain it properly, plus he messes up the class mind. There were rumors in this university, that only the programming teacher (this one only), nearly the whole class has failed and had to repeat the semester (I also saw the ‘resit list’ myself, rumors were true then). So I tried to survive reading the programming C book, and a lot of it I understand and I feel like not going to his miserable class. Though just some parts in the book I don’t understand and that is where I make up a problem and ask people online for help. Don’t think that I have been posting up my assignments, assignments I do on my own. Lastly, I realized I said ‘fix my program’, I apologize for saying that, what I actually meant to say is to ask people to point out which line(s) in my program is wrong, what should be done and with explanation so I can understand. This is what I have been doing lately and referring to book also, and I just pray that I pass this exam.

    Thanks for your attention in reading this post and I hope you understand the problem I’m in.

    #include<stdio.h>
    void main()
    {
    int order,number;
    float t1=0,t2=0,t3=0,t4=0,t5=0,t6=0,p1=0,p2=0,p3=0,p4=0, p5=0,p6=0,all=0;
    char ans;
    printf("Welcome to Mcdonalds, do you want to make an order(y/n)?");
    scanf("%c",&ans);
    while ((ans=='Y')||(ans=='y'))
    {
    printf("****************************************** *********");
    printf("\6 McDonalds Restaurant ");
    printf("\n**************************************** ***********");
    printf("\nMake your selection from the menu below : ");
    printf("\n1. Regular hamburger RM 1.99");
    printf("\n2. Regular cheeseburger RM 2.49");
    printf("\n3. Fish sandwich RM 2.99");
    printf("\n4. French fries RM 1.99");
    printf("\n5. Soft drink (regular) RM 1.79");
    printf("\n6. Soft drink (large) RM 2.29");
    printf("\nType Q when you are finished with your selections.");
    printf("\n**************************************** ***********");
    printf("\nPlease select 1,2,3,4,5,6: ");
    scanf("%d",&order);
    printf("\nHow many orders of item number %d would you like? ",order);
    scanf("%d",&number);

    if (order==1)
    {
    p1=number*1.99;
    t1+=p1;
    printf("The regular hamburger is : RM%.2f\n ",t1);

    }
    if (order==2)
    {
    p2=number*2.49;
    t2+=p2;
    printf("The regular cheeseburger is : RM%.2f\n ",t2);

    }
    if (order==3)
    {
    p3=number*2.99;
    t3+=p3;
    printf("The Fish sandwich is : RM%.2f\n ",t3);

    }
    if (order==4)
    {
    p4=number*1.99;
    t4+=p4;
    printf("The French fries is : RM%.2f\n ",t4);

    }
    if (order==5)
    {
    p5=number*1.79;
    t5+=p5;
    printf("The Soft drink (regular) is : RM%.2f\n ",t5);

    }
    if (order==6)
    {
    p6=number*2.29;
    t6+=p6;
    printf("The Soft drink (large) is : RM%.2f\n ",t6);

    }
    printf("Do you want to make any more order(press q if finish)?");
    scanf("%c",&ans);
    }
    if ((order=getchar())!='Q')
    {
    all=(t1+t2+t3+t4+t5+t6)*1.05;
    printf("Please pay: RM%.2f\n ",all);
    printf("Thank you for eating at McDonald's !");
    }

    }

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    11
    Hi take a look at this , the wife was out today so i spent some time on your program, its not perfect no real error checking done didn't have time but it works and calculates ok. It compiles and runs ok on bloodshed. If you work on it a bit more it will be a good little programme.

    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>





    int main()
    {
    int order,number;
    float t1=0,t2=0,t3=0,t4=0,t5=0,t6=0,p1=0,p2=0,p3=0,p4=0,
    p5=0,p6=0,total_order_value=0,all=0;
    char ans;
    printf("Welcome to Mcdonalds,do you want to make an order(y/n)?");

    while(ans!='Y') /*This means keep on looping*/
    {
    ans=toupper(getchar()); /*get a char from the keyboard*/
    if(ans=='Y')break; /*show the menu*/
    fflush(stdin);
    printf("\n ENTER 'Y' TO ORDER ");
    }


    printf(" **************************************************
    ");
    printf("\6 McDonalds Restaurant ");
    printf(" \n************************************************
    ");
    printf("\nMake your selection from the menu below : ");
    printf("\n1. Regular hamburger RM 1.99");
    printf("\n2. Regular cheeseburger RM 2.49");
    printf("\n3. Fish sandwich RM 2.99");
    printf("\n4. French fries RM 1.99");
    printf("\n5. Soft drink (regular) RM 1.79");
    printf("\n6. Soft drink (large) RM 2.29");
    printf("\nType Q when you are finished with your selections.");
    printf(" \n************************************************
    ");

    while(ans=='Y')
    {
    printf("\nPlease select 1,2,3,4,5,6: ");
    scanf("%d",&order);
    fflush(stdin); /*flush the input buffer*/
    printf("\nHow many orders of item number %d would you like? ",order);
    scanf("%d",&number);
    fflush(stdin); /*flush the input buffer*/

    t1=0,t2=0,t3=0,t4=0,t5=0,t6=0,p1=0,p2=0,p3=0,p4=0,
    p5=0,p6=0; /*initialise variables before each pass through loop*/

    if (order==1)
    {
    p1=number*1.99;
    t1+=p1;
    printf("The regular hamburger is : RM%.2f\n ",t1);
    }

    if (order==2)
    {
    p2=number*2.49;
    t2+=p2;
    printf("The regular cheeseburger is : RM%.2f\n ",t2);
    }

    if (order==3)
    {
    p3=number*2.99;
    t3+=p3;
    printf("The Fish sandwich is : RM%.2f\n ",t3);
    }

    if (order==4)
    {
    p4=number*1.99;
    t4+=p4;
    printf("The French fries is : RM%.2f\n ",t4);
    }

    if (order==5)
    {
    p5=number*1.79;
    t5+=p5;
    printf("The Soft drink (regular) is : RM%.2f\n ",t5);
    }

    if (order==6)
    {
    p6=number*2.29;
    t6+=p6;
    printf("The Soft drink (large) is : RM%.2f\n ",t6);
    }

    printf("Do you want to make any more order(press q if finish)?");
    ans=toupper(getchar()); /*get a char from the keyboard*/
    total_order_value=(total_order_value+(t1+t2+t3+t4+ t5+t6)*1.05);
    printf("\n\n Order total so far Including Tax is RM%.2f",total_order_value);
    }/*end of outer while*/

    printf("\n\nPlease pay: RM%.2f\n ",total_order_value);
    printf("\n\nThank you for eating at McDonald's !");
    printf("\n\n");
    system("PAUSE");

    }/*end main*/

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    11

    Talking Tax calculation take it put of the loop!!

    re my code, take the tax calc out of the loop and do it at the end of the program.

    like this:

    printf("Do you want to make any more order(press q if finish)?");
    ans=toupper(getchar()); /*get a char from the keyboard*/
    total_order_value=total_order_value+(t1+t2+t3+t4+t 5+t6);
    printf("\n\nOrder total so far is RM%.2f",total_order_value);
    }/*end of outer while*/

    printf("\n\nPlease pay: RM%.2f including tax\n",total_order_value*1.05);
    printf("\n\nThank you for eating at McDonald's !");
    printf("\n\n");
    system("PAUSE");

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Just as an FYI, if I see a question that has code which is longer than say 10 or 15 lines and doesn't use the [ code ] [ /code ] tags, I'll bypass it completely and move on to the next question.

    Quite a few of us do this, it's just not worth the hasstle to reformat a larger program when we don't have a great deal of free time.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM