Char in function is not recognized in c

Asked

Viewed 28 times

0

I am unable to make the variable char be comparable and validated, the IDE says that the variable 'a' was undeclared, some could help me please

Do a function that receives the 3 notes of a student per parameter and a letter. If a letter is A, calculates the arithmetic mean of the student’s grades, if P, their average weighted (weights: 5, 3 and 2). Use this function in the main program to return the weighted average of a student.

#include <stdio.h>
#include <math.h>

int main ()
{
    float x,y,z;
    char e;
    printf("Digite as 3 notas e uma letra: ");
    scanf ("%f %f %f %c", &x, &y, &z, &e);
    float ers (float x,float y,float z, char e);
    printf("%f %f %f %c", ers(x,y,z,e));
    return 0;
}
float ers (float x,float y,float z, char e){
    if (e==a)
        float media = (x + y + z) / 3;
    else if (e==p)
        float media = (5*x + y*3 + z*2) / 10;
    return media;
}
  • Or put the function setting before main or at least put the function signature. In this printf("%f %f %f %c", ers(x,y,z,e)); there’s something missing, maybe you wanted to printf("%f %f %f %c %f", x, y, z, e, ers(x,y,z,e));.

  • I didn’t understand, I just wanted to keep the values of the notes and the letter

  • Everything makes me understand that you want: float ers (float x,float y,float z, char e){ if (e=='a') float media = (x + y + z) / 3; else if (e=='p') float media = (5*x + y*3 + z*2) / 10; return media; }, since it did not define the variables a and p.

  • that’s right, but in the main function at first I wanted to keep the values of notes and a letter, but I can’t keep the letter in the variable char

2 answers

2

In his job ers you should test whether the received parameter and is equal to 'A' or 'P'.

My example code below is in C#, but the idea is the same: test and with 'A' or 'P' between single quotation marks, as this is the type char.

using System;

namespace study
{
    class Program
    {
        static void Main(string[] args)
        {
            char e = 'A';

            Console.Clear();
            Console.WriteLine( "Valor de e = {0}", e);

            if (e == 'A')
                Console.WriteLine("Cálculo por média aritmética!");
            else
                if (e == 'P')
                {
                    Console.WriteLine("Cálculo por média ponderada!");
                }
                else
                {
                    Console.WriteLine("Valor inválido para o tipo do cálculo!");
                }
        }
    }
}
  • 1

    thank you Carlos

1


See if this is it. I don’t understand ao to conseguindo guardar a letra na variável char.

#include <stdio.h>
float ers (float, float, float, char);

int main () {
    float x,y,z;
    char e;
    printf("Digite as 3 notas e uma letra: ");
    scanf ("%f %f %f %c", &x, &y, &z, &e);
    printf("%f %f %f %c %f", x, y, z, e, ers(x,y,z,e));
    return 0;
}

float ers (float x, float y, float z, char e) {
    float media = 0;
    if (e == 'a' || e == 'A')
        media = (x + y + z) / 3;
    else if (e == 'p' || e == 'P')
        media = (5*x + y*3 + z*2) / 10;
    return media;
}
  • anonimo Voce put the function in the global scope, could you tell me why the preference?

  • As I said before, either you put the function definition or the function signature, or prototype before da main. It’s not a question of scope. is for the compiler to resolve the references.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.