Thread: Exponential Calculator: while loop fail for negative base

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    27

    Exponential Calculator: while loop fail for negative base

    So, I had to make an exponent calculator without using pow() or any other predefined functions. It's supposed to use recursion.

    Below is my current code. The issue is, my program is supposed to handle negative bases, but my attempt to reprompt the user to enter a positive base with a while loop has been failing.

    Code:
    #include <stdio.h>
    
    
    float exponential(float base, int exponent);
    
    
    int main(){
        float base, result;
        int exponent;
    
    
        printf("Enter base: ");
        scanf("%f", &base);
    
    
        while(base < 0.0);
        {
            printf("Sorry, the base is not positive.");
            printf("Enter base: ");
            scanf("%f", &base);
        }
    
    
        printf("Enter the exponent: ");
        scanf("%d", &exponent);
    
    
        result = exponential(base, exponent);
    
    
        printf("Exponential calculation result: %f^%d = %f", base, exponent, result);
        return 0;
    }
    
    
    float exponential(float base, int exponent){
    
    
        if(exponent == 0){
            return 1;
        }
    
    
        else if (exponent < 0){
            return  1.0 / (base * exponential(base, abs(exponent) - 1));
        }
    
    
        else{
        return base * exponential(base, exponent - 1);
        }
    }
    Currently, the while loop executes even if the initial base entered is positive, and I'm not sure why this is the case.

    Thanks in advance for any assistance!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    while(base < 0.0);
    That semi-colon should not be there.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You have an extraneous semicolon after your while !!!

    You need to include stdlib.h for the abs() function. If your compiler didn't complain about that you need to turn up the warning level (on gcc you can use the flags -Wall -W -pedantic to maximize warnings).

    And you're forgetting to print newlines after your output.

    BTW, you are over-spacing your code. There's no need for the double spacing. It's just annoying.
    Last edited by algorism; 02-07-2017 at 11:11 AM.

  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    27

    Thanks!

    Perfect, I figured it was something easily-fixed like that. My apologies for the simple error.

    Also, I will endeavour to reduce my spacing. Sorry for the inconvenience

    Thanks again <3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [prob] base power calculator
    By Rock Rocky in forum C Programming
    Replies: 8
    Last Post: 04-08-2016, 03:26 PM
  2. How to end loop by enter a negative number?
    By maymay2016 in forum C Programming
    Replies: 18
    Last Post: 04-05-2016, 05:16 PM
  3. fail to count digit of an integer (fail at 9)
    By azsquall in forum C++ Programming
    Replies: 3
    Last Post: 05-02-2008, 09:42 AM
  4. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  5. Negative Exponential
    By vasanth in forum Tech Board
    Replies: 2
    Last Post: 09-01-2004, 09:31 AM

Tags for this Thread