Help on a program that returns a pointer?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Help on a program that returns a pointer?

  1. #1
    Join Date
    Oct 2009
    Posts
    4

    Help on a program that returns a pointer?

    I've made a program that was supposed to just return a value for the function selected while using x and y which are global variables. All that it outputs is the location of the data in the memory and not the actual value. I was thinking maybe I could just leave it and place a reverse pointer in there somewhere but I'm not sure how to do that... Any tips?

    #include <iostream>

    using namespace std;

    float x = 0.0;
    float y = 0.0;

    float function1(void);
    float function2(void);
    float function3(void);

    float main(void)
    {
    int choice = 0;

    cout << "Please enter a value for x" << endl;
    cin >> x ;
    cout << "Please enter a value for y" << endl;
    cin >> y ;

    do
    {
    cout << "which function would you like to compute? 1, 2, or 3? press 0 to exit." << endl;
    cin >> choice;

    while ((choice < 0) || (choice > 3))
    {
    cout << " you have entered an invalid choice... please try again." << endl ;

    cout << "which function would you like to compute? 1, 2, or 3? press 0 to exit." << endl;
    cin >> choice;

    }
    if (choice == 1)
    {

    cout << " the value of function one is " << function1 << endl;
    }
    if (choice == 2 )
    {
    cout << " the value of function 2 is " << function2 << endl;
    }
    if (choice == 3 )
    {
    cout << " the value of function 3 is " << function3 << endl;
    }

    }while ((choice >= 1)&&(choice <=3));

    cout << " goodbye"<< endl;

    return 0;

    }

    float function1(void)
    {
    float F= 0.0;
    F = 2 * (x * x * x * x) + 9 * (1/(y * y));
    return F;
    }

    float function2(void)
    {
    float F = 6 * x + 5 * (y*y*y);
    return F;
    }

    float function3(void)
    {
    float F = ( x * x * x ) + (1/( y * y * y ));
    return F;
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Help on a program that returns a pointer?

    Quote Originally Posted by heinrich View Post
    I've made a program that was supposed to just return a value for the function selected while using x and y which are global variables.
    First, please use code tags when posting code. The code you posted is practically unreadable.

    Second, this is illegal
    Code:
    float main(void)
    The main() function returns int and only int.
    Code:
    int main()
    Third, to call a function, you must provide arguments within parentheses. If there are no arguments, then you just specify parentheses only. You did not do that at all (look at your cout statememts).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 6th, 2009 at 12:14 PM.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Help on a program that returns a pointer?

    In short, a function name by itself is a pointer. To call a function, you need to use parentheses after the function name.
    Code:
    function3();
    That's why you were only seeing an address -- you didn't specify that you are calling a function.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Oct 2009
    Posts
    4

    Re: Help on a program that returns a pointer?

    What are code tags?

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Help on a program that returns a pointer?

    Quote Originally Posted by heinrich View Post
    What are code tags?
    click

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center