Thread: C program crashed

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    21

    C program crashed

    this is my program.

    Code:
    #include<stdio.h>
    #include<conio.h>
    struct abc
    {
     char name[20];
     int day;
     char month[10];
     int year;
     float salary;
    };
    
     void main()
     {
     struct abc person[10];
     //float totalsalary=0;
     int n,i,t;
     char month[12];
     clrscr();
     printf("enter number of employees\n");
    
    
    
     scanf("%d",&n);
     printf("number of employee =%d\n",n);
    
     for(i=0;i<=n;i++)
     {
      printf("input values\n");
      scanf("%s %d %s %d %f",person[i].name,&person[i].day,person[i].month,&person[i].year,&person[i].salary);
    //  printf("%s %d %s %d %f\n",person[i].name,person[i].day,person[i].month,person[i].year,person[i].salary);
    
    
     }
     /*
     printf("enter the month for totalsalary");
     scanf("%s",month);
    
     for(i=0;i<=n;i++)
     {
      t=strcmp(person[i].month,month);
      if(t==0){
         totalsalary=totalsalary+person[i].salary;
      }
     }
    
    
    printf("the totalsalary for giving moth = %f",totalsalary);  */
    getch();
    }
    enter number of employees
    2

    number of employee =2

    input values
    Test 23 january 2012 6700 //Enter pressed

    Now It goes back to turbo c++ editor

    what I'm doing wrong ?
    Last edited by Stream; 04-15-2018 at 05:00 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Your main problem is TurboCrap. Stop using it!
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,570
    Well the first thing you're doing wrong is using a compiler from the stone age.

    Second, your output doesn't agree with your code.
    > printf("number of employee =%d\n",n);
    Did you see this?
    Make sure you're compiling and running your latest edit.

    Third, for(i=0;i<=n;i++) runs n+1 times, not n times.
    So when you get around to typing in 10, you overflow your array.

    Then there's the whole void main issue as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Quote Originally Posted by Salem View Post

    Second, your output doesn't agree with your code.
    > printf("number of employee =%d\n",n);
    Did you see this?
    yes .. it prints 2 when I input 2.

    Make sure you're compiling and running your latest edit.
    Yes


    Third, for(i=0;i<=n;i++) runs n+1 times, not n times.
    So when you get around to typing in 10, you overflow your array.

    Then there's the whole void main issue as well.
    Okay.

    I changed to
    Code:
     for(i=1;i<=n;i++)
    but same result...It did not work

    so whats the issue here ? Why my program did not work. What to fix ?


    updated code
    Code:
    #include<stdio.h>
    #include<conio.h>
    struct abc
    {
     char name[20];
     int day;
     char month[10];
     int year;
     float salary;
    };
    
     void main()
     {
     struct abc person[10];
     //float totalsalary=0;
     int n,i,t;
     char month[12];
     clrscr();
     printf("enter number of employees\n");
    
    
    
     scanf("%d",&n);
     printf("number of employee =%d\n",n);
    
     for(i=1;i<=n;i++)
     {
      printf("input values\n");
      scanf("%s %d %s %d %f",person[i].name,&person[i].day,person[i].month,&person[i].year,&person[i].salary);
      printf("%s %d %s %d %f\n",person[i].name,person[i].day,person[i].month,person[i].year,person[i].salary);
    
    
     }
     /*
     printf("enter the month for totalsalary");
     scanf("%s",month);
    
     for(i=0;i<=n;i++)
     {
      t=strcmp(person[i].month,month);
      if(t==0){
         totalsalary=totalsalary+person[i].salary;
      }
     }
    
    
    printf("the totalsalary for giving moth = %f",totalsalary);  */
    getch();
    }
    Last edited by Stream; 04-15-2018 at 05:07 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,570
    Well changing to starting at 1 fixes the number of loop iterations, but doesn't fix your array overrun issue.
    Array subscripts start at 0, and finish at n-1.

    Another problem is that your array is on the stack, and probably takes 300 to 400 bytes.
    Your archaic compiler with it's 16-bit world is probably running out of stack space. The default stack was very small IIRC.

    You might try making your person array global to save blowing your meagre stack away.

    But it's just putting lipstick on a pig IMO.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    Quote Originally Posted by Salem View Post
    Your archaic compiler with it's 16-bit world is probably running out of stack space. The default stack was very small IIRC.

    You might try making your person array global to save blowing your meagre stack away.
    Does this mean to put person array outside main() ?
    Last edited by Stream; 04-15-2018 at 10:23 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,570
    Yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashed
    By byebyebyezzz in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2011, 04:42 PM
  2. Program crashed, no compilation errors
    By kaopei in forum C Programming
    Replies: 1
    Last Post: 11-20-2010, 06:44 AM
  3. using fprintf crashed my program
    By kiros88 in forum C Programming
    Replies: 4
    Last Post: 11-08-2010, 01:14 PM
  4. Auto shutdown of crashed program...
    By phil_drew in forum Windows Programming
    Replies: 1
    Last Post: 04-24-2004, 12:48 AM
  5. My Program Crashed help!!!
    By St0rmTroop3er in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2003, 04:25 PM

Tags for this Thread