簡體   English   中英

pow()返回0(C ++)

[英]pow() returning 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 您的代碼甚至不檢查返回值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM