Thread: why only the succeeding value adds up???

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    why only the succeeding value adds up???

    hi guys i wonder what i am messing in my program,

    Code:
    main()
    {
    clrscr();
    
    int order;
    char ans;
    int total;
    
    printf("Good Day Ma'am/Sir !!!\n\n");
    
    
    printf("\t1. Regular Yum \t\t\t\t\tP25.00\n");
    printf("\t2. One Piece Chicken Joy\t\t\tP55.00\n");
    printf("\t3. One Piece Burger Steak \t\t\tP45.00\n");
    printf("\t4. Spaghetti\t\t\t\t\tP35.00\n");
    printf("\t5. Regular Yum with Spaghetti\t\t\tP50.00\n");
    printf("\t6. One Piece Burger Steak with Spaghetti\tP70.00\n");
    printf("\t7. One Piece Chicken Joy with Spaghetti\t\tP80.00\n");
    printf("\t8. Softdrinks\t\t\t\t\tP15.00\n\n\n");
    
    do
    {
    printf("\nPlease select your order:  ");
    scanf("%d",&order);
    total=0;
    
    if(order==1){
           total=25.00;
           }
    else if (order==2){
    	total=55.00;
    	}
    else if(order==3){
    	total=45.00;
    	}
    else if (order==4){
    	total=35.00;
    	}
    else if(order==5){
    	total=50.00;
    	}
    else if (order==6){
    	total=70.00;
    	}
    else if(order==7){
    	total=80.00;
    	}
    else if(order==8){
    	total=15.00;
    	}
    
    total+=total;
    
    
    printf("\nWould you like to order again?(y/n)  ");
    scanf(" %c",&ans);
    
    }
    while(ans=='y');
    
    printf("\nYour total order is P%d.",total);
    
    getch();
    return(0);
    }
    when my program displays the total, only the second orders adds up or the succeeding order adds up?

    am i messing something???

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't have two variables with the same name -- you have an order total, which you call "total", and you have a total-for-the-whole-program, which you call "total". Needless to say, the compiler treats these as the same variable.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    [Never mind]
    Last edited by rags_to_riches; 01-01-2009 at 01:07 PM. Reason: tabstop beat me and has better eyes...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your analyzis of what goes wrong is probably incorrect - you are overwriting the "total" every time you select something, so if you order number 1 three times over, it will still end up with 2 times "total += total", where total has been set to 25 in the first case.

    [You may want to consider using "switch" instead of if-else to determine the choice].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    you mean like this???

    Code:
    main()
    {
    clrscr();
    
    int order;
    char ans;
    int total;
    int sum;
    
    printf("Good Day Ma'am/Sir !!!\n\n");
    printf("Welcome to Jollibee ...\n\n\n");
    
    printf("\t1. Regular Yum \t\t\t\t\tP25.00\n");
    printf("\t2. One Piece Chicken Joy\t\t\tP55.00\n");
    printf("\t3. One Piece Burger Steak \t\t\tP45.00\n");
    printf("\t4. Spaghetti\t\t\t\t\tP35.00\n");
    printf("\t5. Regular Yum with Spaghetti\t\t\tP50.00\n");
    printf("\t6. One Piece Burger Steak with Spaghetti\tP70.00\n");
    printf("\t7. One Piece Chicken Joy with Spaghetti\t\tP80.00\n");
    printf("\t8. Softdrinks\t\t\t\t\tP15.00\n\n\n");
    
    
    
    do
    {
    printf("\nPlease select your order:  ");
    scanf("%d",&order);
    total=0;
    
    if(order==1){
           total=25.00;
           }
    else if (order==2){
    	total=55.00;
    	}
    else if(order==3){
    	total=45.00;
    	}
    else if (order==4){
    	total=35.00;
    	}
    else if(order==5){
    	total=50.00;
    	}
    else if (order==6){
    	total=70.00;
    	}
    else if(order==7){
    	total=80.00;
    	}
    else if(order==8){
    	total=15.00;
    	}
    
    sum+=total;
    
    
    printf("\nWould you like to order again?(y/n)  ");
    scanf(" %c",&ans);
    
    }
    while(ans=='y');
    
    printf("\nYour total order is P%d.",sum);
    
    getch();
    return(0);
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That looks better. Does it work? [You may find that you get a strange result - what is sum before you start adding orders together?]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're nearly there -- you need to start sum off at 0.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It would be a lot easier to follow with some decent indentation.
    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.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Code:
    main()
    {
    clrscr();
    
    int order;
    char ans;
    int total;
    int sum;
    
    printf("Good Day Ma'am/Sir !!!\n\n");
    
    
    printf("\t1. Regular Yum \t\t\t\t\tP25.00\n");
    printf("\t2. One Piece Chicken Joy\t\t\tP55.00\n");
    printf("\t3. One Piece Burger Steak \t\t\tP45.00\n");
    printf("\t4. Spaghetti\t\t\t\t\tP35.00\n");
    printf("\t5. Regular Yum with Spaghetti\t\t\tP50.00\n");
    printf("\t6. One Piece Burger Steak with Spaghetti\tP70.00\n");
    printf("\t7. One Piece Chicken Joy with Spaghetti\t\tP80.00\n");
    printf("\t8. Softdrinks\t\t\t\t\tP15.00\n\n\n");
    
    
    
    do
    {
    printf("\nPlease select your order:  ");
    scanf("%d",&order);
    total=0;
    
    switch(order){
    case 1:
           total=25.00;
    
    case 2:
    	total=55.00;
    
    case 3:
    	total=45.00;
    
    case 4:
    	total=35.00;
    
    case 5:
    	total=50.00;
    
    case 6:
    	total=70.00;
    
    case 7:
    	total=80.00;
    
    case 8:
    	total=15.00;
    }
    
    
    sum+=total;
    
    
    printf("\nWould you like to order again?(y/n)  ");
    scanf(" %c",&ans);
    
    }
    while(ans=='y');
    
    printf("\nYour total order is P%d.",sum);
    
    getch();
    return(0);
    }
    now it goes more crazy this time T_T

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need breaks when you use a switch.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And your sum is still undefined... (Yes, you've declared the variable, but never set it to an initial value).

    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
    clrscr();
    
    int order;
    char ans;
    int total;
    int sum;
    
    printf("Good Day Ma'am/Sir !!!\n\n");
    
    
    printf("\t1. Regular Yum \t\t\t\t\tP25.00\n");
    printf("\t2. One Piece Chicken Joy\t\t\tP55.00\n");
    printf("\t3. One Piece Burger Steak \t\t\tP45.00\n");
    printf("\t4. Spaghetti\t\t\t\t\tP35.00\n");
    printf("\t5. Regular Yum with Spaghetti\t\t\tP50.00\n");
    printf("\t6. One Piece Burger Steak with Spaghetti\tP70.00\n");
    printf("\t7. One Piece Chicken Joy with Spaghetti\t\tP80.00\n");
    printf("\t8. Softdrinks\t\t\t\t\tP15.00\n\n\n");
    
    
    
    do
    {
    printf("\nPlease select your order:  ");
    scanf("%d",&order);
    total=0;
    
    switch(order){
    case 1:
           total=25.00;
           break;
    
    case 2:
    	total=55.00;
    	break;
    case 3:
    	total=45.00;
    	break;
    case 4:
    	total=35.00;
    	break;
    case 5:
    	total=50.00;
    	break;
    case 6:
    	total=70.00;
    	break;
    case 7:
    	total=80.00;
    	break;
    case 8:
    	total=15.00;
    	break;
    }
    
    
    sum+=total;
    
    
    printf("\nWould you like to order again?(y/n)  ");
    scanf(" %c",&ans);
    
    }
    while(ans=='y');
    
    printf("\nYour total order is P%d.",sum);
    
    getch();
    return(0);
    }

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    ok, i set the value for the SUM! please help here, i change some of the values and followed what you said, still im getting the wrong answers!

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
    clrscr();
    
    int order;
    char ans;
    int total=0;
    int sum;
    
    printf("Good Day Ma'am/Sir !!!\n\n");
    
    
    printf("\t1. Regular Yum \t\t\t\t\tP25.00\n");
    printf("\t2. One Piece Chicken Joy\t\t\tP55.00\n");
    printf("\t3. One Piece Burger Steak \t\t\tP45.00\n");
    printf("\t4. Spaghetti\t\t\t\t\tP35.00\n");
    printf("\t5. Regular Yum with Spaghetti\t\t\tP50.00\n");
    printf("\t6. One Piece Burger Steak with Spaghetti\tP70.00\n");
    printf("\t7. One Piece Chicken Joy with Spaghetti\t\tP80.00\n");
    printf("\t8. Softdrinks\t\t\t\t\tP15.00\n\n\n");
    
    
    
    do
    {
    printf("\nPlease select your order:  ");
    scanf("%d",&order);
    sum=0;
    
    switch(order){
    case 1:
           total=25.00;
           break;
    
    case 2:
    	total=55.00;
    	break;
    case 3:
    	total=45.00;
    	break;
    case 4:
    	total=35.00;
    	break;
    case 5:
    	total=50.00;
    	break;
    case 6:
    	total=70.00;
    	break;
    case 7:
    	total=80.00;
    	break;
    case 8:
    	total=15.00;
    	break;
    }
    
    
    sum+=total;
    
    printf("\nWould you like to order again?(y/n)  ");
    scanf(" %c",&ans);
    
    }
    while(ans=='y');
    
    printf("\nYour total order is P%d.",sum);
    
    getch();
    return(0);
    }

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably don't want to set the value of sum to zero in EACH iteration of the loop...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    yow thnx! i've set the SUM to the upper part of the program and works like charm...


    thnx a lot to you guys,

    but one more question, can i get it working by not using the switch ?

    instead i will use the if else command?

    more power ....!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ctime adds \n
    By Elkvis in forum C Programming
    Replies: 2
    Last Post: 05-19-2009, 07:54 AM
  2. Google maps adds keyhole integration
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-05-2005, 10:55 AM
  3. How to Remove computer adds
    By nipun in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-28-2004, 12:07 PM
  4. Function that adds "!" to end of String
    By M204Programmer in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2003, 03:07 PM
  5. banner adds
    By iain in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-16-2001, 11:35 AM

Tags for this Thread