SZise
SZise

Reputation: 13

What is causing the error “expected ‘;’, ‘,’ or ‘)’ before ‘.’ token”?

I'm currently working to make an indexer for the students score, but, since I am new in C, I dont get the grasp of reading errors. Here's the error I have:

  1. c:11:27: error: expected ‘;’, ‘,’ or ‘)’ before ‘.’ token 11 | double average(float input.score, int input.many){
  2. c:34:13: warning: comparison between pointer and integer 34 | for(i=0; i < nas->many; i++){
  3. c:48:3: warning: implicit declaration of function ‘average’ [-Wimplicit-function-declaration] 48 | average(sum, nas->many);
  4. The while loop won't work properly

This is the code I work on:

#include <stdio.h>
#include <string.h>

#define MAX 100

typedef struct{
    float score, avg, sum, *many;
    char name[100][100];
} input;    
    
double average(float input.score, int input.many){
    float sum=0, average, a;
    int i;
        
    for(i=0; i<input.many;i++){
        sum += input[i].score;
    }
    a = sum/input.many;
    average=a;
    
    return average;
}

int main(){
        
    input nas[MAX];
    
    float sum;
    int choose, i, aa;
    
    printf("How many students do you want to input?\n");
    scanf(" %d",&nas->many);
    
    for(i=0; i < nas->many; i++){
        input nas[i];
    
        printf("\nName of Student-%d\t\t: ",i+1);
        scanf(" %[^\n]s", nas[i].name);
        printf("The score of Student-%d\t\t: ",i+1);
        scanf(" %f", &nas[i].score);
    
        while(nas[i].score > 100 && nas[i].score < 0){
            printf("Invalid! Please re-input the score\t\t: ");
            scanf(" %f",&nas[i].score);
        }
        average(sum, nas->many);
    }
    
    printf("1--> Average of the scores");
    scanf("%d", &choose);   
    
    if(choose == 1){
        printf("The average of %d students is %f", nas->many, average());
    }
    ...
    else{ return 0;}

Can anybody help me to understand it? Thank you very much

Upvotes: 1

Views: 4189

Answers (3)

user3629249
user3629249

Reputation: 16540

regarding:

scanf("%[^\n]s", nas[i].name);

are you expecting the user entered field name to be followed by a '\n' then a s?

Probably not.

Also, this statement does not remove the '\n' from stdin.

Therefore, IF anything, that s should be replaced with a space (so any trailing 'white space' is consumed. However, that is 'always' a bad idea.) Strongly suggest using:

scanf("%[^\n]", nas[i].name);  // note: no trailing `s`

note the next call to scanf() with a %f specifier will consume any leading 'white space'.

regarding:

scanf(" %d",&nas->many);

This may result in the first element in the array nas[] field many being set, but a much better method is:

scanf(" %d",&nas[0]->many);

regarding: right after the for() statement:

input nas[i]; 

This is declaring/re-declaring a new array with however many elements the i variable indicates. Probably not what you want to do.

regarding:

char name[100][100];

This is declaring a 2 dimensional array of 100 entries, with each entry being 100 characters. However, when ever the posted code references this 2d array, all that is given is name. The result will be all names will overlay each other in the first [0] entry

regarding:

scanf("%[^\n]s", nas[i].name);

besides the previously mentioned problem with the format string, and with using name without any indexing, the specifier: %[\n] does not limit the length of a name. Suggest:

scanf("%99[^\n]", nas[i].name[i]);

There are LOTS of other problems in the posted code besides those mentioned above.

Upvotes: 0

Gon&#231;alo Bastos
Gon&#231;alo Bastos

Reputation: 421

So, you have some variables on struct thats do not make any sens, and you have some errors by send the parameters to the function and calling the function! You want the average of N students so you only can call the function after the insertion of score!

Other thing is why you need to save on every student, the sum of scores of all students, as the average of all students! And you are declaring the function always on for so when you insert for 1 student when loops back you lost your data because you declares it again!

#include <stdio.h>
#include <string.h>


#define MAX 100

typedef struct{
    float score;
    char name[100];
} input;    
    
float average(input nas[], int many){
    float sum=0, a;
    int i;
        
    for(i=0; i< many;i++){
        sum += nas[i].score;
    }
    a = sum / many;
    
    return a;
}

int main(){
        
    input nas[MAX];
    
    float sum;
    int choose, i, many;
    
    printf("How many students do you want to input: ");
    scanf(" %d", &many);
    
    for(i=0; i < many; i++){
    
        printf("\nName of Student (%d): ",i+1);
        scanf("%[^\n]s", nas[i].name);
        printf("The score of Student (%d): ",i+1);
        scanf("%f", &nas[i].score);
    
        while(nas[i].score > 100 && nas[i].score < 0){
            printf("Invalid! Please re-input the score: ");
            scanf("%f", &nas[i].score);
        }
    }    

    sum = average(nas, many);
    
    printf("\n1--> Average of the scores: ");
    scanf("%d", &choose);   
    
    if(choose == 1){
        for(int j=0; j < many; j++){
            printf("\nName of Student: %s | Score: %f", nas[j].name, nas[j].score);

        }
        printf("\n\nThe average of %d students is %f\n", many, sum);
    }
    else{ 
        return 0;
    }
}

Upvotes: 0

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

Because you mustn’t use . inside a C identifier. It’s not valid. You could for instance replace it with _.

The only valid characters in a C identifier are letters, digits and underscore (ignoring Unicode).

Upvotes: 2

Related Questions