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

1. ## 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. 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. You could read in the user's input with fgets() and use isdigit() etc (<ctype.h>) to parse it.

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

4. 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. 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 '.'.