need help on code please.
Hi ppl
Im having a bit of trouble with one of my c programs. Im sort of new to c prog. so im not too sure if im like failing to notice an obvious mistake. anyway heres what the problem is:
im trying to code a prog that will do investment calculations. It'll accept input of type int and double (or float). I ask for each variable to be entered, scanf it and then plug it all into an equation:
CODE
-----------------------
#include <stdio.h>
#include <math.h>
main()
{
int t1, t2, t3, n1, n2, n3;
double p1, p2, p3, a1, a2, a3, i1, i2, i3;
printf(" \n Investment Calculator ");
printf(" \n --------------------- ");
printf(" \n ");
printf(" \n Entering data for Investment A: \n");
printf(" \n Please Enter the Principal : ");
scanf(" %lf ", &p1);
/* testing */
/*printf(" \n entered : %.2f", p1); */
printf(" \n Please Enter the Annual Interest Rate :");
scanf("%lf", &i1);
printf(" \n Please Enter the Number of Compouding Periods per Year: ");
scanf("%d", &n1);
printf(" \n Please Enter the Number of Years over which you are calculating this Investment : ");
scanf("%d", &t1);
a1 = p1 * pow(1.0 + (i1 / n1), (n1 * t1));
printf(" \n The final amount is : %.2f", a1);
return 0;
}
--------------
i was test running the prog as i coded and i came accross a strange problem. After it asks to enter the Principal value and i enter it and press ENTER nothing happenes. The prog just freezes. i put some testing code in but that never came up on the screen either (later i uncomented it). When i tried to close the prog once i accidentaly presses ctrl_x and enter after it and the rest of the prog just flashed before me and exited (didnt ask for other inputs - the text just printed and ------ end). i cant c what the problem is so im asking you experienced ppl to help me out.
thanx a lot
p.s.
i also tried to make the double variables into float type. didnt do anything.
i have also tried coding the prog with functions in it but that had the same problem:
#include <stdio.h>
#include <math.h>
double inv(double, double, int, int);
void quit(void);
void proc(void);
main()
{
char qw;
printf(" \n ");
printf(" \n Investment Calculator ");
printf(" \n --------------------- ");
printf(" \n To continue press c. To quit press q. > ");
scanf("%c", &qw); /* get choice */
fflush(stdin); /* flush (clear) input */
while(qw != 'c' && qw != 'q')
{
printf("\n please enter (c) or (q) > ");
scanf("%c", &qw);
fflush(stdin);
}
if (qw == 'q')
{
quit();
}
else if (qw == 'c')
{
proc();
}
return 0;
}
void proc(void)
{
int t1, t2, t3, n1, n2, n3;
double p1, p2, p3, a1, a2, a3, i1, i2, i3;
printf(" \n Entering data for Investment A: \n");
printf(" \n Please Enter the Principal : ");
scanf(" %lf ", &p1);
printf(" \n Please Enter the Annual Interest Rate :");
scanf("%lf", &i1);
printf(" \n Please Enter the Number of Compouding Periods per Year: ");
scanf("%d", &n1);
printf(" \n Please Enter the Number of Years");
printf("\n over which you are calculating this Investment : ");
scanf("%d", &t1);
a1 = inv(p1, i1, n1, t1);
printf(" \n Investment A: %lf ", a1);
}
double inv(double p, double i, int n, int t)
{
double A;
A = p * pow(1.0 + (i / n), (n * t));
return A;
}
void quit(void) /* function to terminate the prog */
{
printf("\n Exiting the program.....\n");
printf("\n");
exit(1);
}