-
April 28th, 2014, 08:24 AM
#1
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;
}
}
}
I need help please. Sorry for my bad explanation and english. Looking forward to your reply. Thank you very much!
-
April 28th, 2014, 08:46 AM
#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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2014, 09:03 AM
#3
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
-
April 28th, 2014, 09:31 AM
#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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2014, 09:47 AM
#5
Re: C++ pow() limit loop [URGENT]
I see. What do you suggest on typing? I'm really confused right now
-
April 28th, 2014, 09:53 AM
#6
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.
-
April 28th, 2014, 10:06 AM
#7
Re: C++ pow() limit loop [URGENT]
For the different for loop parts, do I have to set each in 3 different voids?
-
April 28th, 2014, 10:14 AM
#8
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.
-
April 28th, 2014, 11:08 AM
#9
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++);
}
-
April 28th, 2014, 11:14 AM
#10
Re: C++ pow() limit loop [URGENT]
And what this condition
Originally Posted by Lieutenantpickle
Code:
if (limit > 1, limit < result)
{
...
}
could mean?
Victor Nijegorodov
-
April 28th, 2014, 11:29 AM
#11
Re: C++ pow() limit loop [URGENT]
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2014, 11:31 AM
#12
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.
-
April 28th, 2014, 01:12 PM
#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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|