So, if you declare an integer in C (e.g. int a;) and then the user inserts a diff type of number(e.g let's say we declare 'a' as an integer and the user inserts 5.6 for a), it will probably crash, or at least it will output some random numbers I remember reading somewhere that there is a way of checking his input if he does so, but I cant find anything anywhere (e.g. int a; /* then the user inserts for example a float number, INSTEAD OF CRASHING IS THERE A WAY TO MAKE IT REASK FOR THE USER TO REPUT THE VALUE UNTIL HE INSERTS AN INTEGER? */)
Asked
Active
Viewed 33 times
0
-
3What do you mean by "user inserts a number?" Do you mean the running program prompts the user for input, the program parses that input and then attempts to store it in an integer variable? You have limited to no control over what the user types in (it could be the word "hello", for example), so your program needs to validate the input is of the required type. – jarmod Jan 20 '22 at 16:46
-
If you test `scanf`, you'll see that it tries to do something sensible e.g. if the user inputs "12dogs", it will yield 12, which may or may not be the behavior you want. If you enter "hello", the result may be 32766 (it was in my test), almost definitely not what you want. Ergo, you can't use `scanf` unless you're happy with what `scanf` does with 'bad' input. Otherwise, consider using `gets` and parse the input string yourself. – jarmod Jan 20 '22 at 17:00
-
@jarmod int main() { int a; printf("Give an integer"); scanf("%d", a); //When we run the program, it will output the "Give an integer" and we, as a user, will need to input a value. If we do not input an integer type value the program will crash – γιαννης ζησης Jan 20 '22 at 17:01
-
Did you run that program? Did it crash when you entered a non-numeric input? – jarmod Jan 20 '22 at 17:01
-
γιαννης ζησης, the [dupe](https://stackoverflow.com/questions/3455913/validate-input-to-a-c-program) suggested by @Barmar does not well address how to "re-ask". I added an answer there that does loop until success. – chux - Reinstate Monica Jan 20 '22 at 17:05
-
@jarmod It does not exactly crashes, it does the same thing as your "hello" I try to find a command where instead of the "32766" result, it will reask for input and it will stop when the input is the one declared Like a validation check, but for type variables – γιαννης ζησης Jan 20 '22 at 17:07
-
γιαννης ζησης, the [dupe](https://stackoverflow.com/q/3455913/2410359) suggested by @Barmar does not well address how to "re-ask". I added an answer [there](https://stackoverflow.com/a/70790344/2410359) that does loop until success. – chux - Reinstate Monica Jan 20 '22 at 17:12
-
@chux - Reinstate Monica The one that you mentioned, it says about char and num, I have the same problem but with diff types of numbers (How do I validate if the input number was float instead of integer) – γιαννης ζησης Jan 20 '22 at 17:14
-
@γιαννηςζησης The one [there](https://stackoverflow.com/q/3455913/2410359) is about 2 `int`s not "char and num" and discusses how to repeat - which is what this question asked. To your new [question](https://stackoverflow.com/questions/70790129/is-there-any-formula-to-check-if-the-user-gave-the-same-variable-as-it-got-decla?noredirect=1#comment125146831_70790129), to validate a `float`, use `strtof()` after reading the string. Why are you using `float` instead of `double`? – chux - Reinstate Monica Jan 20 '22 at 17:20
-
Related question: [Validate the type of input in a do-while loop C](https://stackoverflow.com/q/31633005/12149471) This question is maybe not a good dupe, because OP of that question made several specific mistakes which were addressed in the answers. – Andreas Wenzel Jan 20 '22 at 17:21
-
@γιαννηςζησης If the duplicate questions don't cover your problem, you should [edit] your question, show the sourcecode in the question as a code block and also show the input, actual output and expected output or behavior. Most of the information from your comments should be edited into the question instead. – Bodo Jan 20 '22 at 17:26
-
I believe that my function `get_int_from_user` in the second code block of [this answer of mine to another question](https://stackoverflow.com/a/69636446/12149471) is exactly what you are looking for. That function will validate the input and if it cannot be converted to an `int`, it will reprompt the user for input. – Andreas Wenzel Jan 20 '22 at 17:28