Hi guys, I've got a really strange problem with my program that I have written for an assignment that is due today. Below is a sample program that I have written that demonstrates the weird thing that keeps happening to me:*/
Code:
#include <stdio.h>
int main()
{
char fileName[30];
char location[100][30];
double depth[100];
FILE *filePtr;
int i, j;
printf("Enter name of file to load: ");
fgets(fileName, sizeof(fileName),stdin);
strtok(fileName, "\n");
printf("\n");
filePtr = fopen(fileName, "r");
if(filePtr == NULL)
return 0;
else
{
for(i = 0; i < 100; i++)
{
if(feof(filePtr))
{
break;
}
fgets (location[i],sizeof(location[i]),filePtr);
strtok(location[i], "\n");
fscanf(filePtr,"%lf", &depth[i]);
}
}
fclose(filePtr);
for(j=0; j<i;j = j+2)
{
printf("%s\n%lf\n", location[j], depth[j]);
}
return 0;
}
/*The Program runs perfectly how it should 'like this', What I cant understand is why I need to increment j by 2?
because for some reason the for loop that reads the data from a file only assignes the correct values of depth and location to every second array element??? Am I missing something, I am really stumpped by this would appreciate the help.
btw an example of a file is
Name:
eldorado.txt
Contents of file:
Eldorado 01
22.32
Eldorado 02
67.93
ARRRRRRRGGGGGGGG it doesnt make sense!!!!!!!!!!!!!!!