造成这种"无效类型参数的原因是' *'"?

时间:2016-01-23 19:46:54

标签: c++ calculator exponential

好的,所以我已经研究了我的小脑可以接受的内容。我还没有找到一个可以解决问题的答案。试图编写一个计算器,将基数提高到不高于 214783647 的数字。到目前为止,我正试图让程序在没有设定值的情况下运行。但这就是调试器所说的:

main.cpp: In function 'int main()':
main.cpp:25:11: error: invalid type argument of unary '*' (have 'int')
 result = *x;

这是代码。

#include <iostream>
using namespace std;

int solve(int, int, char);

int main() 
{

    int x = 0;
    int y = 0;
    int result = 1;
        cout << "Enter your base:";
        cin >> x;
        cout << "Enter the number to raise base by:";
        cin >> y;

for (x = 0; result <= y; result++)
{
result = *x;
}
        cout << result;

return 0;

}

我现在处于C ++的最初阶段,但我可以接受所有建设性的批评。

固定!! 我不确定这是否允许,但我终于让该程序完全正常运行,感谢y&#39;所有!

1 个答案:

答案 0 :(得分:2)

这里的问题是你试图去除一个不是指针的变量。只需从*移除result = *x;即可将x的值指定为result

但是我不相信你会得到预期的效果。因为您在循环中使用x并将其初始化为0,所以无论用户输入的是什么值,您都会感到沮丧。

我相信您正在尝试执行以下操作

int x = 1;
int y = 1;

std::cout << "Enter your base:";
std::cin >> x;
std::cout << "Enter the number to raise base by:";
std::cin >> y;

int result = x;

for(int i = 1; i < y; i++ ) { //loop y times 
  result = result * x;        //exponents just times itself y times
}

std::cout << result << endl;

值得一提的是,使用std::命名空间使用标准库中的内容时,即使编译器不需要它,也是很好的形式。除了使用结束endl完成任何输出之外,您的终端提示将在输出后立即开始。希望有所帮助。