初学者遇到简单的家庭作业代码(C ++ Visual Studio)

时间:2017-09-12 21:29:22

标签: c++

我无法弄清楚我的代码有什么问题!我的代码看起来与教授的副本完全相同,但由于第25行(最后E0109语句)中的错误C2064cout而无法运行。

我是否需要为productsqrt变量编写方程式,或者cmath会自动处理这个变量吗?

我的问题肯定是使用pow() / sqrt()函数。

输出应显示:

Enter the base: 3
Enter the exponent: 2
3 to the 2 power equals 9.
The square root of 3 equals 1.73.

Press any key to continue

我的完整代码:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int base;
    int exponent;
    int product;
    float sqrt;

    cout << "Enter the base: ";
    cin >> base;

    cout << "Enter the exponent: ";
    cin >> exponent;

    cout << base << " to the " << exponent << " power equals " << pow(base, exponent);
    cout << "The square root of " << base << " equals " << sqrt(base) << ".\n\n";
    return 0;

}

1 个答案:

答案 0 :(得分:6)

您命名了一个局部变量sqrt,它隐藏了sqrt()中的cmath函数。然后,当您尝试调用sqrt(base)时,编译器必须解析sqrt名称,因此它首先查找本地范围并找到sqrt变量,这不是函数,因此无法使用()运算符调用。

将变量的名称更改为squareRoot,或其他任何非sqrt的名称。

另外,你还没有真正使用那个变量,所以严格来说这不是必需的,可以删除。