Early program stop problem

This is a discussion on Early program stop problem within the C Programming forums, part of the General Programming Boards category; I've checked out all my teacher's online examples, and worked at this thing for a long time, and i cannot ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Wichita, KS
    Posts
    6

    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;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    Wichita, KS
    Posts
    6
    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.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Open a command shell and then invoke the program from the command line. Or else follow the other suggestion(s) in the FAQ.

    [edit]http://img143.imageshack.us/img143/217/cmd5tx.jpg
    Last edited by Dave_Sinkula; 02-17-2006 at 11:25 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    Wichita, KS
    Posts
    6
    Thanks,

    Just for curiosity sakes, do you know why it closes out using the compiler?

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    15
    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

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    15
    O.... ic... tks quzah...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I have finished my program, one problem
    By sloopy in forum C Programming
    Replies: 4
    Last Post: 11-29-2005, 02:10 AM
  2. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 10:37 PM
  3. Replies: 2
    Last Post: 04-25-2005, 12:59 PM
  4. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 07:33 AM
  5. Some Problem With My Program
    By Americano in forum C Programming
    Replies: 5
    Last Post: 10-18-2003, 02:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21