Thread: Enter data in struct

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    3

    Enter data in struct

    This function should accept student details from the user and store them in a structure. I'm using a temp array to hold the reg number as I'm going to perform a validation test on it later.

    Code:
    void add_student(studentRecord student[],int i){
                    system("cls");
    	printf("\nEnter Registration number :");
    	gets(temp);
    	strcpy(student[i].regNumber,temp);
    	printf("\nEnter Surname :");
    	gets(student[i].surname);
    	printf("\nEnter Forename :");
    	gets(student[i].forename);
    	printf("\nEnter number of credits :");
                    scanf("%d",&student[i].credits);
                    printf("Press any key to continue.\n");
    
    }
    When I display the structure I have some strange output i.e.
    *** Input***
    reg No : 1234
    Surname : smith
    Forename: john
    Credits : 12

    *** Actual output ***
    reg No: 1234smith
    Surname: smith
    forename: john
    credits: 6684136

    Can any one help? I cant see why the Surname is being appended onto the Reg No or why the Credits are being displayed as an address ( I think ??!!)

    if its any help this is the function I'm using to display the structure, for testing:
    Code:
    void display(studentRecord student[],int i){
    	printf("\nReg %s\n",student[i].regNumber);
    	printf("\nsurname %s\n",student[i].surname);
    	printf("\nforename  %s\n",student[i].forename);
    	printf("\ncredit  %d\n",student[i].credits);
    	getch();
    }

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    You're inputting the information backwards, it would be best to use strcpy() for copying the names in, and just fgets() with an atoi() or atof() function to convert. Then just assign those values returned to the structure members. Think strcpy() for strings and fgets() (atoi/atof) for integer/float values.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    As to your first error: How many bytes is regNumber? I would wager it's only 4 bytes -- but "1234" takes up *five* bytes because it has to be null terminated. If it has to be a pure number you're better off using an integer and atoi() to get the number.

    Show us how the struct itself is defined.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    3
    thanks for taking a look at this guys...
    I tried the strcpy() but still get the same result.
    here is the struct :
    Code:
    typedef struct {
    	char regNumber[4];
    	char surname[20];
    	char forename[15];
    	int credits;
    }studentRecord;
    I fixed the second problem (displaying the int credits) but still cant get the regNumber / surname problem fixed.

    the regNumber has to be alphanumeric as it may contain a letter.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The answer is as described. Your first array is not long enough to store the null terminator. As such, when displaying a string, it just runs along merrily until it encounters an actual null.

    Try using strncpy instead, and specify "array size - 1" for the third argument.

    Quzah
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    3
    AAAhhhhhh!
    working fine now. thanks very much guys, much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 12:25 PM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM