1
I’m doing a program where it calculates arithmetic, harmonic and podenrada media using a function called note in C++. At the time I compile the media result only returns 1.
#include<iostream>
#include<cstdlib>
using namespace std;
float nota( float n1, float n2, float n3, char a, char p, char h){
float m;
cout<<"Escolha uma media: ";
cin>>m;
if(m == a){
a = n1+n2+n3/3;
}
if (m==p){
p=n1*5+n2*3+n3*2/3;
}
if (m==h){
h=1/n1+1/n2+1/n3 /3;
}
return m;
}
char escolha;
int main(){
cout<<"\t ++++++ MEDIA ++++++\n";
cout<<"\t ======================================== \n";
cout<<"\t |1=Aritemtica 2=Ponderada 3=Harmonica| \n";
cout<<"\t ======================================== \n";
float n1,n2,n3;
cout<<"Nota do aluno:";
cout<<"\n\n";
nota;
cout<<"Nota 1: ";
cin>>n1;
nota;
cout<<"Nota 2: ";
cin>>n2;
nota;
cout<<"Nota 3: ";
cin>>n3;
nota;
cout<<"Ecolha uma media: ";
cin>>escolha;
cout<<"Media: "<<nota;
cout<<"\n\n";
system("pause");
}
The code even compiles ? In the function call you pass the arguments ?
– italojd32
Yes, compile and return zero does not compute the values of the variables
– João Victor
Note that in your
if
you’re comparingfloat
withchar
. Study operator precedence, e.g. the arithmetic mean is calculated by summing the 3 values and dividing the result by 3:a = (n1+n2+n3)/3;
and not adding up the 2 values with 1/3 of the third (a = n1+n2+n3/3;
as you did. Idem too medium. This commandnota;
has no sense, maybe you intended to call a function?– anonimo