Search code examples
cexponential

Trouble with an Exponential


I cannot get the exponential to work with user input. Every time user is prompted to add input, after the input is entered the program immediately closes.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
   double input = 0;
   double exp_val;
   printf("Plese enter the exponent to check for convergence:");
   scanf("%f", input,"\n");
   printf("%f", input);/*Checking to verify the input is entered*/
   exp_val = exp(input);
   printf("%f", exp_val);
   getchar();
}

Solution

  • Besides @happydave's answer to use &input, you also need the %lf format specifier to read a double:

    scanf("%lf", &input);
    

    Check out Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?


    After this, you should get the correct answer, see it live: http://ideone.com/246clu.