LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2005, 04:59 AM   #1
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Rep: Reputation: 15
C++ Function Help


Hi again,

I have some more problems with a function that I made... it isn't working properly. Well anyway it's pretty obvious what it is supose to do so I won't mention it.

The problem is that it keeps returning the wrong answer . I will copy and paste it into a [/CODE] box so you's can try and run it in your own IDE and hopefully tell me what is wrong in it, thanks for your help.

Code:
#include <iostream.h>
#include <stdlib.h>

    float  areaOfTriangle(float, float);

int main()
{
    float perpindicularHeight = 0.00f;
    float baseOfT = 0.00f;
    float areaOf_T_Answer  = 0.00f;

     cout<<"Please enter the base of the triangle: ";
     cin>>baseOfT;

     cout<<"Please enter the perpindicular height of the triangle: ";
     cin>>perpindicularHeight;
     cout<<"The answer is "<<areaOf_T_Answer<<" cm2";
     cin.get();
     cin.get();
     cin.get();
     cin.get();
     cin.get();
     cin.get();
     cin.get();
     cin.get();




     areaOf_T_Answer = areaOfTriangle(perpindicularHeight, baseOfT);
      return 0;
}

float areaOfTriangle(float perpindicularHeight, float baseOfT)
{


return    (baseOfT * perpindicularHeight) / 2;


}
Thanks
 
Old 08-17-2005, 05:21 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Why are you trying to print out the value in areaOf_T_Answer before calling the function? You should have something like

Code:
areaOf_T_Answer = areaOfTriangle(perpindicularHeight, baseOfT);

cout << areaOf_T_Answer;
If you do it in the order as in your code, you'll get whatever value is stored in areaOf_T_Answer already (the one you've assigned). The lines are executed in order!

Last edited by Nylex; 08-17-2005 at 05:24 AM.
 
Old 08-18-2005, 06:06 AM   #3
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Lol, because I'm a n008. Thanks for you help though... it's fixed now
 
Old 08-18-2005, 07:40 AM   #4
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Sorry for double posting, I didn't want to start a new thread.

I have been trying to learn how to overload function and the point of overloading them. Here is a incomplete program I wrote, it contains overloaded functions. I would like to know how would I return the value when using the overloaded functions (that's why I left the function definition blank).

If you could fill it in or something and re-post it with a bit of explanation on to what you changed/added and why it would be a great help.

Thank You.


Code:
#include <iostream>

using namespace std;


int myOverloadedFunction(int, int);
float myOverloadedFunction(float, float);
double myOverloadedFunction(double, double);

int main()


{   //This is the interger section//
// This section has every thing to do with the integer part of my program
// each of the different variables have two for the multiplying action. //

           int multInt = 0;
           int multInt2 = 0;
           int intAnswer = 0;

        cout<<"Please enter a integer number to be multiplied: ";
        cin>>multInt;
        cout<<"\nPlease enter a second integer number to be multiplied by the first integer entered: ";
        cin>>multInt2;
        intAnswer = myOverloadedFunction(multInt, multInt2);

           //This section contains the floating point part of my program.
           float multFloat = 0;
           float multFloat2 = 0;
           float floatAnswer = 0;

        cout<<"\nPlease enter a floating point number to be multiplied: ";
        cin>>multFloat;
        cout<<"\nPlease enter a second floating point number to be multiplied by the first\n entered: ";
        cin>>multFloat2;
        floatAnswer = myOverloadedFunction(multFloat, multFloat2);

        //This section contains the double part of my program.
        double doubleAnswer = 0;
        double multDouble = 0;
        double multDouble2 = 0;

        cout<<"\nPlease enter a double number to be multiplied: ";
        cin>>multDouble;
        cout<<"Please enter a second double number to be multiplied: ";
        cin>>multDouble2;
        doubleAnswer  = myOverloadedFunction(multDouble, multDouble2);

        //This next part should display my the return result of my overloaded functions.
        cout<<"Two integer numbers multiplied equal: "<<intAnswer;
        cout<<"Two floating point numbers multiplied equal: "<<floatAnswer;
        cout<<"Two double numbers multiplied equal: "<<doubleAnswer;



        cin.get();
        cin.get();
        cin.get();






      return 0;
}

Last edited by InvisibleSniper; 08-18-2005 at 07:44 AM.
 
Old 08-18-2005, 08:10 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You just write the implementations as you would for a function that isn't overloaded. The compiler knows which one to call based on the parameter list. So yeah, just do something like this

Code:
int main()
{
     // Code that calls your overloaded functions
}

int myOverloadedFunction(int x, int y)
{
     return x*y;
}

float myOverloadedFunction(float x, float y)
{
     return x*y;
}

double myOverloadedFunction(double x, double y)
{
     return x*y;
}
 
Old 08-18-2005, 08:45 AM   #6
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Thank you, problem solved once again. Damn this is a really good forum
 
Old 08-18-2005, 09:12 PM   #7
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
Once again sorry for double posting but I didn't want to open a new thread.

Anyway the reason for this post is because I would like to know what does the authour of "SAMS Teach Yourself C++ in 24 Hours" mean when he says:

Code:
long myFunction (int x = 50);

This prototype says "myFunction() returns a long and takes an integer parameter. If an argument is not supplied, use the default value of 50."
I tried to use the defualt parameters in a program but it didn't work. Can you tell me how I would use them, because I don't have a clue. If you could just write a small noobish sample of default parameters in use I will then understand it, thanks.
 
Old 08-18-2005, 09:36 PM   #8
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Code:
/* simple.cpp */
#include <iostream>
using std::cout;

void PrintSize(int size = 50)
{
    cout << size << '\n';
}

int main()
{
    PrintSize();     /* prints 50 because of default param */
    PrintSize(129);  /* prints 129 */
    PrintSize(0);    /* prints 0  */

    return 0;
}
Code:
[shell]$ g++ -o simple simple.cpp
[shell]$ ./simple
50
129
0

I use these alot for constructors, when I want to force a constructor to take an argument, even if the default constructor is to be called. I believe creating a class like this will force the constructor we specify to be the default constructor which is important at times.

Code:
class Simple
{
public:
    Simple(const char * filename = "default.txt");

private:
    char * filename;
};

Last edited by lowpro2k3; 08-18-2005 at 09:38 PM.
 
Old 08-18-2005, 11:22 PM   #9
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
The book I'm using is also by Jesse Liberty and David Horvath, but it's "Teach Yourself C++ for Linux in 21 days" .
 
Old 08-19-2005, 12:23 AM   #10
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
I just changed it a bit and I'm wondering why it goes back to 50 right after you initialized it to 129. Any way here is what I mean:

Code:
/* simple.cpp */
#include <iostream>
using std::cout;

void PrintSize(int size = 50)
{
    cout << size << '\n';
    cin.get();
}

int main()
{

    PrintSize();     /* prints 50 because of default param */
    PrintSize(129);  /* prints 129 */
    PrintSize();       /*Goes back to 50 on this line... how come?*/
    PrintSize(0);    /* prints 0  */
    cin.get();

    return 0;
}
Thanks for your help
 
Old 08-19-2005, 12:26 AM   #11
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Your third line in main() is taking no arguments, so the default parameter is used as in the first line. You aren't initialising anything in the second line, just passing the value 129 to that function. The next line is simply another call to the function, where you're using the default parameter.
 
Old 08-19-2005, 12:45 AM   #12
InvisibleSniper
Member
 
Registered: Jul 2005
Location: Australia
Distribution: Debian
Posts: 113

Original Poster
Rep: Reputation: 15
So after the 129 is passed in... what happens to it?

Is it still possible to use it after, I mean is it still in there?

Like if I was to put 129 in a variable and print the variable I would get 129. But if I put 129 in a function and print the function I get 50?
 
Old 08-19-2005, 12:56 AM   #13
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
If pass the value 129 to the function, the function prints out 129. Your function header is

Code:
void PrintSize(int size = 50)
which says that if you don't pass a value, to set size = 50. You can't use that 129 after the function has finished executing, because size has gone out of scope. You understand this:

Code:
PrintSize(); // Prints the no. 50
PrintSize(129); // Prints the no. 129
If you then do

Code:
PrintSize(129);
PrintSize();
that will print 129 and then 50. It's just the previous lines of code in reverse order. Each call you make to your function has nothing to do with the last one.
 
Old 08-19-2005, 12:36 PM   #14
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Nylex is right. Here is one more example. This function takes a C++ string as a parameter. You can pass it any string you like and the function will print the string to the console. However if you don't pass a string as an argument, the default parameter declared in the function header will be printed instead.

Code:
#include <iostream>
#include <string>
using namespace std;

void PrintString(string parameter = "I am the default parameter")
{
    cout << parameter << '\n';
}

int main()
{
    PrintString("This string will be printed to the screen");
    PrintString();
    PrintString("foo bar");
}
Running that code should print:

This string will be printed to the screen
I am the defalt parameter
foo bar


I haven't compiled it but I think it should work.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 03:15 PM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Is the wait function is the same as the sleep function ? Linh Programming 3 04-28-2004 12:39 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration