Thread: having troubles with gets and EOF

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    1

    having troubles with gets and EOF

    Hi guys,
    I'm going to write a program which i had to get at most 200 student informations and then sort them in some ways, getting input will continue until user enter EOF or 200 students information entered.
    i wrote a struct for students contain firstName, lastName and studentCode.
    firstName maximum size is 20 chars.
    lastName maximum size is 50 chars.
    studentCode maximum size is 7 chars.
    and a flag to check if that segment of students array is active or not.
    here is what i wrote:

    Code:
    #define TRUE 1
    #define FALSE 0
    #include<stdio.h>
    #include<stdio.h>
    struct Student
    {
    charfirstName[20];
    charlastName[50];
    charstudentCode[7];
    int flag;
    };
    void studentInitializeser (struct Student* student);
    
    int main(void)
    { 
    struct Student students[200];
    int i=0;
     for(i=0;i<200;i++)
    studentInitializeser(&students[i]);
    i=0;
    gets(students[0].firstName);
     while(students[i].firstName!=EOF)
    {
    gets(students[i].lastName);
    gets(students[i].studentCode);
    students[i].
    flag=TRUE;
    i++;
    gets(students[i].firstName);
    }
    return0;
    }
    
    void studentInitializeser(struct Student* student)
    {
    student->flag=FALSE;
    inti=0;
    for(i=0;i<20;i++)
    student->firstName[i]=" ";
    i=0;
    for(i=0;i<50;i++)
    student->lastName[i]=" ";
    i=0;
    for(i=0;i<7;i++)
    student->studentCode[i]=-1;
    }
    the problem i have, is working with EOF,
    i know that gets function return NULL if EOF entered, but i tried that too, its not working ^Z(i'm using Ubuntu) but program still waiting for input.
    i attached a picture of running program.
    having troubles with gets and EOF-screenshot-2015-12-10-16-43-48-jpg

    thanks in advance for your helps.
    Last edited by amirtork; 12-10-2015 at 07:15 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Exactly, it returns NULL. What you do is comparing a static array with NULL, which can never be equal.

    Also, please take a look at this link:
    Why gets() is bad
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted and answered here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RPG troubles
    By MartinThatcher in forum Game Programming
    Replies: 3
    Last Post: 10-02-2009, 08:57 PM
  2. cin.get() Troubles
    By yukapuka in forum C++ Programming
    Replies: 19
    Last Post: 06-04-2008, 01:30 PM
  3. tic tac toe troubles...
    By mkylman in forum Game Programming
    Replies: 8
    Last Post: 09-16-2006, 06:44 PM
  4. GTK troubles...again...
    By cornholio in forum Linux Programming
    Replies: 4
    Last Post: 01-16-2006, 01:37 AM
  5. .AVI troubles
    By Untitled1 in forum Tech Board
    Replies: 6
    Last Post: 10-26-2003, 10:39 AM

Tags for this Thread