0

I keep getting a formatting error and I am not sure of how adjust my code.

int main()
{
    FILE* fp=fopen("food.txt","r");
    float costs_2018[10];
    float costs_2020[10];
    char* name[1000][10];
    char* quantity[1000][10];
    fscanf(fp,"%s %s %s %s",&name[0], &quantity[0], &costs_2018[0], &costs_2020[0]);
    for (int i=0;i<10;i++)
    {
        fscanf(fp,"%s %d%*s %f %f",&name[i],&quantity[i],&costs_2018[i],&costs_2020[i]);
    }
    printf("total cost in 2018 is = %f",calculate_total_cost(costs_2018));
    printf("\ntotal cost in 2020 is = %f",calculate_total_cost(costs_2020));
    printf("\naverage cost in 2018 is = %f",calculate_total_average(costs_2018));
    printf("\naverage cost in 2020 is = %f",calculate_total_average(costs_2020));
    printf("\nDifference in total price between 2018 and 2020 is = %f",calculate_total_cost(costs_2018)-calculate_total_cost(costs_2020));
    printf("\nDifference in average price between 2018 and 2020 is = %f",calculate_total_average(costs_2018)-calculate_total_average(costs_2020));
    return 0;
}

on both of my "fscanf" lines im getting a formatting error. The first fscanf line name,quantity,costs_2018,costs2019 are underlined red. The 2nd fscanf line just name and quantity are underlined red.

AB_12
  • 9
  • 3
  • What do you intend to achieve with your code? – Armali May 23 '21 at 20:41
  • 1
    Why `fscanf` outside and inside the loop? – Armali May 23 '21 at 20:42
  • name and quantity **are arrays of pointers** to type char. What you probably want is a pointer that points to a 2-dimensional array of chars. In that case it is `char (*name)[1000][10];` –  May 23 '21 at 20:55
  • I don't think the extra code you just added to the question makes the question clearer. It was pretty clear already. – Ted Lyngmo May 23 '21 at 20:56

2 Answers2

1

It looks like you want 10 char[1000]'s for name and quantity.

Declare those like this:

char name[10][1000];
char quantity[10][1000];

Then passing the arguments to scanf should be done like this:

if(fscanf(fp,"%999s %999s %f %f", name[i], quantity[i],
                                  &costs_2018[i], &costs_2020[i]) == 4)
{
    /* success */
} else {
    /* failure */
}

All arrays (like the char[1000] array at name[i]) decay into pointers when passed as arguments to functions which is why &name[i] should not be used.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

char* name[1000][10]; is a 1000x10 array of (uninitialised) char pointers. What you probably mean is char name[10][1000] - an array of 10 strings with up to 1000 chars each. Char arrays are by nature pointers. There's no need to put & there. So right line is fscanf(fp,"%s ...", name[0], ...).

Plus, line fscanf(fp,"%s %s %s %s",&name[0], &quantity[0], &costs_2018[0], &costs_2020[0]); specifies string arguments for all parameters, but the two cost fields are floats.

  • how would i specify float arguments for the two cost fields ? or would it be better to just change the cost fields to a string argument – AB_12 May 23 '21 at 21:05
  • Floats are `%f`. A C++ reference or a short internet search for "printf format specifiers" gives extensive lists of all available specifiers. – senatores conscripti May 23 '21 at 21:09