Handle and validate general input under certain constraints

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I have been programming in C++ for less than six months, so please correct anything you can.



I think one of the most annoying things programmers have to deal with is validating user input. For instance, if my program says:




Enter a number between 0 and 5




If the user keeps entering 7, the program will not work properly, so a data validation method must be implemented. It could be checking if the input is within range, or we could just try and catch. However, we would have to write different code for different variables and conditions. So, I tried to create a generic method:



#include <iostream>

void manage_input(auto & x, auto lambda_cond,
std::string in_msg, std::string err_msg)


One way we could use it is the following:



int main() 
int x;
auto lambda_cond = (int x) return x < 5;;
std::string in_msg = "[x]: ";
std::string err_msg = "Error! x must be less than 5!n";

// Make sure that x is less than 5.
manage_input(x, lambda_cond, in_msg, err_msg);

// Print a number that is less than 5.
std::cout << x << "n";

return 0;



Now, there are many things wrong with this method:



  1. It does not support strings with spaces.

  2. If a string is entered, the error message will be displayed up to the number of characters in the string.

Another thing is that I do not know how efficient this program is in terms of resource consumption.



Questions:



  1. Is this method at least a little bit efficient? How can it be more efficient?

  2. Is there a standard way to validate user input? If so, which is it?

I have read about other methods, for example:




  • This one, which works for integers only.


  • This one, which I might actually implement in the method defined above.

NOTE: When I talk about an efficient solution, I am talking about portable and reusable method that can reliably handle inputs of different data types given certain constraints without sacrificing performance.



It would be portable in the sense that it is OS agnostic and does not depend on code that is not in the standard library. It would be reusable in the sense that it can work with all data types without producing errors. It would not sacrifice performance in the sense that it would not run unnecessary steps.







share|improve this question





















  • How did you compile this? It seems this isn't valid C++: error: 'auto' not allowed in function prototype. (Nevermind, apparently this is allowed in C++14)
    – mkrieger1
    Jan 19 at 12:35











  • Well, actually apparently this is not part of C++14 but GCC supports it, and Clang doesn't
    – mkrieger1
    Jan 19 at 12:41










  • And #include <iostream> is missing in the code.
    – mkrieger1
    Jan 19 at 12:43











  • You might find this blog post about using lambdas for input validation interesting.
    – Null
    Jan 19 at 14:46










  • @mkrieger1 I have added the header now. Also, I did not now auto was not supported everywhere. Does that mean that a template is the way to go for this type of case, or is there another practical option besides function overloading?
    – Armando H.
    Jan 19 at 15:03
















up vote
0
down vote

favorite












I have been programming in C++ for less than six months, so please correct anything you can.



I think one of the most annoying things programmers have to deal with is validating user input. For instance, if my program says:




Enter a number between 0 and 5




If the user keeps entering 7, the program will not work properly, so a data validation method must be implemented. It could be checking if the input is within range, or we could just try and catch. However, we would have to write different code for different variables and conditions. So, I tried to create a generic method:



#include <iostream>

void manage_input(auto & x, auto lambda_cond,
std::string in_msg, std::string err_msg)


One way we could use it is the following:



int main() 
int x;
auto lambda_cond = (int x) return x < 5;;
std::string in_msg = "[x]: ";
std::string err_msg = "Error! x must be less than 5!n";

// Make sure that x is less than 5.
manage_input(x, lambda_cond, in_msg, err_msg);

// Print a number that is less than 5.
std::cout << x << "n";

return 0;



Now, there are many things wrong with this method:



  1. It does not support strings with spaces.

  2. If a string is entered, the error message will be displayed up to the number of characters in the string.

Another thing is that I do not know how efficient this program is in terms of resource consumption.



Questions:



  1. Is this method at least a little bit efficient? How can it be more efficient?

  2. Is there a standard way to validate user input? If so, which is it?

I have read about other methods, for example:




  • This one, which works for integers only.


  • This one, which I might actually implement in the method defined above.

