Thread: student structure

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    student structure

    this is suppose to read the following information from the keyboard for a set of students:
    Name
    Age
    GPA

    this is how far i got, i made lines to the information, i just cant figure out, any help would be helpful, any input or ideas at all.
    thanks


    Code:
    typedef struct
    {
    	char Name[50];
    	_int age_;
    	_double GPA_;
    }Students;
    
    int main()
    {
    	FILE *fpout;
    
    	fpout = ______________________;
    
    	if(__________)
    	{
    		printf("file output.txt can not be opened!");
    		return(100);
    	}
    
    	Students ______________;
    
    	for(int i = 1; i < 4; i++)
    	{
    		printf("Please enter the student's name:\n");
    		scanf("_%d_", _&name__);
    		printf("Please enter the student's Age:\n");
    		scanf("%d", _&age_);
    		printf("Please enter the student's GPA:\n");
    		scanf("_%d_", _&GPA_);
    		fprintf(fpout, "__Student %d %d%d", name, age, GPA__", 
    			_________________________________, students[i].GPA);
    	}
    
    	return 0;
    }

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    get rid of the _'s

    int not _int
    double not _double

    Code:
    #include <stdio.h>
    typedef struct
    {
    	char Name[50];
    	int age;
    	double GPA;
    }Students;
    
    int main()
    {
    	FILE *fpout;
    	char ch;
    	fpout = fopen("myfile.txt","w");
    
    	if(!fpout)
    	{
    		printf("file output.txt can not be opened!");
    		return(0);
    	}
    
    	Students myStudents;
    
    	for(int i = 1; i < 4; i++)
    	{
    		
    		
    		myStudents.GPA=0;
    		printf("Please enter the student's name:\n");
    		scanf("%s", &myStudents.Name);
    
    		printf("Please enter the student's Age:\n");
    		scanf("%d", &myStudents.age);
    		printf("Please enter the student's GPA:\n");
    		scanf("%f", &myStudents.GPA);
    		fprintf(fpout, "  Student %s %d %f\n", myStudents.Name, myStudents.age, myStudents.GPA);
    	}
    	fclose(fpout);
    	return 0;
    }
    For some reason the GPA thingy doesn't work... that's really strange..

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    scanf("%s", &myStudents.Name);

    should be

    scanf("%s", myStudents.Name);

    you don't use the & with an array, such as a string.

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    oops, forgot to change that.. but it worked on mine...


    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM