Reputation: 5
Could someone explain why pow()
in the following code is returning a 0
when the program is run, rather than the actual calculation? I'm a newbie to programming and I'm entirely stumped.
Thanks for any help.
#include <iostream>
#include <math.h>
#include <windows.h>
using namespace std;
//Prototypes:
double phiExpo;
double phiNegExpo;
double opt1f(double phi, double userInput){
return userInput * phi;}
double opt2f(double phi, double userInput){
return userInput / phi;}
double opt3f(){
return phiExpo;}
double opt4f(){
return phiNegExpo;}
double phiExpof(double phi, double userInput){
pow(phi, userInput);}
double phiNegExpof(double phi, double userInput){
pow(phi,-userInput);}
//Execute program:
int main()
{
double userInput;
int userChoice;
double phi = 1.61803399;
bool quit = false;
int userChoice2;
cout << "I want to (press corresponding number, then enter):" << endl;
cout << endl;
startchoices:
cout << "1. Multiply by phi:" << endl;
cout << "2. Divide by phi:" << endl;
cout << "3. Exponentiate phi:" << endl;
cout << "4. Negatively exponentiate phi:" << endl;
cout << "5. Quit." << endl;
cout << endl;
cin >> userChoice;
cout << endl;
do {
switch (userChoice){
case 1:
cout << "Enter number for multiplication: ";
cin >> userInput;
cout << endl;
cout << "Phi multiplied by " << userInput << ": ";
cout << opt1f(phi, userInput) << endl;
cout << endl;
Sleep(2000);
cout << "1. Continue." << endl;
cout << "2. Return to menu." << endl;
cout << endl;
cin >> userChoice2;
cout << endl;
if(userChoice2 > 1){
goto startchoices;}
break;
case 2:
cout << "Enter number for division: ";
cin >> userInput;
cout << endl;
cout << "Phi divided by " << userInput << ": ";
cout << opt2f(phi, userInput);
cout << endl;
Sleep(2000);
cout << "1. Continue." << endl;
cout << "2. Return to menu." << endl;
cout << endl;
cin >> userChoice2;
cout << endl;
if(userChoice2 > 1){goto startchoices;}
break;
case 3:
cout << "Enter number to exponentiate phi by: ";
cin >> userInput;
cout << endl;
cout << "Phi to the power of " << userInput << ": ";
cout << opt3f();
cout << endl;
Sleep(2000);
cout<<endl;
cout << "1. Continue." << endl;
cout << "2. Return to menu." << endl;
cout << endl;
cin >> userChoice2;
cout << endl;
if(userChoice2 > 1){goto startchoices;}
break;
}
}
}
Upvotes: 0
Views: 3195
Reputation: 30099
It is probably not returning 0. Instead, you are not returning the result of pow
:
double phiExpof(double phi, double userInput){
return pow(phi, userInput);
}
When you don't explicitly return a value, you will get undefined behavior, in this case 0.
Note:
I didn't notice the other code... This is one problem. The other is that you aren't actually calling phiExpof. Instead you are returning phiExpo
which is a global variable.
Upvotes: 1
Reputation: 131779
You never actuall call pow
. On choice 3
, you only call opt3f
, which only returns the global variable phiExpo
, which is initialized to 0
because it's global. Then you also need to return from the phiExpof
function, like others already pointed out.
Upvotes: 5
Reputation: 224834
How do you know it's returning 0
? Your code doesn't even check the return value.
Upvotes: 0