首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c++中幂方法5的幂错误

c++中幂方法5的幂错误
EN

Stack Overflow用户
提问于 2017-07-03 22:16:38
回答 1查看 123关注 0票数 0

每当我将5写成n,p写成2时,我得到的输出是24...please,它让我知道出了什么问题?对于其他数字,这是完全没有问题的。

代码语言:javascript
复制
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;

int main()
{
    double n, p;
    cout << "enter the number" <<endl;
    cin >> n;
    cout << "enter the power" <<endl;
    cin >> p;
    int result = pow(n, p);
    cout << "Result is " << result;
    return 0;
}
EN

回答 1

Stack Overflow用户

发布于 2017-07-03 22:20:29

您的数据类型有问题!

代码语言:javascript
复制
double n, p; // here you use double types
std::cout << "enter the number" << std::endl;
std::cin >> n;
std::cout << "enter the power" << std::endl;
std::cin >> p;
int result = pow(n, p); // then here you use double pow(double, double) but assign it to an int
std::cout << "Result is " << std::result;

解决方案:

代码语言:javascript
复制
double result = pow(n, p);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44887872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档