isalpha() returns true to '[a-zA-Z]' and false to everything else. isdigit() returns true to '[0-9]' and false to everything else.
the point of these are not to tell type char from type int but to tell if the character they represent is a letter or a number, which is where the acs ii tables come in.
I was making a program that would like users to choose from 7 different menus to enter, if they entered anything besides 1, 2, 3, 4,5, 6 or 7. The program would output something to call users to enter the available options only and ask them to input again. How can I do that ?
I was considering for if the users keyed in characters. Because the code that I've accomplished can only handle with floats and integers, and if any characters are typed in. The program would be in an infinite loop.
This would mess up my code for dealing with floats since my code for dealing with that is.
I thought you were allowing the user to choose from one of 7 items on a menu. Why do you use type float for this - is the user allowed to choose option for example 3.68678 or -1.7 ?
To begin by stating the solution is to put the cart ahead of the horse. You need to start by stating what are the requirements, how is the program intended to behave, purely from the external point of view, that is from the user's perspective. Only after that is clear can you determine what would be a suitable data type to capture the user input.
In any case, it is a simple enough matter to convert from one type to another, if there is a real need to do so. By 'real need' I mean to satisfy some functional requirement.
I thought you were allowing the user to choose from one of 7 items on a menu. Why do you use type float for this - is the user allowed to choose option for example 3.68678 or -1.7 ?
I think you need to start by stating what are the requirements, how is the program intended to behave, purely from the external point of view, that is from the user's perspective. Only after that is clear can you determine what would be a suitable data type to capture the user input.
In any case, it is a simple enough matter to convert from one type to another, if there is a real need to do so.
Yes, but I was just trying to make the program bug free. What if the user keys in a float ? or a character? Even though I've told them to key in integers?
Well, the code I suggested will read just a single character. If the user keys in the integer 1, then that will be read in and considered valid. On the other hand if the user keys in the letter 'A' that will be read in and rejected as not valid. This is a reasonably robust solution - thought it could still be further refined, but at this stage I am trying to keep things simple.
As you already mentioned, when you used type float, if the user keyed in a letter, it resulted in an infinite loop, so that is a significantly less robust approach.
You might want to try out this code which I posted yesterday in answer to a different question - it accepts only integers: