带代数的蛋白质计算器

Proteincalculator with algebra

我正在尝试使这个蛋白质计算器(用 C 语言)工作,但它不起作用:

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

int main()
{
    double proteinhalt[20];
    double proteinmangdtot[20];
    double kottfiskmengdtot;
    printf("Enter proteinhalt / 100 gram: ");
    fgets(proteinhalt, 20, stdin);
    printf("Enter proteinmangd att konsumera idag (gram): ");
    fgets(proteinmangdtot, 20, stdin);
    kottfiskmengdtot = ((double)proteinmangdtot/(double)proteinhalt)*100;
    printf("Du behöver %f gram.", kottfiskmengdtot);
}

错误是:

Line 13   error: pointer value used where a floating-point was expected

怎么了?

编辑:英文:

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

int main()
{
    double proteinpercentage[20];
    double proteinamounttot[20];
    double meatfishamounttot;
    printf("Enter proteinpercentage / 100 gram: ");
    fgets(proteinpercentage, 20, stdin);
    printf("Enter protein amount to consume today (gram): ");
    fgets(proteinamounttot, 20, stdin);
    meatfishamounttot = ((double)proteinamounttot/(double)proteinpercentage)*100;
    printf("You need %f gram.", meatfishamounttot);
}

你有两个错误。

  1. fgets,需要一个字符数组作为参数,但你指定了一个双精度数组

  2. 在 meatfishamounttot 的分配中,您将双精度数组转换为双精度数组

解决方案(简化,无错误检查):

char str[20];
double proteinpercentage;
double proteinamounttot;
double meatfishamounttot;

// read at most 20 characters from stdin
fgets(str, 20, stdin);
// convert the string to a double
proteinpercentage = strtod(str, NULL);

fgets(str, 20, stdin);
proteinamounttot = strtod(str, NULL);

meatfishamounttot = (proteinamounttot / proteinpercentage) * 100;

程序应该是这样工作的:

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

int main()
{

char str[20];
double proteinpercentage;
double proteinamounttot;
double meatfishamounttot;

printf("Enter proteinpercentage / 100 gram: ");
fgets(str, 20, stdin);
proteinpercentage = strtod(str, NULL);
printf("Enter protein amount to consume today (gram): ");
fgets(str, 20, stdin);
proteinamounttot = strtod(str, NULL);

meatfishamounttot = (proteinamounttot / proteinpercentage) * 100;
printf("You need %f grams.", meatfishamounttot);

}