Jump to content

Functions

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Whitey

Whitey

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
#include <cstdlib>

#include <iostream>


using namespace std;


double meam(double a, double b, double c);

double maximum(double a, double b, double c);

double minimum(double a, double b, double c);


double a1;

double b1;

double c1;


int main(int argc, char *argv[])

{

    cout << "Please give 3 numbers. It will return the Mean, Maximum, Minimum, Median, and Celsius (of each number)." << endl;

    cout << "First number: ";

    cin >> a1;

    cout << "Second number: ";

    cin >> b1;

    cout << "Third number: ";

    cin >> c1;

    mean(a1,b1,c1);

    system("PAUSE");

    return EXIT_SUCCESS;

}


double meam(double a, double b, double c){

    cout << "Calling mean(" << a << ", " << b << ", " << c ")" << endl;

    double mean = (a + b + c)/2; 

    cout << "Mean returned " << mean << endl;     

}

double maximum(double a, double b, double c){

    if(a>b && a>c){

        cout << "Maximum returned " << a << endl;          

    } 

    else if(b>a && b>c){

        cout << "Maximum returned " << b << endl; 

    }  

    else{

        cout << "Maximum returned " << c << endl;     

    }

}

double min(double a, double b, double c){

    if(a<b && a<c){

        cout << "Minimum returned " << a << endl;          

    } 

    else if(b<a && b<c){

        cout << "Minimum returned " << b << endl; 

    }  

    else{

        cout << "Minimum returned " << c << endl;     

    }  

}


This is my code and i get an error

Quote

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c functions.cpp -o functions.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

functions.cpp: In function `int main(int, char**)':
functions.cpp:23: error: `mean' undeclared (first use this function)
functions.cpp:23: error: (Each undeclared identifier is reported only once for each function it appears in.)

functions.cpp: In function `double meam(double, double, double)':
functions.cpp:29: error: expected `;' before string constant

make.exe: *** [functions.o] Error 1

Execution terminated

i am in a c++ class and that is how they showed me how to do "functions" you have to do a "function prototype"

#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Note the difference between meam and mean.

Also note a missing operator:
    cout << "Calling mean(" << a << ", " << b << ", " << [COLOR="Magenta"]c ")"[/COLOR] << endl;


#3
redkid

redkid

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
Well nothing wrong really, just human errors!!

#include <cstdlib>
#include <iostream>

using namespace std;

double [COLOR="Red"]mean[/COLOR](double a, double b, double c);
double maximum(double a, double b, double c);
double minimum(double a, double b, double c);

double a1;
double b1;
double c1;

int main(int argc, char *argv[])
{
    cout << "Please give 3 numbers. It will return the Mean, Maximum, Minimum, Median, and Celsius (of each number)." << endl;
    cout << "First number: ";
    cin >> a1;
    cout << "Second number: ";
    cin >> b1;
    cout << "Third number: ";
    cin >> c1;
    mean(a1,b1,c1);
    system("PAUSE");
    return EXIT_SUCCESS;
}

double mean(double a, double b, double c){
    cout << "Calling mean(" << a << ", " << b << ", " << c [COLOR="Red"]<<[/COLOR] ")" << endl;
    double mean = (a + b + c)/2; 
    cout << "Mean returned " << mean << endl;     
}
double maximum(double a, double b, double c){
    if(a>b && a>c){
        cout << "Maximum returned " << a << endl;          
    } 
    else if(b>a && b>c){
        cout << "Maximum returned " << b << endl; 
    }  
    else{
        cout << "Maximum returned " << c << endl;     
    }
}
double min(double a, double b, double c){
    if(a<b && a<c){
        cout << "Minimum returned " << a << endl;          
    } 
    else if(b<a && b<c){
        cout << "Minimum returned " << b << endl; 
    }  
    else{
        cout << "Minimum returned " << c << endl;     
    }  
}


There are some warnings too, since you are not returning values even though you are using non-void functions.



#4
Steve.L

Steve.L

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 444 posts
functions.cpp:23: error: `mean' undeclared (first use this function)
functions.cpp:23: error: (Each undeclared identifier is reported only once for each function it appears in.)

You know, these errors are given to you for a reason, they TELL you what and where you screwed up.