NOTE: When I talk about an efficient solution, I am talking about portable and reusable method that can reliably handle inputs of different data types given certain constraints without sacrificing performance.



It would be portable in the sense that it is OS agnostic and does not depend on code that is not in the standard library. It would be reusable in the sense that it can work with all data types without producing errors. It would not sacrifice performance in the sense that it would not run unnecessary steps.







share|improve this question





















  • How did you compile this? It seems this isn't valid C++: error: 'auto' not allowed in function prototype. (Nevermind, apparently this is allowed in C++14)
    – mkrieger1
    Jan 19 at 12:35











  • Well, actually apparently this is not part of C++14 but GCC supports it, and Clang doesn't
    – mkrieger1
    Jan 19 at 12:41










  • And #include <iostream> is missing in the code.
    – mkrieger1
    Jan 19 at 12:43











  • You might find this blog post about using lambdas for input validation interesting.
    – Null
    Jan 19 at 14:46










  • @mkrieger1 I have added the header now. Also, I did not now auto was not supported everywhere. Does that mean that a template is the way to go for this type of case, or is there another practical option besides function overloading?
    – Armando H.
    Jan 19 at 15:03












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have been programming in C++ for less than six months, so please correct anything you can.



I think one of the most annoying things programmers have to deal with is validating user input. For instance, if my program says:




Enter a number between 0 and 5




If the user keeps entering 7, the program will not work properly, so a data validation method must be implemented. It could be checking if the input is within range, or we could just try and catch. However, we would have to write different code for different variables and conditions. So, I tried to create a generic method:



#include <iostream>

void manage_input(auto & x, auto lambda_cond,
std::string in_msg, std::string err_msg)


One way we could use it is the following:



int main() 
int x;
auto lambda_cond = (int x) return x < 5;;
std::string in_msg = "[x]: ";
std::string err_msg = "Error! x must be less than 5!n";

// Make sure that x is less than 5.
manage_input(x, lambda_cond, in_msg, err_msg);

// Print a number that is less than 5.
std::cout << x << "n";

return 0;



Now, there are many things wrong with this method:



  1. It does not support strings with spaces.

  2. If a string is entered, the error message will be displayed up to the number of characters in the string.

Another thing is that I do not know how efficient this program is in terms of resource consumption.



Questions:



  1. Is this method at least a little bit efficient? How can it be more efficient?

  2. Is there a standard way to validate user input? If so, which is it?

I have read about other methods, for example:




  • This one, which works for integers only.


  • This one, which I might actually implement in the method defined above.

NOTE: When I talk about an efficient solution, I am talking about portable and reusable method that can reliably handle inputs of different data types given certain constraints without sacrificing performance.



It would be portable in the sense that it is OS agnostic and does not depend on code that is not in the standard library. It would be reusable in the sense that it can work with all data types without producing errors. It would not sacrifice performance in the sense that it would not run unnecessary steps.







share|improve this question













I have been programming in C++ for less than six months, so please correct anything you can.



I think one of the most annoying things programmers have to deal with is validating user input. For instance, if my program says:




Enter a number between 0 and 5




If the user keeps entering 7, the program will not work properly, so a data validation method must be implemented. It could be checking if the input is within range, or we could just try and catch. However, we would have to write different code for different variables and conditions. So, I tried to create a generic method:



#include <iostream>

void manage_input(auto & x, auto lambda_cond,
std::string in_msg, std::string err_msg)


One way we could use it is the following:



int main() 
int x;
auto lambda_cond = (int x) return x < 5;;
std::string in_msg = "[x]: ";
std::string err_msg = "Error! x must be less than 5!n";

// Make sure that x is less than 5.
manage_input(x, lambda_cond, in_msg, err_msg);

// Print a number that is less than 5.
std::cout << x << "n";

return 0;



Now, there are many things wrong with this method:



  1. It does not support strings with spaces.

  2. If a string is entered, the error message will be displayed up to the number of characters in the string.

Another thing is that I do not know how efficient this program is in terms of resource consumption.



