Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ pow()返回0(C+;+;)_C++_Return_Pow - Fatal编程技术网

C++ pow()返回0(C+;+;)

C++ pow()返回0(C+;+;),c++,return,pow,C++,Return,Pow,有人能解释一下为什么在程序运行时,下面代码中的pow()返回的是0,而不是实际的计算结果吗?我是个编程新手,完全被难倒了 谢谢你的帮助 #include <iostream> #include <math.h> #include <windows.h> using namespace std; //Prototypes: double phiExpo; double phiNegExpo; double opt1f(double phi, dou

有人能解释一下为什么在程序运行时,下面代码中的
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; 
        }
    }
}
#包括
#包括
#包括
使用名称空间std;
//原型:
上海世博会;
双菲涅格斯波;
双opt1f(双phi,双用户输入){
返回用户输入*phi;}
双opt2f(双phi,双用户输入){
返回userInput/phi;}
双opt3f(){
返回值;}
双opt4f(){
返回phiNegExpo;}
双phiExpof(双phi,双用户输入){
pow(φ,用户输入);}
双PHIEGEXPOF(双phi,双用户输入){
pow(phi,-userInput);}
//执行程序:
int main()
{
双用户输入;
int用户选择;
双φ=1.61803399;
bool-quit=false;
int用户选择2;

cout它可能没有返回0。相反,您没有返回
pow的结果:

double phiExpof(double phi, double userInput){
      return pow(phi, userInput);
}
当您没有显式返回值时,您将得到未定义的行为,在本例中为0

注:
我没有注意到其他代码…这是一个问题。另一个问题是,您实际上没有调用phiExpof。相反,您返回的是一个全局变量
phiExpo

它可能没有返回0。相反,您没有返回
pow
的结果:

double phiExpof(double phi, double userInput){
      return pow(phi, userInput);
}
当您没有显式返回值时,您将得到未定义的行为,在本例中为0

注:
我没有注意到另一个代码…这是一个问题。另一个问题是您实际上没有调用phiExpof。而是返回一个全局变量
phiExpo

您如何知道它返回
0
?您的代码甚至没有检查返回值。

您如何知道它返回
0
?您的code甚至不检查返回值。

您永远不会实际调用
pow
。在选择
3
时,您只调用
opt3f
,它只返回全局变量
phiExpo
,该变量初始化为
0
,因为它是全局变量。然后您还需要像其他函数一样从
phiExpof
函数返回它已经指出。

您实际上永远不会调用
pow
。在选择
3
时,您只调用
opt3f
,它只返回全局变量
phiExpo
,该变量初始化为
0
,因为它是全局变量。然后您还需要像其他人已经指出的那样,从
phiExpof
函数返回这看起来像是家庭作业。如果是,请把它贴上。这不是我教自己C++的一种爱好,这个程序是我决定写的。很高兴看到我走的方向正确,(我希望)一个学生的练习——甚至是自学的——是在标签的意义上的作业。这也不是问题:我们对作业很满意。这看起来像是家庭作业。如果是,请把它贴上。这不是——我教C++作为一种爱好,这个程序是我决定写的。很高兴看到我走的方向是正确的。(我希望!)对于学生来说,一个练习——甚至是自己指定的——是标签意义上的家庭作业。这也不是问题:我们对家庭作业没问题。你的意思是:当你没有明确返回时,这是未定义的行为,任何事情都可能发生。你的意思是:当你没有明确返回时,这是未定义的行为,任何事情都可能发生。