# Program closes too early

• 03-08-2011
RebekahNicole
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!!
• 03-08-2011
GReaper
Quote:

Originally Posted by RebekahNicole
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...
• 03-08-2011
RebekahNicole
• 03-08-2011
GReaper
Quote:

Originally Posted by RebekahNicole

I did a mistake, but now i corrected it! :D
• 03-08-2011
RebekahNicole
Do you know the answer to my question or?
• 03-08-2011
GReaper
Quote:

Originally Posted by RebekahNicole
Do you know the answer to my question or?

What's the value of "n_students"?

Quote:

Originally Posted by RebekahNicole
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? :confused:
• 03-08-2011
RebekahNicole
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
• 03-08-2011
GReaper
Quote:

Originally Posted by RebekahNicole
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.