如何将相同的变量添加到自身?

时间:2016-10-15 03:39:40

标签: c variables addition

我试图在C中添加一个变量。我必须解决的问题要求我询问用户X发生了多少次Y.简单示例:每次有人喝果汁(x) ,我需要反复添加果汁量(y)。 到目前为止,这是我的计划。我想要做的一切都是按照我的知识运作,除了最后一段代码,我需要弄清楚在&#34之前需要做什么;如果"声明。提前感谢您的协助。

#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;
}

3 个答案:

答案 0 :(得分:0)

我相信这就是你要找的东西:

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

在这里,您要将啜饮的数量加到啜饮的总数中。

答案 1 :(得分:0)

您的代码存在的问题是您从未在代码中添加了总量。因此,在最后一个if语句之前,您需要添加该行。它看起来像:

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

这将解决您当前的问题。

答案 2 :(得分:0)

这是正确的和最终的代码人。感谢您所有的帮助!天才到处都是!

Found

The requested resource /webapp/XXX/XXXXX does not allow request data with POST requests, or the amount of data provided in the request exceeds the capacity limit.

Additionally, a 413 Request Entity Too Large error was encountered while trying to use an ErrorDocument to handle the request.