pow()返回0(C ++)

有人可以解释为什么在下面的代码中
pow()
在程序运行时返回
0
而不是实际计算?我是编程的新手,我完全被难倒了。 谢谢你的帮助。
#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; 
        }
    }
}
    
已邀请:
你永远不会致电
pow
。在选择
3
时,你只调用
opt3f
,它只返回全局变量
phiExpo
,它被初始化为
0
,因为它是全局的。然后你还需要从
phiExpof
函数返回,就像其他人已经指出的那样。     
它可能不会返回0.相反,您没有返回
pow
的结果:
double phiExpof(double phi, double userInput){
      return pow(phi, userInput);
}
如果不显式返回值,则会得到未定义的行为,在本例中为0。 注意: 我没注意到其他代码......这是一个问题。另一个是你实际上并没有打电话给phiExpof。相反,你要返回
phiExpo
这是一个全局变量。     
你怎么知道它回来了
0
?您的代码甚至不检查返回值。     

要回复问题请先登录注册