pow() 返回 0 (C++)

发布于 2024-10-27 16:13:08 字数 3469 浏览 2 评论 0原文

有人可以解释为什么下面代码中的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; 
        }
    }
}

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; 
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

热血少△年 2024-11-03 16:13:08

你从来没有真正调用过pow。选择 3 时,您仅调用 opt3f,它仅返回全局变量 phiExpo,该变量初始化为 0因为它是全球性的。然后,您还需要从 phiExpof 函数返回,就像其他人已经指出的那样。

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.

つ可否回来 2024-11-03 16:13:08

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

double phiExpof(double phi, double userInput){
      return pow(phi, userInput);
}

当您没有显式返回值时,您将得到未定义的行为,在本例中为 0。

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

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.

七秒鱼° 2024-11-03 16:13:08

你怎么知道它返回0?您的代码甚至不检查返回值。

How do you know it's returning 0? Your code doesn't even check the return value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文