How do you validate input value is correct type? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do you validate input value is correct type?

say you prompt user to input a number, but by a slip of a hand they hit a letter and hit enter. Usually a error would occur because it tries to pass the letter as a number. How does one validate the value before converting it. in C# it'd be imputed to string then Parse.int32(string). How would this translate? EDIT: It doesn't neccesarily give an answer, but if you have 4 cin functions for int values, and you input a letter, it skips the other cin functions and continues. If you cout the variable that you put the ltter in, the value is zero.

30th Mar 2017, 5:35 PM
JustSomeGuy
JustSomeGuy - avatar
8 Answers
+ 2
You could take the input as a string and process that, or this could do it: int x; std::cout << "Enter an integer: "; while(!(std::cin >> x)){ std::cout << "I said an integer!!" << std::endl; std::cin.clear(); std::cin.ignore(1000000, '\n'); //or some other high number } You can also use this to limit the input for example int x; std::cout << "Enter an integer below 4: "; while(!(std::cin >> x) || x >= 4){ std::cout << "..." << std::endl; } cin sets an error flag when it gets bad input clear erases that flag and ignore just ignores anything else remaining in the stream
30th Mar 2017, 5:42 PM
Dennis
Dennis - avatar
0
I think you are refering to exceptions in c#. But I can't answer you if you are asking about c++ Try looking up exceptions maybe that helps ._.
30th Mar 2017, 5:08 PM
Dextozz
Dextozz - avatar
0
Ah @Dennis, we meet again! Thanks again for your help! Would you mind explaining to me the ignore() function a bit more, your brief explanation isn't helpful to better understanding it.
30th Mar 2017, 6:19 PM
JustSomeGuy
JustSomeGuy - avatar
0
The first parameter says how many characters you want to ignore Lets pick 1 The user enters: abcd 1 character gets ignored and deleted from the stream, thus a gets ignored. bcd is now remaining in the stream now the while loop goes back and attempts to read from the stream, bcd, which again causes an error. Thats why you use a high number. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); would be even better The second parameter tells the function to stop when this character is reached. Since '\n' means newline and cin reads until a newline you are clearing the entire stream.
30th Mar 2017, 6:34 PM
Dennis
Dennis - avatar
0
That clears all but one question, the use of your std:: I know I dont have to since I use namespace std, but why do you insist on it @Dennis? Is there a drawback/advantage to using namespace/using std::?
30th Mar 2017, 7:14 PM
JustSomeGuy
JustSomeGuy - avatar
0
Consider the following code: #include <iostream> #include <vector> #include <algorithm> namespace foo{ template<typename Iter> void sort(Iter begin, Iter end){/*Some sorting algorithm*/}; } using namespace std; using namespace foo; int main() { std::vector<int> n{3,5,1}; sort(n.begin(), n.end()); //Ambiguous, call the sort in foo or the one in std? foo::sort(n.begin(), n.end()); //No problem, the sort of the foo namespace is called for(auto &i: n) std::cout << i << " "; } Global using namespace's are bad. They are okay to use in small projects and stuff. I also like the look of std:: way more than without.
30th Mar 2017, 7:27 PM
Dennis
Dennis - avatar
0
Thanks, I've quite a lot from you, and I just met you @Dennis. I followed you and will come in with questions occasionally if that's okay.
30th Mar 2017, 7:45 PM
JustSomeGuy
JustSomeGuy - avatar
0
Sure, I don't mind ^^
30th Mar 2017, 7:46 PM
Dennis
Dennis - avatar