Questions:



  1. Is this method at least a little bit efficient? How can it be more efficient?

  2. Is there a standard way to validate user input? If so, which is it?

I have read about other methods, for example:




  • This one, which works for integers only.


  • This one, which I might actually implement in the method defined above.

NOTE: When I talk about an efficient solution, I am talking about portable and reusable method that can reliably handle inputs of different data types given certain constraints without sacrificing performance.



It would be portable in the sense that it is OS agnostic and does not depend on code that is not in the standard library. It would be reusable in the sense that it can work with all data types without producing errors. It would not sacrifice performance in the sense that it would not run unnecessary steps.









share|improve this question












share|improve this question




share|improve this question








edited Jan 19 at 14:58
























asked Jan 19 at 4:10









Armando H.

185




185











  • How did you compile this? It seems this isn't valid C++: error: 'auto' not allowed in function prototype. (Nevermind, apparently this is allowed in C++14)
    – mkrieger1
    Jan 19 at 12:35











  • Well, actually apparently this is not part of C++14 but GCC supports it, and Clang doesn't
    – mkrieger1
    Jan 19 at 12:41










  • And #include <iostream> is missing in the code.
    – mkrieger1
    Jan 19 at 12:43











  • You might find this blog post about using lambdas for input validation interesting.
    – Null
    Jan 19 at 14:46










  • @mkrieger1 I have added the header now. Also, I did not now auto was not supported everywhere. Does that mean that a template is the way to go for this type of case, or is there another practical option besides function overloading?
    – Armando H.
    Jan 19 at 15:03
















  • How did you compile this? It seems this isn't valid C++: error: 'auto' not allowed in function prototype. (Nevermind, apparently this is allowed in C++14)
    – mkrieger1
    Jan 19 at 12:35











  • Well, actually apparently this is not part of C++14 but GCC supports it, and Clang doesn't
    – mkrieger1
    Jan 19 at 12:41










  • And #include <iostream> is missing in the code.
    – mkrieger1
    Jan 19 at 12:43











  • You might find this blog post about using lambdas for input validation interesting.
    – Null
    Jan 19 at 14:46










  • @mkrieger1 I have added the header now. Also, I did not now auto was not supported everywhere. Does that mean that a template is the way to go for this type of case, or is there another practical option besides function overloading?
    – Armando H.
    Jan 19 at 15:03















How did you compile this? It seems this isn't valid C++: error: 'auto' not allowed in function prototype. (Nevermind, apparently this is allowed in C++14)
– mkrieger1
Jan 19 at 12:35





How did you compile this? It seems this isn't valid C++: error: 'auto' not allowed in function prototype. (Nevermind, apparently this is allowed in C++14)
– mkrieger1
Jan 19 at 12:35













Well, actually apparently this is not part of C++14 but GCC supports it, and Clang doesn't
– mkrieger1
Jan 19 at 12:41




Well, actually apparently this is not part of C++14 but GCC supports it, and Clang doesn't
– mkrieger1
Jan 19 at 12:41












And #include <iostream> is missing in the code.
– mkrieger1
Jan 19 at 12:43





And #include <iostream> is missing in the code.
– mkrieger1
Jan 19 at 12:43













You might find this blog post about using lambdas for input validation interesting.
– Null
Jan 19 at 14:46




You might find this blog post about using lambdas for input validation interesting.
– Null
Jan 19 at 14:46












@mkrieger1 I have added the header now. Also, I did not now auto was not supported everywhere. Does that mean that a template is the way to go for this type of case, or is there another practical option besides function overloading?
– Armando H.
Jan 19 at 15:03




@mkrieger1 I have added the header now. Also, I did not now auto was not supported everywhere. Does that mean that a template is the way to go for this type of case, or is there another practical option besides function overloading?
– Armando H.
Jan 19 at 15:03















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185447%2fhandle-and-validate-general-input-under-certain-constraints%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185447%2fhandle-and-validate-general-input-under-certain-constraints%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods