1

I am a beginner in C. I am making a simple game in c. I have a .txt file that stores players' score such as

gse
12
CKY 
8
asf
8
FWD
7
BCS
6
BBC
5
PYS
4
NSE
3
Jaeh
2
PJH
1

and the one of the functions in my c code is the following:

void ViewScoreBoard() { 
int n = 0;
int c;
char buffer[NAME_LENGTH] = { 0 };
FILE *fp = fopen("scoreBoard.txt", "r");

if (fp == NULL) {
    printf("no score\n");
    fflush(stdin);
    getchar();
    return;
}

system("cls");//clears the screen

printf("=======SCORE=======\n");
if (fp){
   while(fgets(buffer, sizeof buffer, fp)!=NULL){
        if(n%2==0){
            printf("%2d. ",n/2+1); //index
            buffer[strcspn(buffer, "\n")] = 0; //stripes the newline chr
            printf("%5s",buffer);            
        }
        if(n%2==1){
            printf("%5s",buffer);
        }
        n++;
    }

    fclose(fp);
}

printf("Enter AnyKeys");
Sleep(100);
getchar();
getchar();}

The problem is this. On the console, the score is printed out fine up until 3.(before Jaeh)

=======SCORE=======
 1.   gse  12
 2.   CKY   8
 3.   asf   8
 4.   FWD   7
 5.   BCS   6
 6.   BBC   5
 7.   PYS   4
 8.   NSE   3
 9.  Jaeh
10.     2 PJH
11.     1Enter AnyKeys

I need 2 to be inline with Jaeh but don't know what's wrong... Thanks in advance!

***EDIT

changed the code to char buffer[NAME_LENGTH+2] = { 0 }; and everything works fine.

Thanks everyone!

Py11
  • 159
  • 1
  • 2
  • 8
  • 1
    Note: follow-up to OP's [recent question](https://stackoverflow.com/questions/50239406/first-character-in-txt-file-doesnt-print-in-c) – Weather Vane May 08 '18 at 19:17
  • seems that the input has some empty lines. – Jean-François Fabre May 08 '18 at 19:18
  • 3
    Calling `fflush` with an input-only stream (like `stdin`) is explicitly mentioned in the C specification as *undefined behavior*. Some implementations allow it as an *extension* of the language, but please try to avoid it. – Some programmer dude May 08 '18 at 19:20
  • 2
    Show what `NAME_LENGTH` is. – Weather Vane May 08 '18 at 19:21
  • UV for `//stripes the newline chr` but that should be just after `fgets`. – Weather Vane May 08 '18 at 19:22
  • @WeatherVane `#define NAME_LENGTH 5 #define LIST_LENGTH 10` – Py11 May 08 '18 at 19:25
  • @WeatherVane I think OP wants the newline in there when printing the score. Note the lack of a newline char in the `printf`. – 001 May 08 '18 at 19:26
  • 4
    @Py11 "Jaeh" + '\n' + '\0' requires 6 chars. – 001 May 08 '18 at 19:27
  • My comment from a deleted answer: the buffer size is too small, which is where things go wrong. The fault is because there was not enough room for the newline, which gets read on the next iteration. So don't be mean with the `fgets` buffer size, it's a one-off and its content will be processed and passed on to smaller things. – Weather Vane May 08 '18 at 20:06
  • I did not see the comment with the buffer size being too small and I assumed it was big enough when I tested it - that led to my wrong answer. Decided it's best to delete the wrong answer:). – малин чекуров May 08 '18 at 20:12
  • It was the comment from @JohnnyMopp above. 4 + 1 + 1 = 6. – Weather Vane May 08 '18 at 20:14
  • @WeatherVane Indeed, but I missed the comment with `#define NAME_LENGTH 5`, so when I tested it I tested with big enough buffer, assuming it was big enough in the original code. Sometimes correct code leads to incorrect answers:). – малин чекуров May 08 '18 at 20:21
  • changed the code to `char buffer[NAME_LENGTH+2] = { 0 };` and everything works fine. – Py11 May 08 '18 at 20:22
  • @Py11 don't be mean define `char buffer[100]` – Weather Vane May 08 '18 at 20:24

0 Answers0