"undeclared identifier"

Hi, Ive recently been trying to pick up C++ (first computing language, however Ive been around programming/mathematics all my life) and I was reading a guide, and tasked to make a recursive function which calls itself in order to raise a base by an exponent, returning the power. Heres what I did (I know this isnt recursive, bear with me)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

long signed int power(short signed int, short signed int);

int main()
{
	short signed int base;
	short signed int exponent;
	long signed int powerVal; 

	cout << "What is the base?";
	cin >> base;
	cout << "\nAnd what is the exponent?";
	cin >> exponent;

	powerVal = power(base, exponent);

	cout << "" << base << "raised to the" << exponent << "is equal to" << power;

	power(base, exponent);
	{
		while(exponent)
			base = base * base;
			exponent--;
			if(!exponent)
				return(base);
	}
}


Now, I am given the following errors,

.\main.cpp(11) : error C2065: 'cout' : undeclared identifier
.\main.cpp(12) : error C2065: 'cin' : undeclared identifier
.\main.cpp(13) : error C2065: 'cout' : undeclared identifier
.\main.cpp(14) : error C2065: 'cin' : undeclared identifier
.\main.cpp(18) : error C2065: 'cout' : undeclared identifier

Sorry for not having the line numbers shown in the code, however you should be able to see where these errors are. What is going wrong here? Also, are there any tips you have for my code to be in general, more tidy and/or efficient? Also, would this not be more memory efficient than a recursive function?

Thanks for the help!
It's std::cout and std::cin.
If you put in the line:

using namespace std;

at the top of the file, then you won't have to qualify cin and cout with the std namespace every time (i.e. you should just be able to include this line and your errors should go away).

I'm a little concerned about what's happening on line 20 though. Is that the definition for your power function? It should be OUTSIDE the main method. You can put the prototype for the function at the top of the file, and implement it below main if you want. Don't forget to include a return value for your function.
Ok, thanks for the help so far. I looked over it again and realised a few errors, and here it is now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;

	short signed int base;
	short signed int exponent;
	long signed int powerVal; 

long signed int power(short signed int base, short signed int exponent);

int main()
{
	cout << "What is the base? ";
	cin >> base;
	cout << "\nAnd what is the exponent? ";
	cin >> exponent;

	powerVal = power(base, exponent);

	cout << "\n" << base << " raised to the " << exponent << "th is equal to " << powerVal;
}
	long signed int power(short signed int base, short signed int exponent)
{
	while(exponent > 0);
		base = base * base;
		exponent--;
		if(!exponent){
		return base;
		}
}


It seems to go through fine until the function power() is called, at which point it does nothing. Any help at all would be good, thanks!
I think your power function needs to be something like this:

1
2
3
4
5
6
7
8
9
10
long signed int power(short signed int base, short signed int exponent)
{
	long signed int result = base;

	while(exponent-- > 1)
	{
		result *= base;
	}
	return result;
}


Your previous program was getting stuck in an endless while loop because you put a semicolon after the 'while'.
Oh and this won't work for negative exponents of course. Seeing as you're accepting a signed 'exponent' parameter, you may want to think about how to make your function work with negative exponents too :).
Yeah, thought as much. That should do the trick.

As for negative exponents, not concerned about that as Im only doing this as an exercise for my C++

Thanks again for all the help!
Topic archived. No new replies allowed.