Handle and validate general input under certain constraints

Clash 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:
- It does not support strings with spaces.
- 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:
- Is this method at least a little bit efficient? How can it be more efficient?
- 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.
c++ performance validation
 |Â
show 2 more comments
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:
- It does not support strings with spaces.
- 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:
- Is this method at least a little bit efficient? How can it be more efficient?
- 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.
c++ performance validation
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 nowautowas not supported everywhere. Does that mean that atemplateis 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
 |Â
show 2 more comments
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:
- It does not support strings with spaces.
- 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:
- Is this method at least a little bit efficient? How can it be more efficient?
- 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.
c++ performance validation
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:
- It does not support strings with spaces.
- 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:
- Is this method at least a little bit efficient? How can it be more efficient?
- 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.
c++ performance validation
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 nowautowas not supported everywhere. Does that mean that atemplateis 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
 |Â
show 2 more comments
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 nowautowas not supported everywhere. Does that mean that atemplateis 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
 |Â
show 2 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
autowas not supported everywhere. Does that mean that atemplateis 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