Ishkyzone Ishkyzone - 2 months ago 11
C Question

"variable" was not declared on this scope [C]

I have these 3 errors when compiling main:


[Error]"media" was not declared on this scope

[Error]"max" was not declared on this scope

[Error]"min" was not declared on this scope

[Error]"calc" was not declared on this scope


Do I need to scan inside the main and then use the function?

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

typedef struct{
char name[50];
float medias;
}DATA;

DATA *p;


float calc(DATA *p,float media, float max, float min)
{
int a;
max=0;
min=20;
float soma=0;
for(a=0; a<3; a++)
{
printf("\nWrite the score of your %dº test: ", a+1);
scanf("%f", p->medias);
if (p->medias>max){
max=p->medias;
}
if (p->medias<min){
min=p->medias;
}
soma=soma+p->medias;
}
media=soma/3;
return p, media, max, min;
}

int main()
{
calc(p, media, max, min);
system("cls");
printf("Higher score: %f \nLowest score: %f \nMedia: %f", max, min, media);
getch();
}

vz0 vz0
Answer

You have't declared variables media, max, min. They either need to be local in main, or global. In general it is a good idea to have them as local in main, including p which you have put as global but then pass as parameter.

In your program, media, max, and min are parameters in calc. As they are, those variables can only be used inside calc.

Do I need to scan inside the main and then use the function?

If you mean scanf() to read the values from keyboard input, probably yes. It really depends on what you need.

However, while you surely need to declare the variables first, you are also overwriting the value of the parameters inside calc, ignoring whatever values are passed to the function.

float calc(DATA *p,float media, float max, float min)
{
    int a;
    max=0;
    min=20;
Comments