Advanced Integer Input Validation | C Programming Example

  Рет қаралды 12,317

Portfolio Courses

Portfolio Courses

Күн бұрын

How to validate integer input using C with a more advanced technique. It is surprisingly difficult to reliably accept only valid integers from the user in C, as the standard scanf() function used in many examples is not effective in some cases. In this video we present a technique that requires more effort but that more reliably allows to accept only valid int input from the user. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

Пікірлер: 36
@naboulsikhalid7763
@naboulsikhalid7763 Жыл бұрын
what's beautiful in you explanation, you we through each line and what it does int term of reading int string, detecting space, then '-' sign then integers. and it looks so easy and understandable. it's also like a kind of algorithm unfolding by steps. Thank you for all what you've done and make it easier for US.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome Naboulsi, I'm glad you enjoyed the explanation and thank you for the positive feedback! :-)
@hangminh1564
@hangminh1564 2 жыл бұрын
Still can't believe my prof and TAs expect me to do this after two lectures of C programming beginner 💀
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Wow, that sounds tough! Good luck, I hope this video helped a little bit. 🙂
@DaiMoscv
@DaiMoscv 2 жыл бұрын
Thanks a lot, I've seen your others videos too and they were all very useful!
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome Bilguun! :-) And that’s excellent to hear you are finding the videos useful, thank you for sharing that feedback.
@bofa-zi4fj
@bofa-zi4fj 2 жыл бұрын
This is a great video. The program works great however, it doesn’t check for the cases when a user enters an int bigger than INT_MAX or smaller than INT_MIN
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
That's true! :-) Assuming we want to convert the string representation of the integer to an integer type like int, long, etc, we could then apply another "range validation" function to ensure the integer falls within the range of that type. Maybe one day I can do a video on that and post it here as an "extension" or additional validation option, that might be fun. :-)
@mirengeinnocent6865
@mirengeinnocent6865 Жыл бұрын
how can you use this function and read zeros as single integers? If the user enters 09, then read it as 09 not just 9. or if the user enters 0030, the function reads it as 0030 not just 30.
@rubr0nyx
@rubr0nyx Жыл бұрын
Thank you very much! Your lessons are very useful and you have a good presentation of information. Nice to learn from your videos :)
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re very welcome, I’m glad that you’re enjoying the videos and the presentation! :-)
@ZedP
@ZedP Жыл бұрын
I myself get the integer as a char[ ], and then with atoi( ) am typecasting it into a real, calculable integer. I use some ctype functions that prevent user from entering letters, special chars, spaces. For a float number I only exclude comma - and the show goes on. It is not bullet proof, I am not sure whether any validation program in C can be bullet proof (perhaps I'm wrong here). I simply hope that user won't enter something like 1;47. But your code is really great, much more sophisticated than mine.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thanks for sharing this Zed! :-) This video is more "for fun and learning", I would suggest using regex for doing serious validation that an input matches an expected pattern of some kind: www.gnu.org/software/libc/manual/html_node/Regular-Expressions.html.
@AI-NotesExperience
@AI-NotesExperience 2 жыл бұрын
Excellent tutorial... I hope you have the time to code the validation of float numbers.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Hopefully one day I can do this too. 🙂 I would use the regex library normally for handling that type of problem but I think people would be curious to see it done without using that library.
@kwesiafari605
@kwesiafari605 2 жыл бұрын
Mind blowing
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I’m glad you like it! :-)
@theanguyen1015
@theanguyen1015 2 жыл бұрын
Appreciate your video. Thanks to you that I'm able to code without breaking down
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome Thea! I’m very glad to hear the videos are helping you to code. :-)
@BrianParsons-or1lv
@BrianParsons-or1lv 2 ай бұрын
Good stuff 👏
@PortfolioCourses
@PortfolioCourses 2 ай бұрын
Thanks, I’m glad you liked it! :-)
@fabianselamai6493
@fabianselamai6493 Жыл бұрын
i only wanted to prevents characters 2:39 but when i tried this in a online compiler it wanted me to input the value 2 times, why?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's a good question, but I'm not sure Fabian. :-) Sometimes with scanf() it can leave a newline character on the buffer, and when it does we can get weird behaviours where a call to scanf() almost appears to be "skipped". I wonder if something like that is happening here, but I'm not sure.
@fabianselamai6493
@fabianselamai6493 Жыл бұрын
@@PortfolioCourses ok ty !
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome! :-)
@robintau159
@robintau159 Жыл бұрын
Wondering how it would work for checking integers in a matrix...
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm not sure Robin. :-) It works with strings, so I guess by matrix you mean a 2D array of strings? We could apply it to each string in a 'matrix of strings' I'm sure, we would need to call the function for each string.
@shuu8281
@shuu8281 2 жыл бұрын
Would I be able to extract a month and date integer value if the user input is YYYY MM DD?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Great question! 🙂 For something like that, if you're sure the user input is going to be OK/valid, you could use something like: scanf("%d %d %d', &n1, &n2, &n3); If you're not sure the input is OK and you need to do some validation, something like strtok() might help you out to try to identify each individual integer: kzbin.info/www/bejne/pKOykKOOfL-Wabs.
@shuu8281
@shuu8281 2 жыл бұрын
@@PortfolioCourses Thank you so much! What a fast reply too lol. I'll be sure to follow your advice, great videos btw.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
@@shuu8281 You're welcome. 🙂 And I'm glad to hear you enjoy the videos too!
@константинконстантинов-ш1п
@константинконстантинов-ш1п 2 жыл бұрын
How to do it without all of these libraries?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
That's a great question... the solution would look very similar, but we would effectively need to create these library functions ourselves. Maybe one day I can make videos covering how to make these library functions, I have done some functions but not all the functions in this video. 🙂
@MrPasa
@MrPasa 4 ай бұрын
I have wrote this code. It is much more easier and does the same job. #include int main() { float x; char c; while(1) { printf("Enter a positive integer: "); if (scanf("%f", &x) == 1) { if (x > 0 && x - (int)x == 0) { break; } else { printf("Please enter a positive integer. "); } } else { printf("Invalid input. Please enter a positive integer. "); while ((c = getchar()) != ' ' && c != EOF) {} } } int valid_input = (int)x; printf("Your input is: %d ", valid_input); return 0; }
How to parse and validate a number in C
10:16
CodeVault
Рет қаралды 7 М.
Dynamic Memory Allocation | C Programming Tutorial
31:51
Portfolio Courses
Рет қаралды 86 М.
Kluster Duo #настольныеигры #boardgames #игры #games #настолки #настольные_игры
00:47
SHAPALAQ 6 серия / 3 часть #aminkavitaminka #aminak #aminokka #расулшоу
00:59
Аминка Витаминка
Рет қаралды 3,1 МЛН
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 62 М.
Pointers in C for Absolute Beginners - Full Course
2:04:29
freeCodeCamp.org
Рет қаралды 234 М.
Be Careful When Using scanf() in C
12:22
NeuralNine
Рет қаралды 129 М.
How To Return An Array From A Function | C Programming Tutorial
13:01
Portfolio Courses
Рет қаралды 67 М.
Infinite Input Buffer | C Programming Example
13:08
Portfolio Courses
Рет қаралды 16 М.
Check If A Number Is Prime | C Programming Example
11:57
Portfolio Courses
Рет қаралды 12 М.
Advanced C: The UB and optimizations that trick good programmers.
1:12:34
Eskil Steenberg
Рет қаралды 171 М.
Kluster Duo #настольныеигры #boardgames #игры #games #настолки #настольные_игры
00:47