Thread: Checking if the input is a character ?

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

    Question Checking if the input is a character ?

    I wrote this code:

    Code:
    #include "includes.h"
    
    int main(void) {
    
      int number;
      char char1;
    
      printf("%s", "Enter a number: ");
    
      if (scanf(" %d", &number) != 1)
      {
        printf("%s\n", "You didnt enter a number!");
        return -1;
      }
      else
      {
        printf("%s", "Enter a character: ");
    
        if (scanf(" %c", &char1) != 1)
        {
          printf("%s\n", "You didnt enter a character!");
          return -1;
        }
        else
        {
          printf("%s%d%s%c\n", "The number you entered is ", number, " and the character is ", char1);
        }
    
      }
    
      return 0;
    }
    I wanna check if the character i enter is a character or no , i mean char1 variable , the check for the number is fine , but the scanf in the character input always returns 1 whether i enter a single character or various ones.

    Thanks for help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kommand
    but the scanf in the character input always returns 1 whether i enter a single character or various ones.
    That's because you're always reading the first character, with the remaining characters left in the input buffer. If you want to check that the user entered exactly one character, you should read the entire line as a string and check that the string is of length 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2020
    Posts
    4
    Thank you so much for help, i wrote it like this:


    Code:
    #include "includes.h"
    
    int main(void) {
    
      int number1;
      char string1[50];
    
      printf("%s", "Enter a number: ");
    
      if (scanf(" %d", &number1) != 1)
      {
        printf("%s\n", "You didnt enter a number!");
        return -1;
      }
      else
      {
        printf("%s", "Enter a character: ");
        scanf(" %s", string1);
    
        if (strlen(string1) != 1)
        {
          printf("%s\n", "You didnt enter a character!");
          return -1;
        }
        else
        {
          printf("%s%d%s%s\n", "The number you entered is ", number1, " and the character is ", string1);
        }
    
      }
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    114
    I don't know if this is a standard, but most people would write this:
    Code:
    printf("%s%d%s%s\n", "The number you entered is ", number1, " and the character is ", string1);
    Like this:
    Code:
    printf("The number you entered is %d and the character is %s\n", number1, string1);
    Just use the arguments for variables. Text goes in the first part.

  5. #5
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by Yonut View Post
    I don't know if this is a standard, but most people would write this:
    Code:
    printf("%s%d%s%s\n", "The number you entered is ", number1, " and the character is ", string1);
    Like this:
    Code:
    printf("The number you entered is %d and the character is %s\n", number1, string1);
    Just use the arguments for variables. Text goes in the first part.
    Maybe, but the other way is probably less error prone.

    Code:
    printf("The number you entered is %d and the character is %s\n", string1, number1);
    Whoops.

    Code:
    printf("%s%d%s%s\n", "The number you entered is ", string1, " and the character is ", number1);
    The mistake is now way more obvious. Comparing the format string with the arguments is a little easier too.

    Verbose, but still good form.

  6. #6
    Registered User
    Join Date
    Jul 2020
    Posts
    4
    Quote Originally Posted by Yonut View Post
    I don't know if this is a standard, but most people would write this:
    Code:
    printf("%s%d%s%s\n", "The number you entered is ", number1, " and the character is ", string1);
    Like this:
    Code:
    printf("The number you entered is %d and the character is %s\n", number1, string1);
    Just use the arguments for variables. Text goes in the first part.

    I don't know if its a standard or no , but i think its neater to make the format specifiers is the first argument and values in the second argument. I just feel it neater and more organized.

    It may be confusing but the person adapts.

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    114
    Quote Originally Posted by Sir Galahad View Post
    Maybe, but the other way is probably less error prone.

    Code:
    printf("The number you entered is %d and the character is %s\n", string1, number1);
    The compiler will let you know.

    I just think sorting through a bunch of strings to find a variable name seems harder in your expample than to just look for a percent sign (%) which isn't a usual character (and therefore easier to spot), in the inital string.

    But to each his own I guess. Just making sure they're aware of another way.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kommand
    Thank you so much for help, i wrote it like this:
    You're welcome. You should be aware that this makes your code vulnerable to buffer overflow:
    Code:
    scanf(" %s", string1);
    To avoid buffer overflow, you should specify the field width:
    Code:
    scanf(" %49s", string1);
    But it is possible for the user to enter "a cat". In that case, you'll only read "a", and conclude that you've read a character, and all is well. If you're perfectly happy with this, then that's great. If you're not happy with this, then you would need to take an alternative approach of reading the entire line entered.

    Personally, when it comes to reading line-based (interactive) input, I'm in favour of the fgets+sscanf approach, i.e., you always read a line and then parse it, rather than sometimes try to use formatted input on the input stream and sometimes not.

    Quote Originally Posted by Sir Galahad
    The mistake is now way more obvious.
    But consider:
    Code:
    printf("The number you entered is %s and the character is %d\n", number1, string1);
    versus:
    Code:
    printf("%s%s%d%s\n", "The number you entered is ", number1, " and the character is ", string1);
    Now, the mistake is way more obvious in the first example, i.e., it is not true that "the other way is probably less error prone".

    Anyway, as Yonut pointed out, "The compiler will let you know." so it is more of a subjective style preference.
    Last edited by laserlight; 09-11-2020 at 11:22 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int isNumber( char *string ) {
        for (int i=0; i<strlen(string); i++) {
            if ( (int)string[i] >= 48 && (int)string[i] <= 57 ) {
              // valid digit ;
            } else {
              if ( string[i] != '.' ) return 0;
            }
        }
        return 1;
    }
    
    int main( int argc, char *argv[] ) {
        
        char inputText[256]; printf( ".:: " );
        
        scanf( "%s", inputText );
    
        if ( isNumber( inputText ) ) {
            printf( "number." );
        } else {
            printf( "text." );
        }
    
        return 0;
    }
    "without goto we would be wtf'd"

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    That would also return true if you typed in a numeric IP address.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Quote Originally Posted by Salem View Post
    That would also return true if you typed in a numeric IP address.
    Code:
    int isNumber( char *string ) {
        int oneDot = 0;
        for (int i=0; i<strlen(string); i++) {
            if ( (int)string[i] >= 48 && (int)string[i] <= 57 ) {
              // valid digit ;
            } else {
              if ( string[i] != '.' ) {
                  return 0;
              } else {              
                  if (oneDot++ > 0) return 0;
              }
            }
        }
        return 1;
    }
    not anymore.
    "without goto we would be wtf'd"

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf("%d\n", isNumber("."));
    Also returns 1.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Quote Originally Posted by Salem View Post
    > printf("%d\n", isNumber("."));
    Also returns 1.
    Code:
    int isNumber( char *string ) {
        int oneDot = 0;
        for (int i=0; i<strlen(string); i++) {
            if ( (int)string[i] >= 48 && (int)string[i] <= 57 ) {
              // valid digit ;
            } else {
              if ( string[i] != '.' ) {
                  return 0;
              } else {              
                  if (oneDot++ > 0 || i < 1) {
                      return 0;
                  }
              }
            }
        }
        return 1;
    }
    not anymore.
    "without goto we would be wtf'd"

  14. #14
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    preemptive fix...

    Code:
    int isNumber( char *string ) {
        int oneDot = 0;
        for (int i=0; i<strlen(string); i++) {
            if ( (int)string[i] >= 48 && (int)string[i] <= 57 ) {
              // valid digit ;
            } else {
              if ( string[i] != '.' ) {
                  return 0;
              } else {              
                  if (oneDot++ > 0 || (i < 1 && strlen(string) == 1)) {
                      return 0;
                  }
              }
            }
        }
        return 1;
    }
    anything else ?
    "without goto we would be wtf'd"

  15. #15
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338

    Post

    This one includes integer fractions ...

    Code:
    int isNumber( char *string ) {
        int oneDot = 0, stringLength = strlen(string), ii;
        for (int i=0; i<stringLength; i++) {
            ii = (int)string[i];
            if ( !(ii >= 48 && ii <= 57) ) {
              if ( ii != '.' && ii != '/') {
                  return 0;
              } else {              
                  if (oneDot++ > 0 || (i < 1 && stringLength == 1)) {
                      return 0;
                  }
              }
            }
        }
        return 1;
    }
    Last edited by Structure; 09-13-2020 at 06:06 AM.
    "without goto we would be wtf'd"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help checking a character in a structure
    By WranglerYJ in forum C Programming
    Replies: 12
    Last Post: 05-12-2012, 09:05 AM
  2. checking the input is character or not
    By sal in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2012, 06:13 AM
  3. Checking if a character is a '\'
    By serg_yegi in forum C Programming
    Replies: 18
    Last Post: 03-04-2010, 05:42 AM
  4. Checking value of one character in array
    By System_159 in forum C Programming
    Replies: 7
    Last Post: 01-17-2008, 05:05 PM
  5. Checking validity of input character
    By yank in forum C Programming
    Replies: 8
    Last Post: 04-16-2003, 02:42 AM

Tags for this Thread