I need help with a program. I need to know how to make it so the lines with a -1 will not show up. The code is linked to a file. The file looks like this:
4 112.7
154 115.5
4 116.4
321 -1
72 129.8
321 -1
154 219.0
72 128.8
555 110.8
555 137.6
432 86.7
432 -1

If the score was equal to -1 then it would be null. What would I put in here to make it so this happens.

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main()
{

FILE *olympicfile;
float x;
int i;
int j;
int s;
float Scores[12][2];

olympicfile = fopen( "speed", "r" );
if (olympicfile == NULL)
{
 printf("olympicfile not available\n");
 exit(1);
}
for(i = 0; i < 12; i++)
 {
  for (j = 0; j < 2; j++)
  {
   fscanf(olympicfile, "%f", &x);
   Scores[i][j] = x;
  }
 }
 for(i = 0; i < 12; ++i)
  {
   for(j = 0; j < 2; ++j)
    {
     printf("%5.1f ", Scores[i][j], Scores[i][j]);
    }
   printf("\n");
  }
fclose( olympicfile );
}

You have to deal with the array index and the loop index i separately.

Only increment the array index if -1 is not read.
If you see the -1, do not increment the array index.

where would i put that bit of code at. every time i try something like that, nothing happens or it just gets rid of the -1 and leaves the runners number there with a blank space by it.

> printf("%5.1f ", Scores[j], Scores[j]);
What's wrong with this line?

Why not printf("%5.1f %5.1f\n", Scores[i][0], Scores[i][1]); and forego the inner loop.


Can you add a test now to stop it printing?

This article has been dead for over six months. Start a new discussion instead.