Function for not accepting letters?

Sharky Forums


Results 1 to 4 of 4

Thread: Function for not accepting letters?

  1. #1
    Expensive Sushi
    Join Date
    Jun 2001
    Location
    IL
    Posts
    39

    Talking Function for not accepting letters?

    Ok here is my question. I wrote a very simple program and I was wondering if there is a function that will not accept a letter as a value.
    here is the code.

    #include <iostream.h>
    int main() {
    int x;
    cout << " Please enter a number : " ;
    cin >> x;

    if (x>10)
    cout << x << " is larger than 10.";
    else if(x<10)
    cout << x << " is smaller than 10:";
    else if(x==0)
    cout <<x << " Is equal to 10";
    else
    cout << " You have entered an invalid character.";
    return 0;
    }
    Now when I enter a letter or a character it translates it to a number and compares it to 10. Anybody knows if there is a function that wont allow this to happen?
    thank you mucho.
    800p3 OCed to 850
    Gforce 256-32 DDR RAM
    256-100MhZ RAM
    DUAl 10.000RPM 60 Gig total, HDrives
    OSs: WIN2k-WIN98sec ed-LINUX MANDRAKE v8
    Soundcard realy sucks "Philips".
    ATA-TV reciever PCI Card
    HP 9100 CD burner
    DVD
    CD-ROM
    Cable modem.
    17 inc NEC-C700 monitor

  2. #2
    Ursus Arctos Moderatis Grizzly's Avatar
    Join Date
    Sep 2000
    Location
    Providence, RI USA
    Posts
    3,077

    Post

    What's happening is your program is translating the letters into their "ordinal" values, as defined in the ASCI character set.

    There's probably a better way to do this...
    but you could append this into your if statement:

    Code:
    elseif( (x >= 'A') | | ( x <= 'z' ) ){
    cout << " You have entered an invalid character.";
    }
    Read here for more on ordinal values in the ASCII set. http://www.cs.nyu.edu/courses/spring...e5/node16.html

  3. #3
    Ursus Arctos Moderatis Grizzly's Avatar
    Join Date
    Sep 2000
    Location
    Providence, RI USA
    Posts
    3,077

    Post

    Oops, bad connection at the time

    [This message has been edited by Grizzly (edited July 02, 2001).]

  4. #4
    Tiger Shark
    Join Date
    Mar 2001
    Posts
    615

    Post

    Code:
    elseif( (x >= 'A') | | ( x <= 'z' ) ){cout << " You have entered an invalid character.";}
    I don't think this will work, as he may WANT to use the numerical values from 65 to 122. He'd have to do something like what was discussed in an earlier thread, which was using a buffer.

    You'll probably want to use an array of 12 characters (10 for digits, 1 for a sign, and 1 for the null character). You'll have to loop through the array checking each character against some conditions until you hit the null. Then you'll need to convert the string into an integer, which is pretty easy.

    Ex:
    Code:
    bool valid_int(char *someString)
    {
     char *c = someString;
     count = 0;
    
     if (*c == 0)
      return false;
    
     if ((*c == '-') | | (*c == '+'))
      c++;
    
     while ((*c != 0) && (count < 10))
     {
      if ((*c < 48) | | (*c > 57))
      {
       return false;
      }
      c++;
      count++;
     }
    
     if (count > 10)
      return false;
     
     return true;
    }
    Two problems with this that I don't feel like testing to fix:

    1. This doesn't check to see if the number is greater than 2^32

    2. I'm not sure if count is being checked correctly (count is used to find out the number of digits we got).

    Close enough

    Code:
    int str_to_int(char *someString)
    {
     int x = 0;
     int scale;
     int count = 0;
     char *c = someString;
    
     if (*c == '-')
     {
      scale = -1;
      c++;
     }
     else if (*c == '+')
     {
      scale = 1;
      c++;
     }
     else
     {
      scale = 1;
     }
    
     while (*c != 0)
     {
      x += power(10, count) * (*c - 48);
      count++;
      c++;
     }
    
     return scale*x;
    }
    I think that works

    [This message has been edited by Zoma (edited July 02, 2001).]
    System specs:


    | Core i5 750 | GA-P55A-UD3 | 4.0 GB G.skill DDR3 1600 | eVGA 470 GTX |
    | Intel X25-M 80 GB SSD | WD 5000AAKS | Lian Li PC-7FN | Corsair TX750W |
    | Windows 7 Home 64-bit |

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •