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.

dkm30

Dave Sinkula commented: Use code tags. +0

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++)".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.