-
November 6th, 2009, 11:44 AM
#1
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;
}
-
November 6th, 2009, 12:11 PM
#2
Re: Help on a program that returns a pointer?
Originally Posted by heinrich
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
The main() function returns int and only int.
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.
-
November 6th, 2009, 12:16 PM
#3
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.
That's why you were only seeing an address -- you didn't specify that you are calling a function.
Regards,
Paul McKenzie
-
November 6th, 2009, 12:21 PM
#4
Re: Help on a program that returns a pointer?
-
November 6th, 2009, 12:23 PM
#5
Re: Help on a program that returns a pointer?
Originally Posted by heinrich
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|