Tap to unmute

C++ Tutorial | Part 31 | Input validation! (cin wrong input error-check)

  Рет қаралды 38,003

Suraj Sharma

Suraj Sharma

Күн бұрын

Пікірлер: 61
@Matice21
@Matice21 2 жыл бұрын
Finally someone who can explain well and not mouth breathe so much! thank you
@zelotypia7874
@zelotypia7874 3 жыл бұрын
JESUS CHRIST, I have been puzzled by this for so long and ignored it until now but couldn't handle not knowing how to do it anymore. Just saved me a huge headache, thank you!
@alfredopozos6981
@alfredopozos6981 5 жыл бұрын
BLESS YOUR SOUL JUST KNOWING THAT I COULD DO "== false" HELPED ME SOLVE MY LAB. THANK YOU. DEF SUBSCRIBED AND LIKED
@islandwoodfired
@islandwoodfired 5 жыл бұрын
14:39 "gotta do a bunch of shit..." sounds about right...lol
@tohruoikawa1713
@tohruoikawa1713 5 жыл бұрын
God bless you man ! this video is seriously underrated .........................................
@SB-ls7qj
@SB-ls7qj 2 жыл бұрын
"We try to crash everything, go crazy apeshit--" lmao
@LunaFlahy
@LunaFlahy 2 жыл бұрын
Thank you, man! Very professional!
@guilhermecampos8313
@guilhermecampos8313 Жыл бұрын
Simple and very good solution. Thanks.
@latronix-omnigenus
@latronix-omnigenus 2 жыл бұрын
Actually setw(1) isn’t doing anything in your code. It’s working because of flushing cin (clear & ignore). Setw is not a good implementation because an app might ask the user for arbitrary numbers, so setting it to a specific width is not good. You cin.clear() to unset the failbit (so you no longer get cin.fail() == true or !cin.good as true in your case). The proper way to do cin.ignore now is like: cin.ignore(std::numeric_limits::max(), ‘ ’) You do have a better alternative, instead of adding clear & ignore after the if statement, you can do this: if (!cin.good() || cin.peek() == ‘ ‘). Peek will check a whitespace. This will force the error if a user types 1(space)2 instead of just assuming that the correct input is 1 and ignoring 2. std::isspace() would not be an option because it also gets the newline. Your error messages should go in cerr instead of cout. Do note that my answer is by no means a “great” implementation. Properly sanitizing content and handling input is a very tedious task. You should probably use a library for this or take the input as strings and properly handle it. Also you need to account for locale and Unicode characters.
@loveandpeace8271
@loveandpeace8271 4 жыл бұрын
maaaaan you're a geniuuuuus thanks sooo mucho, i really mean it , with this u are helping me a lot and i really appreciate it
@dillonfleharty5319
@dillonfleharty5319 2 жыл бұрын
What if I want it to not allow floating point numbers?
@abuzharrluqman9304
@abuzharrluqman9304 4 жыл бұрын
BOYYYYY IM HERE 2020!!! TQ FOR SHARING THIS!!!!
@TJKhara
@TJKhara 6 жыл бұрын
Hi Suraj, This code works really well. Saved me from quite a bit of headache. Do you think you could go over in a bit more detail was you teach at the end of the video with making the menu with templates so that it is not restricted to any type?
@SurajSharmaFourKnob
@SurajSharmaFourKnob 6 жыл бұрын
Thanks for the comment! Sadly I can't get into templates in detail yet like I said in the video. Not everyone might be ready for it yet. I will go through templates a little later in the tutorial.
@TJKhara
@TJKhara 6 жыл бұрын
Okay great. Yea whenever it makes sense. Look forward to it.
@god-of-creation6040
@god-of-creation6040 2 жыл бұрын
is there a way to not accept string input from a keyboard using a c++ function
@kamilosok4454
@kamilosok4454 4 жыл бұрын
Can you make video about input validation in classes?
@SurajSharmaFourKnob
@SurajSharmaFourKnob 4 жыл бұрын
Hey man I'll see what I can do :)
@kamilosok4454
@kamilosok4454 4 жыл бұрын
@@SurajSharmaFourKnob Thanks
@RCPorter92
@RCPorter92 2 жыл бұрын
I used this in a class project and it works for the most part. However, I found that if you are looking for an integer value and the user inputs a floating point it accepts it and then does some craziness in key buffer that caused my program to skip the next input attempt entirely.
@hengjiuxiao
@hengjiuxiao 3 жыл бұрын
Bless you for this great video!
@jaymob5037
@jaymob5037 5 жыл бұрын
Hear me out pls... i want to clear again if i wrote 213abc.... i dont want the system not to read the abc.. i want it to ask again... pls help
@melmenx
@melmenx 4 жыл бұрын
int check_value(int &value) { bool check = true; string temp = ""; do { getline(cin, temp, ' '); for (int i = 0; i < temp.size(); i++) { if (temp[i] != '2' && temp[i] != '3' && temp[i] != '4' && temp[i] != '5' && temp[i] != '6' && temp[i] != '7' && temp[i] != '8' && temp[i] != '9' && temp[i] != '0' && temp[i] != '1' && temp[i] != ' ') { cout
@luigifan63-CyanDestructor
@luigifan63-CyanDestructor 5 жыл бұрын
It was very, very useful! Thanks a lot!
@nathanielbaldevino5364
@nathanielbaldevino5364 5 жыл бұрын
What will happen if your input is 1a for example?
@SurajSharmaFourKnob
@SurajSharmaFourKnob 4 жыл бұрын
If you aren't checking for errors in input it will crash sadly, but check for my tutorials on input handling :) If the stream is expecting an integer, and you give it a string or anything else, it will either truncate the decimal, or crash altogether. Also, great question!
@MrThedrphil
@MrThedrphil 6 жыл бұрын
Great video! Thank you for posting!
@cesarluiz6028
@cesarluiz6028 2 жыл бұрын
U R MY HERO!!!!!
@MoeDouHamitou
@MoeDouHamitou 3 жыл бұрын
do i have to keep using std:: or can i just start with cout without the std::
@edwardibarra7665
@edwardibarra7665 3 жыл бұрын
i believe if you put the following: using namespace std; before int main you wont have to type std:: anymore and it'll let you run it perfectly fine
@amymathews4639
@amymathews4639 3 жыл бұрын
Good sir, God bless you. You really helped me out with my project!
@ARYANKUMAR-yc5tc
@ARYANKUMAR-yc5tc 4 жыл бұрын
Sir please help me! I've coded ifelse statement for my code. I code if(n=='aryan') cout
@UvUtkarsh
@UvUtkarsh 4 жыл бұрын
Bro it is just because you have written Aryan in single quotes ('Aryan') use double quotes ("Aryan") Although I am replying very late but still😀
@Gulemo
@Gulemo 3 жыл бұрын
Thanks, nice video.
@TJKhara
@TJKhara 6 жыл бұрын
Hi Suraj, Quick question on this code. Is there a way to prevent the user from entering a double? I mean if we are expecting 2, right now the code will accept 2.23 as 2. Is there a way to ask the user again to enter an int only and not a double? Thanks.
@SurajSharmaFourKnob
@SurajSharmaFourKnob 6 жыл бұрын
Hmm.. well the choice variable is an integer so it will never allow a double, and even if the user inputs it the data will be converted into an integer so there is no way a double can be accepted :) Although there might be a way to cin.peek() it.. I will look into it, otherwise if you find something please do tell me about it! Thanks for the support as always!
@yulianaluis6105
@yulianaluis6105 Жыл бұрын
thanks great video
@rand7438
@rand7438 3 жыл бұрын
If I want intger to be only from 1-8 where should I put the condition?
@TJKhara
@TJKhara 6 жыл бұрын
Thank you Suraj! Excellent video... will this work is the user just hits a bunch of line returns also? Like :)
@_daniel.w
@_daniel.w 6 жыл бұрын
Yea it will :)
@_daniel.w
@_daniel.w 6 жыл бұрын
But I mean, try it :)
@SurajSharmaFourKnob
@SurajSharmaFourKnob 6 жыл бұрын
It does work but it does not complain, it just waits until something valid comes along since a line return is not really a bad input in that sense :)
@TJKhara
@TJKhara 6 жыл бұрын
Tried it... works great :)
@qwett6348
@qwett6348 4 жыл бұрын
Thanks 👍
@sebastianm6407
@sebastianm6407 5 жыл бұрын
Great! Thank you so much! :D
@sinisakesic7640
@sinisakesic7640 6 жыл бұрын
This is really useful thanks m8! Can u teach us how to use cin in classes and arrays with user's input?
@_daniel.w
@_daniel.w 6 жыл бұрын
Well, using the input stream in a class in exactly the same, no different. But obviously put the "std::cin" in a function inside of a class :)
@SurajSharmaFourKnob
@SurajSharmaFourKnob 6 жыл бұрын
You can do like Vitex said :) Although I will make sure to include an example of this in the coming videos!
@TJKhara
@TJKhara 6 жыл бұрын
I think things just make more sense when Suraj explains them :)
@sinisakesic7640
@sinisakesic7640 6 жыл бұрын
That'll be wonderful Suraj, thanks! ^^
@shiitytrolly2962
@shiitytrolly2962 6 жыл бұрын
Thanks Bro! Liked and Subscribed❤
@SurajSharmaFourKnob
@SurajSharmaFourKnob 6 жыл бұрын
Thanks so much!
@bruhmomento_2197
@bruhmomento_2197 4 жыл бұрын
by any chance do you have the code for the function getchoice?
@SateLight
@SateLight 4 жыл бұрын
Can this code be used for an array? Anyway thanks for the video
@שירהצדוק-ב6ג
@שירהצדוק-ב6ג 4 жыл бұрын
you are amazing thank you so much
@PontusOzzyA
@PontusOzzyA 5 жыл бұрын
you have to use "==false" for it to work, at least for me and another dude in the comment section. thx.
@Roadieroundup
@Roadieroundup 5 жыл бұрын
THANKS
@bnr32bruh41
@bnr32bruh41 3 жыл бұрын
10:00
C++ Tutorial | Part 32 | Classes in classes
13:35
Suraj Sharma
Рет қаралды 1 М.
I am not sorry for switching to C
11:34
Sheafification of G
Рет қаралды 165 М.
Хаги Ваги говорит разными голосами
0:22
Фани Хани
Рет қаралды 2,2 МЛН
Should you learn C++?? | Prime Reacts
20:29
ThePrimeTime
Рет қаралды 423 М.
Cross Platform Graphical User Interfaces in C++
44:49
javidx9
Рет қаралды 876 М.
Real 10x Programmers Are SLOW To Write Code
14:51
Thriving Technologist
Рет қаралды 72 М.
Why Applications Are Operating-System Specific
13:09
Core Dumped
Рет қаралды 110 М.
you will never ask about pointers again after watching this video
8:03
Factorio teaches you software engineering, seriously.
21:27
Tony Zhu
Рет қаралды 2 МЛН
Why learn Zig?
6:44
Kodaps Academy
Рет қаралды 20 М.
Compiling C# into NATIVE code, just like Go, Rust and C++
9:05
Nick Chapsas
Рет қаралды 84 М.