Thread: beginning structure prob.

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    1

    Question beginning structure prob.

    doing a hw assignment using a structure. i have to create a db of students. ask user to determine how many students (max 10) to enter. my problem is my code works ok, but im overwriting my data everytime the user enters new info for each student. so when i print out the info, it only prints the last entry the user inputed. i know this is something fairly simple but i just cant get it, any ideas? here is my code:
    Code:
    #include <stdio.h>
    
    struct student
    {
    	char  name[20];
    	char  city[20];
    	int   id;
    	float units;
    	char  year[10];   //freshman, sophomore, junior, senior
    }x;
    
    
    int main()
    {
    	int i, num;
    	
    	printf("Enter number of students to input in database: ");
    	scanf("%d", &num);
    
    	
    	for(i = 0; i < num; i++)
    	{
    		printf("Enter students name:\n");
    		scanf("%s", x.name);
    		printf("Enter students city:\n");
    		scanf("%s", x.city);
    		printf("Enter students I.D.:\n");
    		scanf("%d", &x.id);
    		printf("Enter number of units:\n");
    		scanf("%f", &x.units);
    		printf("Enter year of student:\n");
    		scanf("%s", x.year);
    	}
    
    	
    	printf("%s\n", x.name);
    	printf("%s\n", x.city);
    	printf("%d\n", x.id);
    	printf("%g\n", x.units);
    	printf("%s\n", x.year);
    
    
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    9

    use array of structures

    you are gonna need a array of structures (size=10) to hold information about all students.
    You would then iterate 10 times in a for loop to take input and then again iterate 10 times to print output.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    8
    Maybe someone would like to help me with this and tlak about it over aol instant messanger or something else if possible... my aol screen name is: BladeBoss2

    I know I'm just a beginner but we all were beginners once...

    Thanks, Blade

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by BladeBoss2
    ..... I know I'm just a beginner but we all were beginners once...
    ... which is why it is often better to discuss on here, that way others learn from the conversation. That is unless it lasts a lifetime, in which case it'd be better to go and read up then come back with more specific questions when you have a basic understanding of C.

    Anything in particular you want to ask or have explained?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM