2016-10-15 8 views
0

私はCで1つの変数を追加しようとしています。解決しなければならない問題は、XがYに何回起こったかをユーザーに尋ねることです。簡単な例:誰かがジュース(x)を飲むたびに、ジュースの量(y)を繰り返し添加する。 これまでの私のプログラムです。私がしようとしていることは、 "if"ステートメントの前にどれが必要であるかを理解するために必要なコードの最後の部分を除いて、自分の知識に意図されたとおりに働いていることです。ご協力いただきありがとうございます。同じ変数を自分自身に追加するにはどうすればよいですか?

#include <stdio.h> 
int main(){ 
int a=1; 
int b=1; 
int i; 
float dollars; 
float size; 
float price;//per ounceprice 
float wieght; 
int drinks;//times roommate took some juice 
int c=0; 
int sips; 
int total; 
int totalowed; 
int loopCounter; 
int sipstotal; 
//.06 per ounce 
float juiceTaken; 
float juiceTakenCost; 
float juiceTakenTotal; 
float costperounce=.06; 

while(a=b){ 
    printf("What is the weight (in oz.) of the original container of OJ?\n\n"); 
    scanf("%f", &wieght); 

    printf("What is the cost of the original container of OJ in dollars?\n\n"); 
    scanf("%f", &dollars); 
    price=dollars/wieght; 
    printf("%f\n\n", price); 

    printf("How many times did your roomate take your juice?\n\n"); 
    scanf("%d", &drinks); 


     for(loopCounter = 0; loopCounter < drinks; loopCounter++){//repeat next line until loop equals amount of times roomate took juice 
     printf("How much juice did your roommate take this time?\n\n"); 
     scanf("%d", &juiceTaken); 
      if(juiceTakenTotal>=10) 
      printf("Your roomate owes you $%.2f\n", juiceTakenTotal); 



} 
} 

return 0; 
} 
+0

ありがとうございます!あなたはどちらも効果的なソリューションを提供しました!私は最初に試したときに動作しなかったコードにも別の問題を発見しました。私のscanf行では、juiceTakenを要求するときに%fの代わりに%dに注意します。私は整数値を求めて浮動小数点値をスキャンしています。笑、助けてくれてありがとう。 –

答えて

0

あなたのコードの問題は、あなたのコード内で合計に取られたジュースの量を決して追加しなかったことです。最後のif文の直前に行を追加する必要があります。

scanf("%d", &juiceTaken); 
juiceTakenTotal = juiceTakenTotal + juiceTaken; 
     if(juiceTakenTotal>=10) 
     printf("Your roomate owes you $%.2f\n", juiceTakenTotal); 

これはあなたの現在の問題を解決します。

0

私は、これはあなたが探しているものであると信じて:

ここ
scanf("%d", &juiceTaken); 
    juiceTakenTotal = juiceTakenTotal + juiceTaken; 
      if(juiceTakenTotal>=10) 
      price = juiceTakenTotal * price //calculating price 
      printf("Your roomate owes you $%.2f\n", juiceTakenTotal); 

、あなたが撮影したSIPSの合計数に一口の数を追加しています。

+0

私は、 "if"の前の行でそれをやろうとしていますが、juiceTakenの "scanf"の後に、juiceTakenが新しい合計のために追加されるようにしています。 –

+0

アイデアは、 "drink" = 10の場合、それはjuicetakenを10回繰り返し要求するということです。 juicetakenの値は、ユーザーに質問されるたびに合計を加算する必要があります。そして、その合計金額に価格を乗じ、 "if"を使って10以上であるかどうかを確認します。 –

+0

あなたはjuiceTakenTotalの価格をチェックしますか? – Fabulous

0

ここに、正しいコードの最後のコードがあります。あなたの助けをありがとう!天才がいっぱい!

#include <stdio.h> 
int main(){ 
int a=1; 
int b=1; 
float dollars; 
float price;//per ounceprice 
float wieght; 
int drinks;//times roommate took some juice 
int loopCounter; 
float juiceTaken; 
float juiceTakenCost; 
float juiceTakenTotal; 

while(a=b){ 
    printf("What is the weight (in oz.) of the original container of OJ?\n\n"); 
    scanf("%f", &wieght); 

    printf("What is the cost of the original container of OJ in dollars?\n\n"); 
    scanf("%f", &dollars); 
    price=dollars/wieght; 
    printf("%f\n\n", price); 

    printf("How many times did your roomate take your juice?\n\n"); 
    scanf("%d", &drinks); 
    //6 cents per ounce of juice 
    //Insert 2nd loop condition - for loop 
     for(loopCounter = 0; loopCounter < drinks; loopCounter++){//repeat next line until loop equals amount of times roomate took juice 
     printf("How much juice did your roommate take this time?\n\n"); 
     scanf("%f", &juiceTaken); 
     juiceTakenTotal=juiceTakenTotal + juiceTaken; 
     juiceTakenCost=juiceTakenTotal*price; 
      if(juiceTakenCost>=10) 
      printf("Your roomate owes you $%.2f\n", juiceTakenCost); 
      } 
    } 
return 0; 
} 
関連する問題