Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exponents don't work! 2

Status
Not open for further replies.

fafasdgsadgas

Programmer
Aug 17, 2003
8
0
0
US
I have recently started learning about C++ and got Visual C++ 6 Standard Edition, I tried to make a program that would ask for a number and then print the values of that number to the power of 0-20, unfortunately it won't seem to do the equation correctly. I tried setting up a function for the equation but it still won't work. I did include the cstdlib and cmath headers and even tried stdlib.h and math.h with no avail. I also have conio.h, if that helps at all. Is it a bug in Visual Studio or my error? I'll send the source if requested.
 
Well, let's see what you've tried. Generally a simple for() loop is sufficient.
 
What is(are) the error message(s) you are getting? Let them see the source for the equation function if you are convinced the error is there.

-Bones
 
#include <conio.h>
#include <iostream>
#include <math.h>
#include <cmath>
#include <cstdlib>
using namespace std;

int pow(int first, int second);

int main()
{
int num=0;
int power=0;
int count=0;
cout<<&quot;Created by Martin Castner&quot;<<endl;
cout<<&quot;It will ask for a number and then show the value of the number to the power of 0 through 20.&quot;<<endl;
cout<<&quot;Please enter the base number:&quot;<<endl;
cin>>num;
for(int count=0;count<21;count++)
{
power=num^count;
cout<<num<<&quot; to the power of &quot;<<expon<<&quot; is equal to &quot;<<power<<&quot;.&quot;<<endl;

}
cout<<&quot;Press any key to quit...&quot;<<endl;
getch();
return 0;
}



That is my source, fairly small. There are no errors but try it out and see if it works for you. If I enter 1 as the base it will do:
1^0=1
1^1=0 (really doing 1-1)
1^2=3 (2+1)
1^3=2 (3-1)
1^4=5 (4+1)
etc...
 
There is no power operator in C. The ^ operator you're using is for the &quot;bitwise exclusive or&quot; operation. The results you're getting reflect those results.

There is, however, a pow() function that does this operation.

Dennis
 
The definition looks like this:

Code:
double pow(double x, double y);

It returns x to the power of y. You'll need to #include math.h.
 
Didn't help! This is what I put:

#include <conio.h>
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;


int main()
{
double num=0;
double power=0;
double count=0;
cout<<&quot;Programmed by Martin Castner&quot;<<endl;
cout<<&quot;It will ask for a number and then show the value of the number to the power of 0 through 20.&quot;<<endl;
cout<<&quot;Please enter the base number:&quot;<<endl;
cin>>num;

for(int count=0;count<21;count++)
{
power=pow(double num, double count);
cout<<num<<&quot; to the power of &quot;<<count<<&quot; is equal to &quot;<<power<<&quot;.&quot;<<endl;

}
cout<<&quot;Press any key to quit...&quot;<<endl;
getch();
return 0;
}

What's wrong, I put a function prototype at the beginning too but it didn't work either. I get the same results as last time.
 
If you fix the compile errors this should work fine. The compile errors you are getting should point you to what to fix, but here are a few more hints:[ol][li]You declared count twice, once as a double and once inside the for loop as an int. Just declare it once as a double.[/li][li]When you are calling a function, you aren't supposed to specify the type of the parameter that you are passing in. So when you call pow you don't need to put 'double' before 'num' and 'count'.[/li][li]Just a suggestion: use all of the new/correct runtime library headers instead of mixing and matching (e.g. cmath, cstdlib, and iostream).[/li][/ol]Once I fixed the compile errors it seemed to work for me.
 
I gotta update my software! I never got any compile errors or anything and it still won't work. Let me get all the updates and check back with you.
 
It works! Just installed SP 5 for Visual C++ and everything worked without needing to change the program at all, I guess it was my compiler!
 
No, you didn't listen to uolj's response, it's not a compiler error, it's a keyboard error ( ie: what you typed. ;) ).

So again:

>> int count=0;

...then

>> for(int count=0;count<21;count++)

...declared again!


Luckily, there is no side effect in this case, but still, an error's an error.


>> power=pow(double num, double count);

That of course should be:

>> power=pow(num, count);


Now for a couple of nitpicks.

>> cin>>num;

cin should *always* be followed up with a:

>> if(cin.good()) // safe to proceed

Finally, cin.get() can be used instead of getch(), meaning you wouldn't have to include the non-portable <conio.h>.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top