gpt4 book ai didi

c++ - pow() 返回 0 (C++)

转载 作者:太空宇宙 更新时间:2023-11-04 15:52:39 26 4
gpt4 key购买 nike

有人可以解释为什么下面代码中的 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,它只返回初始化为 0 的全局变量 phiExpo因为它是全局性的。然后你还需要从 phiExpof 函数返回,就像其他人已经指出的那样。

关于c++ - pow() 返回 0 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5466627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com