0

I am taking my first programming class this semester and I can't figure out what is going on with my program. I have to write a program that calculates the total amount of money after so many years with interest. The formula is y=p*(1+r)^n

Anyways, whenever I run my code it comes up as "_ has stopped working" and closes.

Here is my code:

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

int main (void)
{
    double p, r, n, y; 

    printf("Enter the interest rate>");
    scanf("%lf", r);
    printf("Enter the principle amount of money invested>");
    scanf("%lf", p);
    printf("Enter the number of years invested>");
    scanf("%lf", n);

    y = pow(p*(1+r),n);

    printf("The total amount of money is %f.\n", y);

    system("PAUSE");
    return (0);
}

I have tried googling it and it seems like it might have something to do with "initializing", but I'm not sure what that means or how to do it. Any help is greatly appreciated!

2 Answers2

0

First of all the scanf() function expects the adress of a variable not the variable itself, so it should be used like that scanf("%lf", &r);Try it and you will be okay

And secondly never use system("PAUSE")!! It is platform specific (windows) and it is wrong system("pause"); - Why is it wrong?

You are learning to program in a wrong way by using system(PAUSE) and in the above link you can see why!

Community
  • 1
  • 1
0

This code was written and tested on linux platform.

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

int main (void)
{
    double p, r, n, y, value; 
    int a = 3, b = 2;

        printf("Enter the interest rate>");
        scanf("%lf", &r);
        printf("Enter the principle amount of money invested>");
        scanf("%lf", &p);
        printf("Enter the number of years invested>");
        scanf("%lf", &n);
    value = p * (r + 1);
    y = pow(value, n);
        printf("The total amount of money is %f.\n", y);

        //system("PAUSE"); 
        return (0);
}

to compile this code in linux use,

gcc code.c -lm

I dont know why, I am forced to include -lm at compile time even though I am adding #include. Any one feel free to update the answer on this.

Update.

Please see this answer for why we must use -lm Undefined reference to 'pow' even though -lm is a compile flag. [C]

Community
  • 1
  • 1
Megharaj
  • 1,589
  • 2
  • 20
  • 32