1

私は、ユーザーの入力を受け取り、その入力を入力したい金額をユーザーに入力させるプログラムを作成しましたが、合計と金額を印刷するときに。ただし、これは全面的に同じ値を取り戻しています。

合計:5.00食べ物:5.00請求書:5.00旅行:5.00たばこ:5.00

それ以外の:

合計:14.00食べ物:2.00請求書:3.00旅行:4.00たばこ:5.00

int main(void)
{

float food = 0.00;
float travel = 0.00;
float bills = 0.00;
float fags = 0.00;
float total = 0.00;

float t_food, t_travel, t_bills, t_fags;

char userInput[3];

while(userInput[0] != 'X')
{

    printf("Select option,\nA: Food\nB: Travel\nC: Bills\nD: Fags\nX: Exit\n");
    scanf("%s", userInput);
    if((userInput[0] == 'A') || (userInput[0] =='a'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &food);
        printf("You have entered: %.2f\n", food);
        t_food += food;

    }
    if((userInput[0] == 'B') || (userInput[0] =='b'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &travel);
        printf("You have entered: %.2f\n", travel);
        t_travel += travel;

    }
    if((userInput[0] == 'C') || (userInput[0] =='c'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &bills);
        printf("You have entered: %.2f\n", bills);
        t_bills += bills;

    }
    if((userInput[0] == 'D') || (userInput[0] =='d'))
    {
        printf("Please enter an amount: ");
        scanf("%f", &fags);
        printf("You have entered: %.2f\n", fags);
        t_fags += fags;

    }
    if((userInput[0] =='X') || (userInput[0] =='x'))
    {
        total = t_food + t_fags + t_travel + t_bills;

        printf("Total: %.2f\n", &total);
        printf("Food: %.2f\n", &t_food);
        printf("Travel: %.2f\n", &t_travel);
        printf("Bills: %.2f\n", &t_bills);
        printf("Fags: %.2f\n", &t_fags);
        break;
    }
}
return 0;

}

何か案は?

4

5 に答える 5

7

変化する

    printf("Total: %.2f\n", &total);
    printf("Food: %.2f\n", &t_food);
    printf("Travel: %.2f\n", &t_travel);
    printf("Bills: %.2f\n", &t_bills);
    printf("Fags: %.2f\n", &t_fags);

    printf("Total: %.2f\n", total);
    printf("Food: %.2f\n", t_food);
    printf("Travel: %.2f\n", t_travel);
    printf("Bills: %.2f\n", t_bills);
    printf("Fags: %.2f\n", t_fags); 

コンパイラの言うことを聞いてください。

warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat]
于 2013-03-16T11:33:15.690 に答える
1

をドロップし&ますprintf。これは、値自体ではなく、値の場所を渡すことを意味します。

于 2013-03-16T11:32:50.547 に答える
1

アドレスではなく、の値を使用する必要がprintfあります。

于 2013-03-16T11:33:22.083 に答える
0

&を削除すると、最後の if コードブロックで、すべて正常に動作します

于 2013-03-16T11:52:35.320 に答える
0

printf に関する以前の回答に加えて、合計値を含む float 変数も初期化する必要があります。何かのようなもの:

float t_food=0, t_travel=0, t_bills=0, t_fags=0;
...
printf("Total: %.2f\n", total);
printf("Food: %.2f\n", t_food);
printf("Travel: %.2f\n", t_travel);
printf("Bills: %.2f\n", t_bills);
printf("Fags: %.2f\n", t_fags);
于 2013-03-16T13:29:16.837 に答える