So I've completed my latest assignment last night in Dev C++, which is what we are required to create it in. Got it working flawlessly, compiles great, runs great, everything is fine. However, we have to run our program through 'MobaXTerm' and through a VPN, using our University's system that they use to grade on. I tried to run it and I get an error, 'segmentation fault'. That was the first time I heard of the error so I researched it. In most cases, it's putting the wrong variable in a 'for loop' but I checked my loops 50 times, it works. I assume it has something to do with my 2D array, maybe going out of bounds, but I debugged, and from what I can tell, it doesn't escape the size that I assigned for it.
I don't exactly just want to paste my whole program in here but I'll give a few snippets and maybe someone could try to tell me what might possibly be the issue. I just don't really understand how it runs fine in one and not the other.
#include <stdio.h>
#include <stdlib.h>
#define ROWS 12
#define COLS 8
void makeArray(FILE*, int [][COLS]);
int getScore(int [][COLS], int, int);
int getMonthMax(int [][COLS], int);
int getYearMax(int [][COLS]);
float getMonthAvg(int [][COLS], int);
float getYearAvg(int [][COLS]);
int toursMissed(int [][COLS]);
void displayMenu();
int processRequest(int [][COLS], int);
void printArray(int [][COLS]);
int main(){
int scoresArray[ROWS][COLS];
int choice, constant = 0;
FILE *scoreFileptr;
scoreFileptr = fopen("scores.txt", "r");
if (scoreFileptr == NULL)
printf("The file isn't opening");
makeArray(scoreFileptr, scoresArray);
while(constant == 0){
displayMenu();
scanf("%d", &choice);
processRequest(scoresArray, choice);
}
fclose(scoreFileptr);
}
Here's a function:
void makeArray(FILE *scoreFileptr, int scoresArray[][COLS]){
int i, j, filler = 0;
for(i = 0; i < ROWS; i++){
for(j = 0; j < COLS; j++){
fscanf(scoreFileptr, "%d", &filler);
if(filler == 999){
break;
}
scoresArray[i][j] = filler;
}
}
}
Here's another function:
int processRequest(int scoresArray[][COLS], int choice){
int month = 0, tourNum = 0;
switch(choice){
case 1:
printf("Please enter the month and the game\n");
scanf("%d %d", &month, &tourNum);
printf("The score for tournament %d is %d\n\n",tourNum, getScore(scoresArray, month, tourNum));
break;
case 2:
printf("Please enter the month\n");
scanf("%d", &month);
printf("The max score in month %d is %d\n\n", month, getMonthMax(scoresArray, month));
break;
case 3:
printf("Please enter the month\n");
scanf("%d", &month);
printf("The average score for month %d is %.2f\n\n", month, getMonthAvg(scoresArray, month));
break;
case 4:
printf("The max score for the year is %d\n\n", getYearMax(scoresArray));
break;
case 5:
printf("The average score for the year is %.2f\n\n", getYearAvg(scoresArray));
break;
case 6:
printf("The number of tournaments missed for the year is %d\n\n", toursMissed(scoresArray));
break;
case 7:
printf("The scores for the year are:\n");
printArray(scoresArray);
break;
case 0: printf("Thank you! Goodbye");
exit(0);
break;
default:
printf("Please enter one of the options listed\n\n");
}
return 0;
}
Do you think it's the way I'm passing the array to the functions? That's the only way I can think of. But then why would it work in the Dev C++ compiler? I'm stumped.
Edit: The problem seems to be in the 'makeArray' function. I took out the function call for it in main and it runs now. Just got to figure out what's causing it.
Edit 2: After going through the function line by line, they problem seems to be the 'fscanf' line in the makeArray function. Thank you all for pointing me in the right direction!