C Program

I have got a problem with a program I just made using a C compiler.
The problem is that no matter what input I give, the answer is always 0.
Please help.
//program starts here
#include<stdio.h>
void main()
{
int line1,line2;
double pr,rate,ti,SI;
clrscr();
for(line1=0;line1<80;line1++)
{
printf("%c",'-');
}
printf("\n\t\t\tFind Simple Interest\n");
for(line2=0;line2<80;line2++)
{
printf("%c",'-');
}
printf("\nPlease enter the principle\n");
scanf("%d",pr);
printf("\nPlease enter the rate of interest\n");
scanf("%d",rate);
printf("\nPlease enter the time period\n");
scanf("%d",ti);
SI=(pr*rate*ti)/100;
printf("The simple interest is ");
printf("%d",SI);
getch();
}
//program ends here
you should pass pointers to scanf in order to get the values: scanf("%d",&pr);
http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
I have used pointers in all scanf's.
Should I try using int.
No, you did not use pointers. You used the double value directly -- which will cause your program to smash memory and probably segfault.

Look at the link Bazzy gave you. To read a double, you must use
- the correct conversion specifier
- a pointer to the target variable

Hence:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>

int main()
  {
  ...

  /* get loan principle, rate, and duration */
  printf( "\nPlease enter the principle\n" );
  scanf( "%lf", &pr );
  printf( "\nPlease enter the rate of interest\n" );
  scanf( "%lf", &rate );

  ...

  /* get rid of that left-over newline in the input after using scanf() */
  while (getchar() != '\n');

  /* calculate and display the interest */
  SI=(pr*rate*ti)/100.0;
  printf( "The simple interest is %f.\n", SI );

  /* keep the console window open long enough to see the program run */
  printf( "Press ENTER to continue..." );
  while (getchar() != '\n');

  return 0;
  }

Notice also that I didn't use that getch() function from <conio.h>.
The main() function returns type int.

Also, please use [code] tags.

[edit] Here's a plug for using readable variable names, also. For example, why not name the principle 'principle', instead of just 'pr'.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.