using Struct-student assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • withu4ever
    New Member
    • Dec 2007
    • 4

    #1

    using Struct-student assignment

    I learned how to use struct when I try to write a program i face some misstakes which I cant correct

    This is the program:
    -------------------------------------------------------
    [code=c]
    #include<stdio. h>
    #include<string .h>

    main()
    {int nu,i; /* nu is number of student */

    struct record{ char id[8];
    char name[35];
    float gpa; }student;


    printf("Enter the number of students");
    scanf("%d",&nu) ;
    for(i=0;i<nu;i+ +)
    { printf("Enter the ID of the student");
    scanf("%d",%stu dent[i].id);
    printf("Enter the name of student");
    gets(student[i].name);
    printf("Enter the GPA");
    scan("%f",stude nt[i].gpa);}
    }

    [/code]
    ----------------------------------------
    It cannot be compiled completly.

    May any one plz help me and see where is the mistakes??
    Last edited by sicarie; Dec 30 '07, 01:11 AM. Reason: Code tags are [code=c] your code here, and [/code] at the end. Please use them.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You can't define the struct inside the main function. It has to be done before the main.

    Also, in C/C++, the main function returns an int, so you need to declare it as int main(), and return a value at the end of the function, conventionally 0.

    Eg:
    [CODE=c]
    struct a{
    int b;
    }

    int main(){
    ...
    return 0;
    }[/code]
    Last edited by sicarie; Dec 30 '07, 01:10 AM. Reason: Don't forget the [/code] at the end of your code. (Thanks again for using code tags!)

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      It's OK to declare the struct inside main().

      The big problem is you have one student and you are using it like it was an array of students.

      Comment

      • kalavnagarjuna
        New Member
        • Jan 2008
        • 2

        #4
        I did not get the editor u are doing. For better values it is recommended to do it in DOS mode but not in WINDOWS mode.
        U can make the structure either inside or outside of the main().But u need to make the declaration as
        1.
        struct {
        ----------------;
        }student;
        (OR)
        2.
        struct {
        -----------------;
        };
        now inside the main(),u call the structure as the given code.
        struct record student;

        This is the program:
        -------------------------------------------------------
        [code=c]
        #include<stdio. h>
        #include<string .h>

        main()
        {int nu,i; /* nu is number of student */

        struct record
        { char id[8];
        char name[35];
        float gpa;
        }student;/* here student is the member of the structure 'record' .
        Even u can make an array for the member list also ,but u need to make another loop for it
        */

        printf("Enter the number of students");
        scanf("%d",&nu) ;
        for(i=0;i<nu;i+ +)
        { printf("Enter the ID of the student");
        /*scanf("%d",%st udent[i].id);*/
        scanf("%d",%stu dent.id[i]);
        /* now u can make the difference because in the
        above line u have given looping to the member of the structure.Don't do like that.that's why u might be getting problem in the compiling process */

        printf("Enter the name of student");
        gets(student.na me[i]);
        printf("Enter the GPA");
        scan("%f",stude nt.gpa[i]);
        }
        }

        Now u can get this program compiled.Any problem
        contact me: kalavnagarjuna@ yahoo.co.in

        Comment

        • kalavnagarjuna
          New Member
          • Jan 2008
          • 2

          #5
          [QUOTE=kalavnaga rjuna] I did not get the editor u are doing. For better values it is recommended to do it in DOS mode but not in WINDOWS mode.
          U can make the structure either inside or outside of the main().But u need to make the declaration as
          1.
          struct {
          ----------------;
          }student;
          (OR)
          2.
          struct {
          -----------------;
          };
          now inside the main(),u call the structure as the given code.
          struct record student;

          This

          Comment

          Working...