2017-01-07 5 views
-1

私は現金登録プログラムを書いていました。私はほとんどすべてを行っていました。私のプログラムはほぼ終了しました。しかし、私は問題が1つあります。このプログラムは、最終的には、ユーザーがテイクアウトに行くことを望んでいる。しかし、ユーザーが現金を入力して支払う必要があるとき、私は問題に直面する。ユーザーが与えた現金が、購入した商品の合計金額を超える場合、変更をユーザーに返しますが、ユーザーが指定した現金がユーザーが選択した総商品数よりも少ない場合は、「不足している金額を追加しました」と表示されますが、代わりに整数値が負の形式で表示されます.itは負の形式で変更を表示します.iはbottom.please help me.itsで出力の例を示しています。 '<'演算子は動作しません。!=、==、>うまく動作します。ありがとう!'<' if文では動作しません

#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 

しかし、それは

"不十分な量given.Addがより"

それはthat.whyをしない表示する必要がありますか?あなたのコードで

+1

あなたはそれを思っています... '合計<("%d "、&現金)'無意味です、ちょうど '合計<現金 'をしてください – JJJ

+2

@JJJですが、また、制約違反であるため、正直なコンパイラ*はこの行に対して警告を出す必要があります。 – EOF

+1

Cコンパイラに警告が表示された場合は、コードにバグが見つかりました。つまり、警告が表示されないようにコードを修正する必要があります。また、どこにでもキャストを貼って修正する必要はありません。コードに警告がある場合は、そう言います。警告の修正方法と警告の意味について尋ねます。あなたの目標は、厳しい警告オプションの下で警告なしでコンパイルする必要があります。 –

答えて

3

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

が間違っています。 意味のあるものはありませんあなたは何の構文エラーがないので、これが唯一の結果としてRHSオペランドを使用していますcomma operatorのプロパティを使用しています

if(total < cash) 

FWIW、if(total < ("%d",&cash))コンパイルを、書きたいことがあります。

しかし、このコードでは、§6.5.8、C11章のように、関係演算子の制約違反が含まれるため、このコードは未定義の動作を呼び出します。

+0

制約違反*はコンパイラからの診断メッセージを必要とします。 – EOF

+0

@EOFしかし、コンパイラがコンパイルを進め、そのコードでバイナリを生成することを選択した場合、その動作はUBです。 –

+0

はい。要点は、OP *がコンパイラの警告*を述べたはずであるということです。 – EOF

関連する問題