'<' not working in if statement

83 views Asked by At

so i was writing a cash register program.I've done mostly everything.My program is almost finish.But i have one problem.this program asks user to choose products and calculate total sum in the end if user wishes to go to takeout.but when the user has to input cash to pay;i face a problem.if the cash given by the user is more than the total amount of products the user chose to buy,it calculates and returns the user the change,but if the cash given by the user is less than the total amount of products chosen by the user,it should display"Insufficient amount given.Add more".but instead it displays an integer value in negative form.it displays the change in negative form.i have given an example of the output at the bottom.please help me.its so fraustrating. '<' operator doesnt seem to work.!=,==,> works fine.Thank You!

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

int main()
{
int a=8,b=10,c=20,d=50,e=30,x,subtotal1=0,subtotal2=0,subtotal3=0,subtotal4=0,subtotal5=0,shopping=1,cash;
int total=subtotal1+subtotal2+subtotal3+subtotal4+subtotal5;
char choice,code,choice2;
printf("                                           WELCOME TO KFC!           \n");
printf("Here are our sample list of products you might be interested in.\n\n");
while(shopping)
{
    printf("\nChoose your product   Enter product code i.e:A,C etc\n\n");
    printf("[A]\n");
    printf("[B]\n");
    printf("[C]\n");
    printf("[D]\n");
    printf("[E]\n");
    printf("[Q] for Quitting\n\n");




    code=getch();

    if(code=='q' || code=='Q')exit(0);

    if(code=='a' || code=='A')
    {
        printf("You have chosen 'A' and price is 8$\n\n");
        printf("Enter Quantity:\n\n");
        scanf("%d",&x);
        subtotal1=a*x;
        printf("Subtotal price is %d\n\n",subtotal1);
    }
    if(code=='b' || code=='B')
    {
        printf("You have chosen 'B' and price is 10$\n");
        printf("Enter Quantity:\n");
        scanf("%d",&x);
        subtotal2=b*x;
        printf("Subtotal price is %d\n",subtotal2);
    }
    if(code=='c' || code=='C')
    {
        printf("You have chosen 'C' and price is 20$\n");
        printf("Enter Quantity:\n");
        scanf("%d",&x);
        subtotal3=c*x;
        printf("Subtotal price is %d\n",subtotal3);
    }
    if(code=='d' || code=='D')
    {
        printf("You have chosen 'D' and price is 50$\n");
        printf("Enter Quantity:\n");
        scanf("%d",&x);
        subtotal4=d*x;
        printf("Subtotal price is %d\n",subtotal4);
    }
    if(code=='E' || code=='e')
    {
        printf("You have chosen 'E' and price is 30$\n");
        printf("Enter Quantity:\n");
        scanf("%d",&x);
        subtotal5=e*x;
        printf("Subtotal price is %d\n",subtotal5);
    }

     printf("Do you want to shop more?\n");
     choice=getch();

     if(choice=='y' || choice=='Y')shopping=1;
     else if(choice=='n' || choice=='N')
     {
         total=subtotal1+subtotal2+subtotal3+subtotal4+subtotal5;
         printf("Do you want to add them to takeout?\n\n");
         choice2=getch();
         if(choice2=='y' || choice2=='Y')
         {

             printf("Enter cash:\n");
             scanf("%d",&cash);
             total=subtotal1+subtotal2+subtotal3+subtotal4+subtotal5;
             if(total < ("%d",&cash))
             {
              printf("You have given %d\n",cash);

              printf("Your change is %d\n",cash-total);
             }
            else printf("Insufficient amount given.Add more.\n");

             shopping=0;
         }
         else shopping=1;
     }

}    

}   


                                       WELCOME TO KFC!
Here are our sample list of products you might be interested in.


Choose your product   Enter product code i.e:A,C etc

[A]
[B]
[C]
[D]
[E]
[Q] for Quitting

You have chosen 'A' and price is 8$

Enter Quantity:

12
Subtotal price is 96

Do you want to shop more?
Do you want to add them to takeout?

Enter cash:
90
You have given 90
Your change is -6

But it should display

"Insufficient amount given.Add more"

it doesn't do that.why?

1

There are 1 answers

4
Sourav Ghosh On BEST ANSWER

In your code,

 if(total < ("%d",&cash))

is wrong. It does not do anything meaningful. You may want to write

 if(total < cash)

FWIW, if(total < ("%d",&cash)) compiles, because there is no syntax error, this uses the property of the comma operator which only uses the RHS operand as the result.

However, as the code involves a constraint violation for relational operator, as per chapter ยง6.5.8, C11, this code thus invokes undefined behaviour.