I'm new to C++, and I'm trying to solve a problem to compute an exponent of an integer to a float. The following is the psuedo code:

Declare x as a float
Declare y as an integer
Declare i as an integer
Declare result as a float

Input x //any float
Input y //positive integer

set result = 1 //intial valuue

For i = 1 to y
result = result *x

end for

output result

I have no problem with declaring the variables or reading in the variables. I have problems with my calculations using the "for loop". This is what I have so far:

For (i = 1; y>=0; i++);
result = result *x;
End for

The program is suppose to allow the user to input the variables (x,y) to solve x^y. I know a number multiplied by itself would give you the exponent, but I'm missing something to allow me to do that.

Thanks in advance for your help.


Votes + Comments
Use code tags.
13 Years
Discussion Span
Last Post by frrossk

For (i = 1; y>=0; i++);

As y is always > 0 (input from user), then your test condition in "for" loop is always true, so the loop is infinite. I think is better to write "for (i=1; i<=y; i++)".

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.