Thread: fgets() and EOF

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    fgets() and EOF

    Hello again,

    Is there a way of turning my code below into a loop which terminates when the user types in "quit"? The usual methods don't seem to work - Pelles C keeps throwing errors about mystring being interpreted as an int? (It's clearly declared as a char array):

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 100
    
    void reverse (char s[]);
    
    int main (int argc, char *argv[]) 
    {
    
          char mystring[MAX];
    
          printf("Please enter a string of characters.\nEnter quit when done.\n");
          
          fgets(mystring,MAX,stdin);
    
          reverse(mystring);
    
    
    return 0;
    }
    
    void reverse (char mystring[])
    {
    
    	int x;
     x=strlen(mystring);
    
          for(x=strlen(mystring); x>=0; x--) 
    		{
              printf("%c", mystring[x]);
    		}
    printf("\n");
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Use strcmp(). Don't forget to fgets() will include '\n' char if there's one.
    Post which line. and error message.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by twazzler View Post
    Hello again,

    Is there a way of turning my code below into a loop which terminates when the user types in "quit"? The usual methods don't seem to work - Pelles C keeps throwing errors about mystring being interpreted as an int? (It's clearly declared as a char array):

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 100
    
    void reverse (char s[]);
    
    int main (int argc, char *argv[]) 
    {
    
          char mystring[MAX];
    
          printf("Please enter a string of characters.\nEnter quit when done.\n");
          
          fgets(mystring,MAX,stdin);
    
          reverse(mystring);
    
    
    return 0;
    }
    
    void reverse (char mystring[])
    {
    
    	int x;
     x=strlen(mystring);
    
          for(x=strlen(mystring); x>=0; x--) 
    		{
              printf("%c", mystring[x]);
    		}
    printf("\n");
    }
    I just compiled and ran your code in Pelles C (v6.50 rc4) and it worked just fine.

    The only change I'd make is to remove the x = strlen(mystring); above the for loop... but that's just housekeeping really.

    For your quit when done thing...

    Code:
    do 
      {  
        //get user input as you are
      }  while (stricmp(mystring,"QUIT"));
    should do the trick...
    Last edited by CommonTater; 05-01-2011 at 10:01 PM.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    Hi Tater,

    Thanks for replying. Do you mean like this?
    Code:
          char mystring[MAX];
    
          printf("Please enter a string of characters.\nEnter quit when done.\n");
          
            
    	fgets(mystring,MAX,stdin);
    	while (strcmp(mystring,"QUIT"));
    	
    
          reverse(mystring);
    
    
    return 0;
    }
    As now it just quits whatever is inputted

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by twazzler View Post
    Hi Tater,

    Thanks for replying. Do you mean like this?
    No i did not mean like that.

    Look what I wrote and what you wrote... see any resemblence whatsoever?

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    You've lost me, sorry...

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Check documentation of strcmp!!!
    It returns zero if two strings are equal.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by twazzler View Post
    You've lost me, sorry...
    Like this... with a minor revision for the newline from fgets...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 100
    
    void reverse (char s[]);
    
    int main (int argc, char *argv[]) 
    {
    
          char mystring[MAX];
    
        do
         {      
           printf("Please enter a string of characters.\nEnter quit when done.\n");
          
          fgets(mystring,MAX,stdin);
    
          reverse(mystring);
          } while (_stricmp(mystring,"QUIT\n"));
    
    return 0;
    }
    
    void reverse (char mystring[])
    {
    
    	int x;
     x=strlen(mystring);
    
          for(x=strlen(mystring); x>=0; x--) 
    		{
              printf("%c", mystring[x]);
    		}
    printf("\n");
    }
    Now... open the Pelles C help file click the index tab ... type in stricmp and press Enter.
    Next put the flashing text cursor on the word stricmp and press F1 ...
    Pelles C is probably the best documented compiler and library I've ever seen.
    Last edited by CommonTater; 05-01-2011 at 10:56 PM.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Don't forget that while strcmp() is standard function , stricmp or _stricmp is not.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    Don't forget that while strcmp() is standard function , stricmp or _stricmp is not.
    Of course... that's why I suggested he look it up in the help file... In Pelles C the help file very clearly identifies non-standard functions. If there's nothin in his task saying "Standard functions only" I don't see a problem using it.

    The advantage of _strichr() in this situation is that it is not case sensitive so any permutation of quit, Quit QUIT, qUit, etc. will work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets help
    By m1cha3l in forum C Programming
    Replies: 5
    Last Post: 12-01-2009, 01:56 PM
  2. Help using fgets()
    By ICool in forum C Programming
    Replies: 7
    Last Post: 09-10-2007, 06:24 AM
  3. fgets
    By CaN Opner in forum C Programming
    Replies: 8
    Last Post: 01-14-2003, 11:58 PM
  4. fgets
    By Inept Pig in forum C Programming
    Replies: 15
    Last Post: 09-12-2002, 08:27 AM
  5. xor (was fgets)
    By Brian in forum C Programming
    Replies: 3
    Last Post: 08-18-2002, 08:13 PM