C++ question. No i dont want you to do my homework.

Sharky Forums


Results 1 to 6 of 6

Thread: C++ question. No i dont want you to do my homework.

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

    Talking C++ question. No i dont want you to do my homework.

    i wrote a program and I would like to have it so that it will give a warning message if you enter a letter i nstead of numbers. I am not using switch statements to put "default;". Its written with "for" and "if else " statements. Anyone can give me an idea about this one to do it with out overflowing each statement with "while" commands?
    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
    Expensive Sushi
    Join Date
    Mar 2001
    Location
    Roi, Finland
    Posts
    33

    Post

    I dont know what you are doing, but the "numbers" in the keycodes are lined up meaning you can use a simple test:

    if(input >= '1' && input <= '0')

    In this case you'd have the 'if' execute when the typed letter was 1, 2, 3 ... 0.

    Then again, you could be reading more than one char. Do you read it to string? Do you convert it to numeric? In this case the conversion should return with a error (if letters present).

    Or you could be coding for Win32, which would make things more difficult (regarding reading input etc.)...


    ------------------
    __
    Yep, I'm one of them
    __
    Yep, I'm one of them

  3. #3
    Hammerhead Shark
    Join Date
    Sep 2000
    Location
    Edinburgh,UK
    Posts
    1,188

    Post

    Isn't there an "isInteger" function somewhere (operates on strings, returns true if the string is a representation of an int, false otherwise)? I remember writing one myself and kicking myself when I found the function...

    ------------------
    Stoo
    Stoo

  4. #4
    Reef Shark gammaray51's Avatar
    Join Date
    Sep 2000
    Location
    Virgina Beach, VA
    Posts
    493

    Post

    you could also typecast the input from a char to an int, then check to see if it is part of the numberic ACSII table.

  5. #5
    Hammerhead Shark
    Join Date
    Feb 2001
    Posts
    1,612

    Post

    The function is called isdigit() (#include <ctype.h> )

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

    Post

    I'd probably read in input as a string of length 256.

    Then I'd loop through each character and compare it to the ascii values that represent numbers. If the type I was supposed to be reading in was signed, I'd also allow for the first character to be a '-' or a '+'. I'd send a "NAN" error if one character was wrong.

    Then check to see if strlen was greater than the size of the data (eg, 5 characters for an unsigned short). If the size is greater, I'd return an overflow. If it's smaller, the string is fine.

    Otherwise, it's the same length and another test must be done. I'd then check the MSB against the largest MSB of the type (eg, 6 is the largest value the MSB an unsigned short can have). I'd have to loop through each character, since 64000 is ok, but 66000 is not.

    If it passes all of this, I accept the string. A conversion loop is pretty easy to write. The entered value would then be stored in the correct numerical type (eg, unsigned short).
    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
  •