0

I need some assistance. I'm trying to write my code's output into a file. However, I'm encountering an issue whenever I attempt to use fprintf within an array. The code works as it should without the fprintf statements, printing 5 scores per line. When added, it appears to continue increment the array?

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

void displayScore(int testScores[]);
FILE *fpOut;

int main(void) {

if (!(fpOut = fopen("csis.txt", "w"))) {
    printf("csis.txt could not be opened for input.");
    exit(1);
}

int testScores[] = { 90, 85, 100, 50, 50, 85, 60, 70, 55, 55, 80, 95, 70, 60, 95, 80, 100, 75, 70, 95, 90, 90, 70, 95, 50, 65, 85, 95, 100, 65 };

displayScore(testScores);

fclose(fpOut);
system("pause");
return 0;
}

void displayScore(int testScores[]) {
int i = 0;

/*Prints 5 scores every line*/
for (i = 0; i < 30; i++) {
    printf("%d, ", testScores[i]);
    fprintf(fpOut, "%d, ", testScores[i]);
    printf("%5d, ", testScores[i += 1]);
    fprintf(fpOut, "%5d, ", testScores[i += 1]);
    printf("%5d, ", testScores[i += 1]);
    fprintf(fpOut, "%5d, ", testScores[i += 1]);
    printf("%5d, ", testScores[i += 1]);
    fprintf(fpOut, "%5d, ", testScores[i += 1]);
    printf("%5d, \n", testScores[i += 1]);
    fprintf(fpOut, "%5d, \n", testScores[i += 1]);
}
return;
}
  • 1
    The error message if you fail to open `csis.txt` is quite misleading, as you don't open the file for input at all. – Some programmer dude Aug 01 '18 at 07:35
  • 3
    As for your problem, nothing a little [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) shouldn't help... Hint: How many times do you increase `i` with `i += 1`? How many times *should* you do it? – Some programmer dude Aug 01 '18 at 07:37
  • 1
    Hint received, face-palm x20, Still relatively new to this. Wasn't sure if there was some operator I needed to use or special syntax. Thank you. – Tak_fortell Aug 01 '18 at 07:43
  • 1
    BTW, `i += 1` is exactly equivalent to `++i` –  Aug 01 '18 at 07:54

2 Answers2

1

As has been pointed out, with

printf("%5d, ", testScores[i += 1]);
fprintf(fpOut, "%5d, ", testScores[i += 1]);

you are printing one number to stdout and the next number to the file. So every alternate number will end up in the file and the other numbers will be printed out to stdout.

You could modify this to

printf("%5d, ", testScores[i]);
fprintf(fpOut, "%5d, ", testScores[i]);
i+=1;

ie, increment i only after printing to both stdout and the file.

Or you could use the postfix increment operator like

printf("%5d, ", testScores[i]);
fprintf(fpOut, "%5d, ", testScores[i++]);

here in i++, the value of i will be incremented but the initial value of i will be used in the expression. Read What is the difference between prefix and postfix operators? .

And instead of repeating by yourself, you could make the computer do the repeating with something like

for (i = 0; i < 30; i++) {
    printf("%5d, ", testScores[i]);
    fprintf(fpOut, "%5d, ", testScores[i]);
    if((i+1)%5==0)
    { 
        printf("\n");
        fprintf(fpOut, "\n");
        }

}

and print the newline only if i+1 is divisible by 5.


The use of system() is discouraged. See Why should the system() function be avoided in C and C++? .

J...S
  • 5,079
  • 1
  • 20
  • 35
0

When added, it appears to continue increment the array?

Yes, you increment your array index too often:

printf("%5d, ", testScores[i += 1]);
fprintf(fpOut, "%5d, ", testScores[i += 1]);

You increment your counter i once for printf and once again for fprintf. While you probably want to print the same element in both commands, you access two adjacent elements.

To fix this only increment once.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39