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

preview_player
Показать описание
Support:
- Udemy courses coming soon!

Links:

-C++ Tutorials:
-SFML:
-SFML Sidescroller:
-SFML Open World RPG:
-SFML Simple Games and Apps:
-SFML Games in ONE VIDEO:
-Livestreams:

#gamedeveloper #2dGames #programming
Рекомендации по теме
Комментарии
Автор

Finally someone who can explain well and not mouth breathe so much! thank you

Matice
Автор

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!

zelotypia
Автор

BLESS YOUR SOUL JUST KNOWING THAT I COULD DO "== false" HELPED ME SOLVE MY LAB. THANK YOU. DEF SUBSCRIBED AND LIKED

alfredopozos
Автор

14:39 "gotta do a bunch of shit..." sounds about right...lol

islandwoodfired
Автор

God bless you man !
this video is seriously underrated

tohruoikawa
Автор

Simple and very good solution. Thanks.

guilhermecampos
Автор

you're a thanks sooo mucho, i really mean it, with this u are helping me a lot and i really appreciate it <3 luv u <3

loveandpeace
Автор

IM HERE 2020!!! TQ FOR SHARING THIS!!!!

abuzharrluqman
Автор

Good sir, God bless you. You really helped me out with my project!

amymathews
Автор

"We try to crash everything, go crazy apeshit--"
lmao

SB-lsqj
Автор

you have to use "==false" for it to work, at least for me and another dude in the comment section. thx.

PontusOzzyA
Автор

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<int>::max(), ‘\n’)


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.

latronix-omnigenus
Автор

It was very, very useful! Thanks a lot!

luigifan-CyanDestructor
Автор

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?

TJKhara
Автор

What if I want it to not allow floating point numbers?

dillonfleharty
Автор

is there a way to not accept string input from a keyboard using a c++ function

god-of-creation
Автор

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.

RCPorter
Автор

If I want intger to be only from 1-8 where should I put the condition?

rand
Автор

Sir please help me!
I've coded ifelse statement for my code.
I code if(n=='aryan')
cout<<"Hello\n";
else(
cout<<"Byy"
)

But when I running program it always interpret else not condition. And program is showing that variable type is too long for variable n.
Please help me!

ARYANKUMAR-yctc
Автор

Can you make video about input validation in classes?

kamilosok
join shbcf.ru