Thread: Determining if input is int, double, or char

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    9

    Determining if input is int, double, or char

    Alright, I'm writing a program that calculates a grade average. I want to allow users to input integers (80, for instance), doubles (92.5), or letters (A, F, a, f, etc).

    Basically, I'll be adding whatever the user enters to a total (letter grades will be given a predefined value... A will be 100, for instance, unless I can also allow for A+ type grades, but I'm not worried about that), and then dividing it by the total number of grades entered.

    Main will prompt the user to input a grade, using the following as examples (85, 92.3, D), and then store their answer in the appropriate variable. What I don't know is how to determine if what they entered is an integer, a double, or a letter (I don't want to make a long if statement like "if (input == a) || (input == A) || (input == b) ||" and so on. I think I have a good way to determine if the input is a whole number or double (using modulus), so really I just want to know if there's a simple way to determine if input is a letter or number and then determine which variable to store it in.

    I figure I'll have to use an overloaded extraction operator, but that still doesn't help me with the letter issue.

    If I -have- to go with the long if (grade == A)... statement, then I will, but I want to know if there's a simpler way.

    This is homework and not for practical use.

    Thanks for any and all help.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can check the return value of cin >> after attempting to read into a double. If it is false, then the read failed and it was not a number, then use clear() to clear the fail state of cin and read it into a string (or a char if you won't allow B-, B+, etc). You can't read it into a string or char first because even numbers will be read into a string correctly as digit characters.

    I don't think you should distinguish between whole numbers and floating-point numbers (doubles). If you have some reason to, then you might have to read the whole thing into a string and search for the '.' character. But again, that shouldn't be necessary.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could read in the user's input with fgets() and use isdigit() etc (<ctype.h>) to parse it.

    [edit]
    Whoops, this is the C++ board.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    9
    Quote Originally Posted by Daved
    You can check the return value of cin >> after attempting to read into a double. If it is false, then the read failed and it was not a number, then use clear() to clear the fail state of cin and read it into a string (or a char if you won't allow B-, B+, etc). You can't read it into a string or char first because even numbers will be read into a string correctly as digit characters.

    I don't think you should distinguish between whole numbers and floating-point numbers (doubles). If you have some reason to, then you might have to read the whole thing into a string and search for the '.' character. But again, that shouldn't be necessary.
    Unfortunetely we haven't learned about clear() in class yet, and so it probably won't be allowed in our homework (as she has stated something to this effect before). I half expected most, if not all solutions presented to be unacceptable for homework, but I still have hope.

    As far as distinguishing between whole and floating-point numbers go, I am doing it as an example, as this program is going to use polymorphism (I can't believe I left that out). I know it's not practical, but neither is most of the homework we do :P

    Thank you for your help, though. Even if I can't use things for my homework, it still expands my knowledge and ability for practical use.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Then I would just read into a string (with cin, not fgets) and parse the string. If the first character is a digit, then you have a number. If the first character is a letter, then you have a grade. If you need to separate whole numbers from floating point, this methods helps with that because you will be checking each digit and can check for '.'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  3. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  4. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  5. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM