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!