Program closes too early

This is a discussion on Program closes too early within the C Programming forums, part of the General Programming Boards category; My program closes immediately after carrying the for loop is terminated. Does any one know why this might be happening? ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    Program closes too early

    My program closes immediately after carrying the for loop is terminated. Does any one know why this might be happening? Here's the for loop, I'm very inexperienced in programming so I've probably made a simple mistake...
    Code:
    for(i=1; i<=n_students; i++) {
            printf ("\nEnter the number of marks student %d recieved on Assignment One: ", i);
            scanf ("%lf", &ass1_mark);
                if (i == 1) ass1_av = ass1_mark;
                else ass1_av = (ass1_mark*(1/i)+ass1_av*(1-(1/i)));  
                  
            printf ("\nEnter the number of marks student %d recieved on Assignment Two: ", i);
            scanf ("%lf", &ass2_mark); 
                if (i == 1) ass2_av = ass2_mark;
                else ass2_av = (ass2_mark*(1/i)+ass2_av*(1-(1/i)));  
                
            printf ("\nEnter the number of marks student %d recieved on Assignment Three: ", i);
            scanf ("%lf", &ass3_mark); 
                if (i == 1) ass3_av = ass3_mark;
                else ass3_av = (ass3_mark*(1/i)+ass3_av*(1-(1/i)));
                  
            printf ("\nEnter the number of marks student %d recieved on Assignment Four: ", i);
            scanf ("%lf", &ass4_mark); 
                if (i == 1) ass4_av = ass4_mark;
                else ass4_av = (ass4_mark*(1/i)+ass4_av*(1-(1/i)));  
                
            printf ("\nEnter the number of marks student %d recieved on Assignment Five: ", i);
            scanf ("%lf", &ass5_mark); 
                if (i == 1) ass5_av = ass5_mark;
                else ass5_av = (ass5_mark*(1/i)+ass5_av*(1-(1/i)));  
                
            printf ("\nEnter the number of marks student %d recieved on the midterm: ", i);
            scanf ("%lf", &midterm); 
                if (i == 1) midterm_av = midterm;
                else midterm_av = (midterm*(1/i)+midterm_av*(1-(1/i)));  
            
            printf ("\nEnter the number of marks student %d recieved on the final: ", i);
            scanf ("%lf", &final); 
                if (i == 1) final_av = final;
                else final_av = (final*(1/i)+final_av*(1-(1/i))); 
            }
    Thank you!!
    Last edited by Salem; 03-08-2011 at 11:39 AM. Reason: Added [code][/code] tags

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Quote Originally Posted by RebekahNicole View Post
    My program closes immediately after carrying the for loop is terminated. Does any one know why this might be happening? Here's the for loop, I'm very inexperienced in programming so I've probably made a simple mistake...

    Code:
    for(i=1; i<=n_students; i++) {
            printf ("\nEnter the number of marks student %d recieved on Assignment One: ", i);
            scanf ("%lf", &ass1_mark);
                if (i == 1) ass1_av = ass1_mark;
                else ass1_av = (ass1_mark*(1/i)+ass1_av*(1-(1/i)));  
                  
            printf ("\nEnter the number of marks student %d recieved on Assignment Two: ", i);
            scanf ("%lf", &ass2_mark); 
                if (i == 1) ass2_av = ass2_mark;
                else ass2_av = (ass2_mark*(1/i)+ass2_av*(1-(1/i)));  
                
            printf ("\nEnter the number of marks student %d recieved on Assignment Three: ", i);
            scanf ("%lf", &ass3_mark); 
                if (i == 1) ass3_av = ass3_mark;
                else ass3_av = (ass3_mark*(1/i)+ass3_av*(1-(1/i)));
                  
            printf ("\nEnter the number of marks student %d recieved on Assignment Four: ", i);
            scanf ("%lf", &ass4_mark); 
                if (i == 1) ass4_av = ass4_mark;
                else ass4_av = (ass4_mark*(1/i)+ass4_av*(1-(1/i)));  
                
            printf ("\nEnter the number of marks student %d recieved on Assignment Five: ", i);
            scanf ("%lf", &ass5_mark); 
                if (i == 1) ass5_av = ass5_mark;
                else ass5_av = (ass5_mark*(1/i)+ass5_av*(1-(1/i)));  
                
            printf ("\nEnter the number of marks student %d recieved on the midterm: ", i);
            scanf ("%lf", &midterm); 
                if (i == 1) midterm_av = midterm;
                else midterm_av = (midterm*(1/i)+midterm_av*(1-(1/i)));  
            
            printf ("\nEnter the number of marks student %d recieved on the final: ", i);
            scanf ("%lf", &final); 
                if (i == 1) final_av = final;
                else final_av = (final*(1/i)+final_av*(1-(1/i))); 
            }
    Thank you!!
    For the sake of everyone's eyes...
    Last edited by GReaper; 03-08-2011 at 11:20 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Sorry, my bad, meaning?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Quote Originally Posted by RebekahNicole View Post
    Sorry, my bad, meaning?
    I did a mistake, but now i corrected it!
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Do you know the answer to my question or?

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Quote Originally Posted by RebekahNicole View Post
    Do you know the answer to my question or?
    What's the value of "n_students"?

    Quote Originally Posted by RebekahNicole View Post
    My program closes immediately after carrying the for loop is terminated.
    What do you actually mean by that? That after the "for" is terminated the program closes immediately or that the "for" terminates immediately?
    Last edited by GReaper; 03-08-2011 at 11:31 AM.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Sorry, I worded that really awkwardly. What I meant was, when I run my program, it closes immediately after the for loop is finished, even though there are more things that are supposed to happen after this.

    n_students is subject to change but when testing the program I've been using the number 2

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Quote Originally Posted by RebekahNicole View Post
    What I meant was, when I run my program, it closes immediately after the for loop is finished, even though there are more things that are supposed to happen after this.
    Well, it depends on what's happening after that. For example, if you use a "getchar()" after the loop, your program will never stop. ( That happens because some character are still in the input stream, like newline, space etc )
    I can't possibly know what's wrong if you don't show me the code after that.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Early program stop problem
    By bmb_ksu in forum C Programming
    Replies: 9
    Last Post: 02-19-2006, 08:24 AM
  2. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 06:44 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. program open and closes
    By Musicdip in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-27-2002, 04:34 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


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