0

Ok, so I have researched as much as my tiny brain can take in. I have yet to find an answer that helps my problem. Trying to write a calculator that raises the base to a number no higher than 214783647. And as of now I'm jut trying to get the program to function without set values. But this is what the debugger says:

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

Here is the code.

#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;

}

I am at the very beginning stages of C++, but I can take all constructive criticism.

FIXED!! I'm not sure if this allowed, but I finally have the program fully functioning thanks to y'all!

Matt331
  • 9
  • 1
  • 5
  • What is this line `result = *x;` supposed to do? Are you aware that your `x` will always be zero? – pfnuesel Jan 23 '16 at 19:48
  • The unary operator `*` is the pointer dereference operator, so what you're trying to do is to dereference `x` as if it was a pointer, which it isn't. – Some programmer dude Jan 23 '16 at 19:51
  • And that is because I have the 0 right? @pfnuesel – Matt331 Jan 23 '16 at 19:53
  • @Matt331 `for (x = 0;` is just wrong since you need `x` to still be the user's entered base, but that's unrelated to the compiler error you're getting. –  Jan 23 '16 at 19:54
  • That said, attempting to use the same `result` variable both for the multiplication and keeping track of how many multiplications you've performed is not going to go well either. –  Jan 23 '16 at 19:55
  • @hvd The compiler error is gone. And now I'm even more lost than before. – Matt331 Jan 23 '16 at 19:59

1 Answers1

2

The problem here is that your are trying to derefernece a variable that is not a pointer. Simply remove the * from result = *x; to assign the value of x to result.

However I don't believe you will get the desired effect. Because you are using x inside your loop and initializing it back to 0, you are clobbering whatever value your user has input.

I believe you are trying to do the following

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;

It's also worth mentioning that when using something from the standard library using the std:: namespace is good form, even if your compiler doesn't require it. As well as finishing any output with an endline endl, otherwise your terminal prompt will start right after the output. Hope that helps.

8bitAlex
  • 66
  • 10
  • `endl` is not always the right thing: http://stackoverflow.com/questions/213907/c-stdendl-vs-n – Alan Stokes Jan 23 '16 at 20:15
  • @AlanStokes I don't think at this level the flush is a problem needed to be addressed. – bolov Jan 23 '16 at 20:25
  • @AlanStokes Fair point but I think in this case it is desired. – 8bitAlex Jan 23 '16 at 20:25
  • That makes so much sense. Before I added your edits it would just ask for base and power and not show anything after that. Thank you so much for clearing that up. Learned more from this than my textbook. – Matt331 Jan 23 '16 at 20:26
  • I edited it a little bit so the process is a little more obvious. – 8bitAlex Jan 24 '16 at 03:56