Question

I was practicing array of structure. I made the following program and there were no compiling errors.But when i try to run it(I guess these errors are called runtime errors?),it stops working just after accepting the roll number. I wonder what wrong i did. I use Dev c++ and gcc compiler. Here's the code:

#include<stdio.h>




struct student{
char Fname[];
char Lname[];
int reg_no;
int Class;
char sec;
};

void enterinfo(student *,int);
void Display(student *,int);

int main()
{
int i;

printf("\t\t\t Enter student's information\n\n\n\n");
printf("How many students are there in you're school: ");
scanf("%d",&i);
student ob[i],*ptr;
ptr=ob;
enterinfo(ptr,i);
Display(ptr,i);

}




void enterinfo(student *e,int y) 
{
    char CONT='y';


        for (int j=0;j<y && (CONT=='y' || CONT=='Y');j++)
        {   
            printf("Enter Students First Name: ");
            scanf("%s",e->Fname);
            printf("Enter Students Last Name: ");
            scanf("%s",e->Lname);
            printf("Enter Roll number: ");
            scanf("%d",e->reg_no);
            printf("Enter class: ");
            scanf("%d",e->Class);
            printf("Enter Section: ");
            scanf("%d",e->sec);

            printf("\n\n\n\n Do you want to enter more?  : ");
            scanf("%c",&CONT);

        }

}






void Display(student *e,int y) 
{
     char CONT='y';


        for (int j=0;j<y;j++)
        {   
            printf("Students name : %s %s",e->Fname,e->Lname);

            printf("Enter Roll number: %d",e->reg_no);

            printf("class: %d",e->Class);

            printf("Enter Section: %d",e->sec);

        }

}
Was it helpful?

Solution

I've made the following changes to your code and it started working for me:

  • char Fname[]; --> char Fname[100];
  • char Lname[]; --> char Lname[100];
  • char sec; --> int sec; This is needed for scanf.
  • scanf("%d",e->reg_no); --> scanf("%d",&e->reg_no);
  • scanf("%d",e->Class); --> scanf("%d",&e->Class);
  • scanf("%d",e->sec); --> scanf("%d",&e->sec);
  • adding \n to the end of printf strings in Display

Please note that scanf("%s", ...) is insecure and it can cause a crash the input string is longer than the array size you're reading it to, i.e. if the user types a name of at least 100 bytes.

Please note that you should always check the return value of scanf, and abort early on error (i.e. if it doesn't return 1 in your case).

Please note that in C++ the istream methods (http://en.cppreference.com/w/cpp/header/istream) provide a safer way to read the input.

OTHER TIPS

Here:

scanf("%d",e->reg_no);

you should insert a '&' symbol before e->reg_no. But I can see many other problems once you solve this...

You can use below code, it will run on both linux & windows. Your program halts because of:



    scanf("%d",&e->reg_no);
    printf("Enter class: ");
    scanf("%d",&e->Class);
    printf("Enter Section: ");
    scanf("%d",&e->sec);


you did not use & which is must for int, char, float data types as it is used as reference of the variable.



    #include


    struct student{
    char Fname[30];
    char Lname[30];
    int reg_no;
    int Class;
    char sec[5];
    };

    void enterinfo(student *,int);
    void Display(student *,int);

    int main()
    {
    int i;

    printf("\t\t\t Enter student's information\n\n\n\n");
    printf("How many students are there in you're school: ");
    scanf("%d",&i);
    student * ob = new student[i];
    enterinfo(ob,i);
    Display(ob,i);

    }




    void enterinfo(student *e,int y) 
    {
        char CONT='y';


            for (int j=0;jFname);
                printf("Enter Students Last Name: ");
                scanf("%s",e->Lname);
                printf("Enter Roll number: ");
                scanf("%d",&e->reg_no);
                printf("Enter class: ");
                scanf("%d",&e->Class);
                printf("Enter Section: ");
                scanf("%s",e->sec);

                getchar();//to eat newline/ enter char of previous statement

                printf("\n\n\n\n Do you want to enter more?  : ");
                scanf("%c",&CONT);

            }

    }






    void Display(student *e,int y) 
    {
            for (int j=0;jFname,e->Lname);

                printf("\nEnter Roll number: %d",e->reg_no);

                printf("\nclass: %d",e->Class);

                printf("\nEnter Section: %s\n",e->sec);

            }

    }


Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top