Im trying to make this protein-calculator (in C language) to work but it doesn't work:
#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);
}
The error is:
Line 13 error: pointer value used where a floating-point was expected
What is wrong?
Edit: In english:
#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);
}
You have two errors.
fgets, expects a character array as argument, but you specify an array of doubles
at the assignment of meatfishamounttot, you cast an array of doubles to a double
Solution (simplified, no error checking):
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;