0

Here I am trying to write a code to add the final_mark data next to a given set of data from another file. Some data have decimals so I tried changing the variable type to float or double but the output was a mess so I decided to just print back the full line in the input file and output the full line + final_mark data.

while ( fgets(buffer,100,cfptr) != NULL ) {
        fgets(buffer,100,cfptr);
        fscanf(cfptr, "%d%d%d%d%d%d%d%d", &T1, &T2, &T3, &HW1, &HW2, &HW3, &HW4, &PRJ);
        HW_total = HW1 + HW2 + HW3 + HW4;
        final_mark = T1 * 0.2 + T2 * 0.2 + T3 * 0.2 + HW_total * 0.1 + PRJ * 0.3;
        fprintf(fmptr, "%s  %d\n", buffer, final_mark);

But since I used fgets() for the string, it automatically proceeds to a new line after it so final_mark data will come in the next line.

20   41   40   1.5   10   10   10   80
  61
39   23   35   10   10   10   10   75
  56

So how do I make it so that the number will appear in the same line?

  • So after the user enters the `'\n'`, you want code to go back up a line, to the end of the previous line and then print the `final_mark`? – chux - Reinstate Monica Dec 10 '21 at 04:26
  • Aside: For computational purposes, `final_mark = T1 * 0.2 + T2 * 0.2 + T3 * 0.2 + HW_total * 0.1 + PRJ * 0.3;` better as `final_mark = (T1 * 2 + T2 * 2 + T3 *2 + HW_total * 1 + PRJ * 3 + 5)/10;` and forego the FP math. – chux - Reinstate Monica Dec 10 '21 at 04:30

0 Answers0