I can't seem to get my program to read in my .txt file correctly and apply the loops to sort the information. I'm not just getting wrong answers back, they're all error returns. Can anyone tell what's going on wrong here?
Code:
#include <stdio.h>
int main(){
// Initialize variables
char fileName [20];
int SIZE, bestRatingID, bestCostID, bestBatteryID, i;
double bestCost, bestRating, bestBattery;
// User prompt
printf ("What is the name of the file?\n");
scanf ("%s", &fileName);
// Initialize file pointer
FILE *ifp = fopen (fileName, "r");
// Search for file length
fscanf (ifp, "%d", &SIZE);
// Initialize variables that rely on SIZE
int manufacturingID [SIZE];
double efficiencyRating [SIZE], cost [SIZE], batteryLife [SIZE];
// Initialize for loop to scan whole document
for (i = 0; i < SIZE; i++){
fscanf (ifp, "%d %f %f %f", &manufacturingID [i], &cost [i], &efficiencyRating [i], &batteryLife [i]);
//For loop to determine best values
for (i = 0; i < SIZE; i++){
// if statement for best cost assignment
bestCost = cost [0];
if (cost [i] < bestCost){
bestCost = cost [i];
bestCostID = manufacturingID [i];
}
// if statement for best efficiency rating
bestRating = efficiencyRating [0];
if (efficiencyRating [i] > bestRating){
bestRating = efficiencyRating [i];
bestRatingID = manufacturingID [i];
}
// if statement for best battery life
bestBattery = batteryLife [0];
if (batteryLife [i] > bestBattery){
bestBattery = batteryLife [i];
bestBatteryID = manufacturingID [i];
}
}
// Final user output
printf ("\nAttribute \t Best Option \t Best Value \n");
printf ("Cost \t\t %d \t \t %f \n", bestCostID, bestCost);
printf ("Efficiency \t %d \t \t %f \n", bestRatingID, bestRating);
printf ("Battery \t %d \t \t %f \n", bestBatteryID, bestBattery);
// if statement for when a robot is best in more than one category
if (bestCostID == bestRatingID || bestCostID == bestBatteryID || bestBatteryID == bestRatingID)
printf("\nRobot %d has the best rating in 2 or more attributes!\n", manufacturingID [i]);
else
printf("\n");
// Close file pointer
fclose (ifp);
return 0;
}