CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2014
    Posts
    5

    Post 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!

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  3. #3
    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. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  5. #5
    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. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    Quote Originally Posted by Lieutenantpickle View Post
    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. #7
    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. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

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

  9. #9
    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. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

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

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

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

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

    Quote Originally Posted by Lieutenantpickle View Post
    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. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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
  •  





Click Here to Expand Forum to Full Width

Featured