0

My code below reads a file in C.It displays the file, the average score, maximum score,and the names of all the students who earned the maximum score. The exam scores(0-100 format to 1 decimal place and use a field width of columns) are stored in an array and the names(name and last name limited to 15 characters) are stored in a 2-dimensional array of characters that is parallel to the scores array. My problems are:

1) The code doesn't read(print) the file properly (I think is related to fscanf and the arrays).

2) My two functions don't print the results.

Any suggestion is appreciated, thanks.

#include "tools.h"
#define MAX 30                  // Maximum number of students
int computeMax(double stSco[], int numSt);  // Gets the average and highest
                                            // score
void outputBest(double num[], char nameHg[][15], int hgPl, int totStu);

int main()
{
    double score[MAX];
    char name[MAX][15];
    char fileName[80];
    int k, count = 0, hgCt;
    stream dataFile;
    banner();
    printf("Type the name of file you want to read\n");
    scanf("%79[^/n]", fileName);
    dataFile = fopen(fileName, "r");
    if (dataFile == NULL)
    {
        fatal("Cannot open %s for input", fileName);
    }
    while (!feof(dataFile))
    {
        fscanf(dataFile, "(%lg,%s)", &score[k], &name[k]);
        printf("%6.1f %s\n", score[k], name[k]);
        count++;                // It counts how many students there are
    }
    hgCt = computeMax(score, count);    // Stores the value sent by the
                                        // function
    outputBest(score, name, hgCt, count);
    fclose(dataFile);
    bye();
    return 0;
}

int computeMax(double stSco[], int numSt)
{
    int k, maxScore = 0, sum = 0;
    double maximum = 0, average = 0;

    for (k = 0; k < numSt; k++)
    {
        sum += stSco[k];        // It sums all scores
        if (stSco[k] > maximum)
        {
            maximum = stSco[k];
            maxScore = k;       // Stores the index of the maximum score
        }
    }
    average = sum / numSt;
    printf("The average score is %d\n", average);
    printf("The maximum score is %d\n", maximum);
    return maxScore;
}

void outputBest(double num[], char nameHg[][15], int hgPl, int totStu)
{
    int k;
    for (k = 0; k < totStu; k++)
    {
        if (num[k] = hgPl)
        {                       // It finds who has the highest score
            printf("%s got the highest score\n", nameHg[k]);
        }
    }
}
lurker
  • 56,987
  • 9
  • 69
  • 103
Cesar
  • 1
  • 1
  • 1

1 Answers1

1

First: scanf("%79[^/n]",fileName); should be scanf("%79[^\n]",fileName);, better to use fgets().

Second typo mistake: misspelled == by = in if() condition

 if(num[k]=hgPl){ //It finds who has the highest score
 //       ^ = wrong 

should be:

 if(num[k] == hgPl){ //It finds who has the highest score

Edit:

Error in while loop..

fscanf(dataFile, "(%lg,%s)", &score[k], &name[k]);
//                ^   ^  ^  remove      ^

should be:

fscanf(dataFile, "%lg%14s", &score[k], name[k]);

and increment k in while loop. after printf("%6.1f %s\n", score[k], name[k]);.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • Thanks for your response @Grijesh. I made the changes but when I run the program it prints like in an infinite loop. – Cesar Sep 20 '13 at 20:21
  • @Cesar Does it out put any thing? And does it goes in infinite loop just after you input some value? – Grijesh Chauhan Sep 20 '13 at 20:24
  • @Cesar btw what is `bye();` ? – Grijesh Chauhan Sep 20 '13 at 20:25
  • @Cesar its not in infinite loop but as I guess its struct in `fscanf()` function because you use unnecessary chars in formatting string: Read updated answer Edit part. To learn about it read: [Store data in array from input](http://stackoverflow.com/questions/17831060/store-data-in-array-from-input?lq=1) – Grijesh Chauhan Sep 20 '13 at 20:28
  • After typing the path name file, it shows long lines of unusual characters that are printed fast one after another (infinite loop). – Cesar Sep 20 '13 at 20:41
  • @ Grijesh After typing the path name file, it shows long lines of unusual characters that are printed fast one after another (infinite loop). – Cesar Sep 20 '13 at 20:50
  • I just made the change you suggested me. Now it prints the score and name(not last name) of the first student only and repeat it several times(no end). – Cesar Sep 20 '13 at 20:53
  • @Cesar that is because you don't increments `k` in while loop now again read updated answer. – Grijesh Chauhan Sep 20 '13 at 21:01
  • When I increment k, it only print the first line correctly. After that it prints a bunch of nonsense line of characters. – Cesar Sep 20 '13 at 21:21
  • @Cesar may be because of undefined behavior increase the value of MAX and `MAX = 200, double score[MAX]; char name[MAX][150];` – Grijesh Chauhan Sep 20 '13 at 21:32