CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: C++ pow() limit loop [URGENT]

1. Junior Member
Join Date
Apr 2014
Posts
5

## C++ pow() limit loop [URGENT]

Hi, I'm super super new to C++. I need help with my code about limiting the pow(base, exponent) to a given value by using a while or for loop. The values are given by the user.
Ex: Base = 2
Exponent = 2
Limit = 100
Result = 64
(loop stops at 2^5)

Ex: Base = 3
Exponent = 3
Limit = 1000
Result = 729
(loop stops at 3^6)

My main() so far:

Code:
```#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdio.h>

using namespace std;

int main()
{
double base;
int exponent;
int limit;
int result = 0;

cout << "Please enter a base: ";
cin >> base;
cout << "Please enter a exponent: ";
cin >> exponent;
cout << "Please enter limit: ";
cin >> limit;

for (result > result; limit < 1; limit++){
result = pow(base, exponent);
{
if (limit > result)
{

cout << base << "^" << exponent << ", " << limit << " LIMIT IS MORE THAN RESULT " << ", " << result << endl;
exponent++;
}
else if (limit < result)
{
cout << base << "^" << exponent << ", " << limit << " LIMIT IS LESS THAN RESULT " << ", " << result << endl;
exponent++;
}

return 0;
}
}
}```

2. ## Re: C++ pow() limit loop [URGENT]

MArking a thread as urgent doesn't get any quicker responses.

Code:
`for (result > result; limit < 1; limit++){`
whatever you thought this statement was going to achieve, it doesn't. It first compares result to itself(?) to see which is greater(?) then does nothing with the result of that comparision. The loop will terminate when limit >=1 and each time around the loop limit will be incremented by 1. The loop needs to compare limit with result and terminate if result is greater than the limit. Once the data has been entered there is no requirement to change limit.

3. Junior Member
Join Date
Apr 2014
Posts
5

## Re: C++ pow() limit loop [URGENT]

Whoops......I changed

Code:
`	for (result > result; limit < 1; limit++){`
to

Code:
`for (result = 0; limit < 1; limit++){`
Thanks for the heads up though

4. ## Re: C++ pow() limit loop [URGENT]

Code:
`for (result = 0; limit < 1; limit++){`
but the condition is still not correct and you are still incrementing limit.

5. Junior Member
Join Date
Apr 2014
Posts
5

## Re: C++ pow() limit loop [URGENT]

I see. What do you suggest on typing? I'm really confused right now

6. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: C++ pow() limit loop [URGENT]

Originally Posted by Lieutenantpickle
I see. What do you suggest on typing? I'm really confused right now
You're just guessing which is never a good plan. You can use a for or a while loop. In general, a for loop works best when a certain number or iterations is needed. A while loop is best while a certain condition is true. In your case, you want to repeat the action while the result is less than the limit.

In the case of a for loop, the first part is your initializer. I would think you'd want to initialize your exponent there. The second part is the condition in which it keeps executing. In this case, the result is less than the limit. The third part is executed every time through the loop, and is usually used to increment or decrement the variable that was initialized, in this case the exponent.
Last edited by GCDEF; April 28th, 2014 at 09:56 AM.

7. Junior Member
Join Date
Apr 2014
Posts
5

## Re: C++ pow() limit loop [URGENT]

For the different for loop parts, do I have to set each in 3 different voids?

8. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: C++ pow() limit loop [URGENT]

Originally Posted by Lieutenantpickle
For the different for loop parts, do I have to set each in 3 different voids?
The question doesn't make sense.

9. Junior Member
Join Date
Apr 2014
Posts
5

## Re: C++ pow() limit loop [URGENT]

I tried this. Unfortunately, it doesn't print anything :/

Code:
```	for (exponent; result < limit; exponent++);

{

if (limit > 1, limit < result)
{

printf("%d, %d, %d", base, exponent, limit, exponent++);

}```

10. ## Re: C++ pow() limit loop [URGENT]

And what this condition
Originally Posted by Lieutenantpickle
Code:
```	if (limit > 1, limit < result)
{
...
}```
could mean?

11. ## Re: C++ pow() limit loop [URGENT]

12. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: C++ pow() limit loop [URGENT]

Originally Posted by Lieutenantpickle
I tried this. Unfortunately, it doesn't print anything :/

Code:
```	for (exponent; result < limit; exponent++);

{

if (limit > 1, limit < result)
{

printf("%d, %d, %d", base, exponent, limit, exponent++);

}```
I'll say it again. You'll never learn this language by guessing. Never. It just won't work. If you want to use a for loop, read about it and learn what each piece does. Keep reading till you really understand. Repeat that with the if statement. Neither your for loop, nor your if statement are correct.

13. ## Re: C++ pow() limit loop [URGENT]

Code:
`if (limit > 1, limit < result)`
What do you expect this statement to do? This may be legal c++ syntax but I doubt it does what you expect. Do you understand the comma operator?
See http://msdn.microsoft.com/en-us/library/zs06xbxh.aspx
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx

I repeat what GCDEF has correctly stated. You cannot program in c/c++ by trying to guess. You have to learn its syntax properly without guessing. Its not like a scripted language where you can try a few things by trial and error until they work. This sort of approach doesn't work with c/c++. See my post #11 for c++ tutorial web sites.

PS Why do you have a return in the middle of a loop?

This is quite a trivial program. Apart from include, variable definitons and input/output statements the program only needs one line of code.
Last edited by 2kaud; April 28th, 2014 at 01:36 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•