How to test against aplhabet input ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moroccanplaya
    New Member
    • Jan 2011
    • 80

    #1

    How to test against aplhabet input ?

    i have this to check for numeric inputs, which returns then menu if a number is entered less or grater than 5 but i dont know how to do this with alphabets ? for any alphabet or any other charterer apart from numbers should return the menu

    Code:
    while (select < 1 || select > 5) ;
    	        return select;
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Look up the functions in ctype.h such as isalpha

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Are you asking how to work with character constants, when select is a char?

      You could do this
      Code:
      if ((select < '1') || (select > '5'))
      but this code assumes that the character codes for the digits 1-5 are contiguous. That is true for ASCII, but it may not be true for other character encodings.

      A fully portable solution would be something more like this:
      Code:
      while (1) {
         switch (select) {
            case '1':
            case '2':
            ...
            case '5':
               break;
            default:
               return select;
            }
         ...
         }
      The switch could be replaced with an if statement.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        but this code assumes that the character codes for the digits 1-5 are contiguous. That is true for ASCII, but it may not be true for other character encodings.
        Actually the C standard requires that character constants for the digits be contiguous or rather that '2' - '1' = 1, '3' - '2' = 1 etc.

        It is the letters you have to beware of, the standard makes no requirement on contiguity on the alphabet so 'b' - 'a' does not necessarily equal 1.

        Comment

        • moroccanplaya
          New Member
          • Jan 2011
          • 80

          #5
          the first one works but for example if i enter 1p it executes 1 and automatically executes p as well because i have when someone enters 1 it lets the user create a file which prompts them to enter the file name they want which would automatically be p. i hope that made sense

          the example with the switch does not work at all for me it when a user enters a number it just goes strait to default and returns select

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Code:
            switch (select) { 
                  case '1': 
                  case '2':
            A '1' is the ASCII character.
            A 1 is the integer 1.
            You cannot enter the character for 1. It is the integer you enter.

            The loop should be:

            Code:
            switch (select) { 
                  case 1: 
                  case 2: 
            etc...

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              "... but i dont know how to do this with alphabets ? for any alphabet or any other charterer apart from numbers should return the menu"
              How do you want to use non-numeric values?

              Comment

              Working...