program compiles but does'nt run

Status
Not open for further replies.

makarand

Broken In
Hey guys help me
I use XP and just made a program in C
when i compile it it shows 0 erros and 0 warnings
when i run the program the first line is displayed "Enter the amount:"
when i do so an erro comes up showing a prolem has occured and we're sorry for the inconvinience
help me
 

::vicky::

In the zone
ya first show the coding i think ur code is ok as u say that code gets compiled but it is a windows error
 

sakumar79

Technomancer
^^ There are two types of errors in programming - compile errors and runtime errors. Compile errors are syntax errors that are caught during compilation of the program. .. The runtime errors are those that are caused by logical errors...
Since your program compiled without problem, you prob. dont have compile errors. Runtime errors can be caused by invalid pointers, division by zero, infinite loops,etc... That seems to be what you have... So, we will need the code to identify what the problem is...

Arun
 

sakumar79

Technomancer
^^^ Post the code because the error most probably lies in the logic somewhere... Only if you post the code can we test and identify the problem...

Arun
 

mayanksharma

Ambassador of Buzz
it seems that your installation for c is not properly done.this thing usually occurs whenever there is corruption. You can also try program compatability mode though!
 
OP
M

makarand

Broken In
#inlcude<stdio.h>
int main()
{
float p,n,r,si;
printf("Enter Principal:",p);
scanf("%f",p);
printf("Enter Years:",n);
scanf("%f",n);
printf("Enter Rate:",r);
scanf("%f",r);
si = p*n*r/100;
printf("Simple Interest:%f",si);
getchar();
}
 

QwertyManiac

Commander in Chief
makarand said:
#inlcude<stdio.h>
int main()
{
float p,n,r,si;
printf("Enter Principal:",p);
scanf("%f",p);
printf("Enter Years:",n);
scanf("%f",n);
printf("Enter Rate:",r);
scanf("%f",r);
si = p*n*r/100;
printf("Simple Interest:%f",si);
getchar();
}
1. u cant use getchar() w/o including its headerfile (conio.h i think, been a long time since C progging)
2. Printf statements that dont display a variable dont need a parameter for that i think.
Code:
printf("Enter Principal:",p); is wrong, printf("Enter Principal:"); is just fine because u are using the scanf statement in the next line
 

Minimalistix

I'm back!
The Solution

QwertManiac said:
u cant use getchar() w/o including its headerfile (conio.h i think, been a long time since C progging)

Yes mate, it's been a long time since you left C programming 'coz the header file <conio.h> isn't needed for getchar(). It's needed for getch() which is what should have been used here.

QwertManiac said:
Printf statements that dont display a variable dont need a parameter for that i think.

Correct! The 2nd parameters aren't needed here but they are not the source of the problem 'coz by nature printf() ignores any "extra" parameters passed to it beyond what's needed by the format string.

it seems that your installation for c is not properly done.
most probably your installation of C could be corrupted.

Now, I don't know how they reached to this conclusion but not everything can be solved by going the "uninstall-then-re-install" way.

So, what's the error? It's actually a very very very simple error and half of you are gonna knock on your heads when you'll know what it is.

scanf() needs variable addresses not their values.

p, n, and r are not pointers. They are simple variables.

So, to pass their addresses to scanf() you have to precede them with an ampersand '&' like &p, &n and &r.

Just modify the original scanf() statements as follows and the program would run fine:

scanf ("%f", &p) ;
scanf ("%f", &n) ;
scanf ("%f", &r) ;

@ makarand :
One more thing, to make your program wait for a keystroke at the end you need to use getch() with #include <conio.h>. getchar() won't work.
 

mayanksharma

Ambassador of Buzz
you have to provide the address pass medium to the variables defined.
do it like this:-

scanf("%f",&r);
and make sure to return value to 0 in the end of the program,if you have included int before main.This will reduce your warnings....
 
Status
Not open for further replies.
Top Bottom