# Early program stop problem

• 02-17-2006
bmb_ksu
Early program stop problem
I've checked out all my teacher's online examples, and worked at this thing for a long time, and i cannot for the life of me figure out why my program stops and closes in the middle.

I took out the if statements to see if that was where the error was, but that didn't fix it.

The program compiles and runs, but after the third scanf it just closes down on me. Any help would be greatly appreciated.

Code:

```#include <stdio.h> float parallel_resis(float,float,float); float series_resis(float,float,float); main() {       float tot_par,tot_ser,r1,r2,r3;       printf("Please input the values for resistance in ohms\n\n");       printf("Value for resistance 1:\n");       scanf("%f",&r1);       printf("Value for resistance 2:\n");       scanf("%f",&r2);       printf("Value for resistance 3:\n");       scanf("%f",&r3);       tot_par=parallel_resis(r1,r2,r3);       tot_ser=series_resis(r1,r2,r3);       printf("The equivalent resistance for values of %f, %f, and %f are:\n\n",r1,r2,r3);       printf("For series wiring: %.3f\n", tot_ser);       printf("For parallel wiring: %.3f\n", tot_par);       return 0; } float parallel_resis(float r1, float r2, float r3) {       return (1/(1/r1 + 1/r2 + 1/r3)); } float series_resis(float r1, float r2, float r3) {       return r1 + r2 + r3; }```
• 02-17-2006
Dave_Sinkula
• 02-17-2006
bmb_ksu
Thanks for the response, i appreciate it.

That didn't work. It still did the same thing. i tried finding it through the "run" program comman and then just by opening the .exe file directly. It still closes after my third input.

Any other ideas.
• 02-17-2006
Dave_Sinkula
Open a command shell and then invoke the program from the command line. Or else follow the other suggestion(s) in the FAQ.

http://img143.imageshack.us/img143/217/cmd5tx.jpg
• 02-17-2006
bmb_ksu
Thanks,

Just for curiosity sakes, do you know why it closes out using the compiler?
• 02-18-2006
Richie T
console apps are not designed to run outside of the shell, so they
close once the program returns a value. The FAQ highlights that
the most common way to run a program from outside the shell
is to have it wait for some input before returning. If you tried the
methods there and they didn't work, then my guess would be that
you tried to use a getchar. The problem is that getchar and scanf
dont mix. read up on flushing the input buffer
• 02-18-2006
dwks
• 02-19-2006
cc870
Quote:

Originally Posted by bmb_ksu
I've checked out all my teacher's online examples, and worked at this thing for a long time, and i cannot for the life of me figure out why my program stops and closes in the middle.

I took out the if statements to see if that was where the error was, but that didn't fix it.

The program compiles and runs, but after the third scanf it just closes down on me. Any help would be greatly appreciated.

Code:

```#include <stdio.h> float parallel_resis(float,float,float); float series_resis(float,float,float); main() {       float tot_par,tot_ser,r1,r2,r3;       printf("Please input the values for resistance in ohms\n\n");       printf("Value for resistance 1:\n");       scanf("%f",&r1);       printf("Value for resistance 2:\n");       scanf("%f",&r2);       printf("Value for resistance 3:\n");       scanf("%f",&r3);       tot_par=parallel_resis(r1,r2,r3);       tot_ser=series_resis(r1,r2,r3);       printf("The equivalent resistance for values of %f, %f, and %f are:\n\n",r1,r2,r3);       printf("For series wiring: %.3f\n", tot_ser);       printf("For parallel wiring: %.3f\n", tot_par);       return 0; } float parallel_resis(float r1, float r2, float r3) {       return (1/(1/r1 + 1/r2 + 1/r3)); } float series_resis(float r1, float r2, float r3) {       return r1 + r2 + r3; }```

Actually, the program does not close on 3rd scanf.... The program calculate N output the answers. It is juz tat u dun have a line to "hold" the screen.

Code:

```printf("For series wiring: %.3f\n", tot_ser);       printf("For parallel wiring: %.3f\n", tot_par);       getch();       return 0;```
Remember to include<conio.h> <== for getch
• 02-19-2006
quzah
Why? Why on earth do people insist on using getch, which requires including another header file, when you can use the header file you're already including, and use getchar?

That was rhetorical.

Quzah.
• 02-19-2006
cc870
O.... ic... tks quzah...