c - Program keeps running even after the error appeared -


i have program retrieves numbers file array. last problem have program keeps running though cannot open file, instead of ending it. when says program over, menu main appears again doesn't show data

int main (void)  {  int choice, max, min; float avg; int test[max_number_of_students]; int file_opened; int number_of_students;  file_opened = get_test_scores(test, &number_of_students);  if (file_opened == 0) {     {     choice = menu();     switch (choice)     {     case 0: printf("\nprogram over.\n");               break;     case 1: test_avg(&avg, test, number_of_students);             printf("\naverage score on test = %5.2f\n", avg);             break;     case 2: test_max_min(number_of_students, &max, &min, test);             printf("\nmaximum score = %3d\n"                       "minimum score = %3d\n", max, min);             break;     case 3: print_test(test,number_of_students);             break;     default:             printf("this should never happen!");   }    } while (choice != 0); }  return 0;  }  int get_test_scores(int test[], int* size) { file* sp_input;    // pointer input stream (from file)   int i;  sp_input = fopen("a20.dat", "r");                                                   if (sp_input == null)      printf("\nunable open file a20.dat\n");   else   {     while( fscanf(sp_input, "%d", &test[i])!=eof)     {     i=i+1;     ++*size;     }     fclose(sp_input);    // close stream   }    return 1; } 

yes, keeps running...

if (file_opened == 0) {     { 

means if file has not been opened, loop. want:

if (file_opened != 0) 

see comments other bugs.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -