Thread: Input Checking

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    7

    Input Checking

    Hello, I'm rather new with C...and I'm trying to make a simple program to check if a user typed "yes" or "no" into the terminal...Then if they typed "yes" one printf comes up, while if they typed "no" a different one comes up.

    If someone could point me in the right direction I'd be very grateful.

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    16
    the code below is just a hint it's not complete just to give you the idea that I came up with
    Code:
    #include <string.h> 
    /*contains a lot of string manipulation functions; necessary in this case to use strcmp(str1,str2)*/
    
    #define MAX_CMD 3
    
    char command[MAX_CMD+1];
    
    printf("Insert command: \n");
    scanf("%s", command); 
    /* %s is the indicator for the string, command is the pointer to the string*/
    
    if(strcmp(command,'yes')==0)
    {
    printf("bla bla");
    }
    else if(strcmp(comand,'no')==0)
    {
    printf("blablablabla");
    }
    else
    {
    printf("invalid code!");
    }
    the function strcmp(str1,str2) returns:
    - zero if the to string are the same
    - negative value if str1<str2 (alphabetically)
    - positive value if str1>str2

    I hope this helps you!!

    Andryjohn.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    That worked perfect! You explained it so well too, thank you so much for the help.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by andryjohn View Post
    the code below is just a hint it's not complete just to give you the idea that I came up with
    Code:
    #include <string.h> 
    /*contains a lot of string manipulation functions; necessary in this case to use strcmp(str1,str2)*/
    
    #define MAX_CMD 3
    
    char command[MAX_CMD+1];
    
    printf("Insert command: \n");
    scanf("%s", command); 
    /* %s is the indicator for the string, command is the pointer to the string*/
    
    if(strcmp(command,'yes')==0)
    {
    printf("bla bla");
    }
    else if(strcmp(comand,'no')==0)
    {
    printf("blablablabla");
    }
    else
    {
    printf("invalid code!");
    }
    the function strcmp(str1,str2) returns:
    - zero if the to string are the same
    - negative value if str1<str2 (alphabetically)
    - positive value if str1>str2

    I hope this helps you!!

    Andryjohn.
    yes & no are strings so they must be enclosed within double quotes.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by BEN10 View Post
    yes & no are strings so they must be enclosed within double quotes.
    Yep, I figured that out for myself when ran into that error. Besides that though, perfect. (I should have said that was the only problem with it, but it's 4:40 AM so...I give myself a bit of an excuse lol)

    But once again, thank you.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    16
    Yes, you're damn right BEN10! my mistake but I was convinced when I wrote above... (still don't know why... :confused)

    any way the correct code is
    [CODE]
    if(strcmp(command,"yes")==0)
    ...
    if(strcmp(command,"no")==0)
    [\CODE]

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) Declare a buffer
    2) Use fgets to read input into buffer
    3) Use strncmp to test the input

    Just keep in mind that fgets will leave a newline in the buffer, which will most likely botch the comparison. You can use strstr or strchr to locate the newline in the buffer, and remove it, if present.

    Just for fun, here's a fgets-like function that discards the newline:

    Code:
    char* getline( char* buf, size_t max, FILE* inf )
    {
    	int
    		chr;
    	char*
    		ptr = buf;
    	if( !max-- )
    		return NULL;		
    	if( feof( inf ) )
    		buf = NULL;
    	else while( max-- && ( chr = fgetc( inf ) ) != EOF && chr != '\n' )
    		*ptr++ = chr;
    	if( ferror( inf ) )
    	{
    		ptr = buf;
    		buf = NULL;
    	}
    	*ptr = 0;
    	return buf;
    }
    EDIT:
    Damn, I replied way too slow.
    Last edited by Sebastiani; 09-05-2009 at 04:36 AM. Reason: improved error-handling

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by Sebastiani View Post
    1) Declare a buffer
    2) Use fgets to read input into buffer
    3) Use strncmp to test the input

    Just keep in mind that fgets will leave a newline in the buffer, which will most likely botch the comparison. You can use strstr or strchr to locate the newline in the buffer, and remove it, if present.

    Just for fun, here's a fgets-like function that discards the newline:

    Code:
    char* getline( char* buf, size_t max, FILE* inf )
    {
    	int
    		chr;
    	char*
    		ptr = buf;
    	if( ferror( inf ) || feof( inf ) || !max-- )
    		return NULL;
    	while( max-- && ( chr = fgetc( inf ) ) != EOF && chr != '\n' )
    		*ptr++ = chr;
    	if( ferror( inf ) )
    	{
    		ptr = buf;
    		buf = NULL;
    	}
    	*ptr = 0;
    	return buf;
    }
    EDIT:
    Damn, I replied way too slow.
    I just took care of the newline problem this way...

    Code:
    void ReplaceReturn(char string[1000])
    {
    	int stringLength;
    	
    	stringLength = strlen(string);
    	
    	string[stringLength - 1] = 0;
    }
    Then I just called the function, and sent it the string I wanted it to get rid of the newline for me. Works perfect.

    Sense I knew that fgets added a newline at the end of the string, I just got rid of that byte of data and replaced it with a ending 0. (I think I explained that correctly) And if I find myself doing that allot(this is so far the first time) I'll just make a function to do it. My way seems a bit easier and less confusing, though your way might be quicker or better in some way I don't know. lol Please explain why your way is better if it is, I'd like to learn why.
    Last edited by SamGray; 09-05-2009 at 04:15 AM.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, there are a few things to keep in mind with that approach: fgets will only place a newline in the input buffer if present (remember: it operates on files, as well) and only if there is room for it. Also, strlen could be zero, in which case subtracting one from it would cause a buffer underflow. So basically:

    Code:
    int len = strlen( buffer );
    if( len != 0 && buffer[ len - 1 ] == '\n' )
          buffer[ len - 1 ] = 0;
    // ...or maybe...
    char* cr = strstr( buffer, "\n" );
    if( cr )
          *cr = 0;
    ...something along those lines.

    And as far as the function I posted, as I said, it was just for fun really. AFAIK it works mostly identical to fgets (aside from the newline discard, and I'm not sure if fgets sets the first character to zero on error, but anyway, more or less the same).

    EDIT:
    Actually, come to think of it, the function I posted doesn't always set the first character to zero on error or in the event that EOF was reached before attempting to read data into the buffer. Fixed.
    Last edited by Sebastiani; 09-05-2009 at 04:39 AM.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Ah, ok now I get it. Thank you for the explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Checking input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-26-2002, 03:06 AM
  4. Checking input
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-10-2002, 09:00 AM
  5. checking if input is a number ?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2001, 09:07 PM