Validate input data in an array C++
I have this program that i took it out from: https://intcpp.tech-academy.co.uk/input-validation/ and it works fine, i did some changes because i need the program to keep asking the user to enter a valid input, so that why it has the while in there however it only asks 4 times after that 4th time the input will be valid it does not matter if it right or not, Does any one know how i can fix this. Thank you
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main () {
cout << "Please enter name:" << endl;
string userName;
getline(cin, userName);
bool rejected = false;
while (rejected == false)
{
for (unsigned int i = 0; i < userName.length() && !rejected; i++)
{
if (isalpha(userName[i]))
continue;
else if (userName[i] == ' ')
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
rejected = false;
}
}
rejected = true;
}
system("pause");
return 0;
}
c++
add a comment |
I have this program that i took it out from: https://intcpp.tech-academy.co.uk/input-validation/ and it works fine, i did some changes because i need the program to keep asking the user to enter a valid input, so that why it has the while in there however it only asks 4 times after that 4th time the input will be valid it does not matter if it right or not, Does any one know how i can fix this. Thank you
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main () {
cout << "Please enter name:" << endl;
string userName;
getline(cin, userName);
bool rejected = false;
while (rejected == false)
{
for (unsigned int i = 0; i < userName.length() && !rejected; i++)
{
if (isalpha(userName[i]))
continue;
else if (userName[i] == ' ')
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
rejected = false;
}
}
rejected = true;
}
system("pause");
return 0;
}
c++
May I suggest you do some rubber duck debugging on your code? Or use an actual debugger to step through the code line by line? That should help you figure out the error pretty quickly.
– Some programmer dude
Nov 15 '18 at 2:11
i think i found the program, break works fine too and the way you show me too but rejected = true; where should be that on at? i tried in 3 different places and in one seems to do nothing until you add a letter in the 2 to places seems to run only one time. Thank you
– Rosario Monroy
Nov 15 '18 at 3:07
add a comment |
I have this program that i took it out from: https://intcpp.tech-academy.co.uk/input-validation/ and it works fine, i did some changes because i need the program to keep asking the user to enter a valid input, so that why it has the while in there however it only asks 4 times after that 4th time the input will be valid it does not matter if it right or not, Does any one know how i can fix this. Thank you
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main () {
cout << "Please enter name:" << endl;
string userName;
getline(cin, userName);
bool rejected = false;
while (rejected == false)
{
for (unsigned int i = 0; i < userName.length() && !rejected; i++)
{
if (isalpha(userName[i]))
continue;
else if (userName[i] == ' ')
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
rejected = false;
}
}
rejected = true;
}
system("pause");
return 0;
}
c++
I have this program that i took it out from: https://intcpp.tech-academy.co.uk/input-validation/ and it works fine, i did some changes because i need the program to keep asking the user to enter a valid input, so that why it has the while in there however it only asks 4 times after that 4th time the input will be valid it does not matter if it right or not, Does any one know how i can fix this. Thank you
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main () {
cout << "Please enter name:" << endl;
string userName;
getline(cin, userName);
bool rejected = false;
while (rejected == false)
{
for (unsigned int i = 0; i < userName.length() && !rejected; i++)
{
if (isalpha(userName[i]))
continue;
else if (userName[i] == ' ')
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
rejected = false;
}
}
rejected = true;
}
system("pause");
return 0;
}
c++
c++
edited Nov 15 '18 at 3:01
Rosario Monroy
asked Nov 15 '18 at 2:06
Rosario MonroyRosario Monroy
114
114
May I suggest you do some rubber duck debugging on your code? Or use an actual debugger to step through the code line by line? That should help you figure out the error pretty quickly.
– Some programmer dude
Nov 15 '18 at 2:11
i think i found the program, break works fine too and the way you show me too but rejected = true; where should be that on at? i tried in 3 different places and in one seems to do nothing until you add a letter in the 2 to places seems to run only one time. Thank you
– Rosario Monroy
Nov 15 '18 at 3:07
add a comment |
May I suggest you do some rubber duck debugging on your code? Or use an actual debugger to step through the code line by line? That should help you figure out the error pretty quickly.
– Some programmer dude
Nov 15 '18 at 2:11
i think i found the program, break works fine too and the way you show me too but rejected = true; where should be that on at? i tried in 3 different places and in one seems to do nothing until you add a letter in the 2 to places seems to run only one time. Thank you
– Rosario Monroy
Nov 15 '18 at 3:07
May I suggest you do some rubber duck debugging on your code? Or use an actual debugger to step through the code line by line? That should help you figure out the error pretty quickly.
– Some programmer dude
Nov 15 '18 at 2:11
May I suggest you do some rubber duck debugging on your code? Or use an actual debugger to step through the code line by line? That should help you figure out the error pretty quickly.
– Some programmer dude
Nov 15 '18 at 2:11
i think i found the program, break works fine too and the way you show me too but rejected = true; where should be that on at? i tried in 3 different places and in one seems to do nothing until you add a letter in the 2 to places seems to run only one time. Thank you
– Rosario Monroy
Nov 15 '18 at 3:07
i think i found the program, break works fine too and the way you show me too but rejected = true; where should be that on at? i tried in 3 different places and in one seems to do nothing until you add a letter in the 2 to places seems to run only one time. Thank you
– Rosario Monroy
Nov 15 '18 at 3:07
add a comment |
2 Answers
2
active
oldest
votes
Personally I would do something like
bool is_valid_username(std::string const& username)
{
// First trim the string of all leading and trailing white-space
trim(username);
if (username.length() == 0)
return false; // Input was empty or all spaces
return std::all_of(begin(username), end(username), (char const ch)
{
return std::isalpha(ch) || ch == ' '; // Only letters and spaces are allowed
});
}
std::string get_username()
{
std::string username;
do
{
std::cout << "Please enter username: ";
std::getline(std::cin, username);
} while (!is_valid_username(username));
return username;
}
[For the trim function please see this old answer]
The get_username function will continue to ask for a username forever if the input is either empty, all spaces, or contains non-letters or not a space.
Here's a reference for std::all_of.
Here's a reference about lambda expressions.
thank you, this one is seems to be working fine but the part i posted it only a part of my program, i dont know how i can join this way to the other part of my program. Thank you so much.
– Rosario Monroy
Nov 15 '18 at 3:04
1
@RosarioMonroy All the code to read and verify the name in your question could be removed and replaced with a single linestring userName = get_username();.
– Some programmer dude
Nov 15 '18 at 3:08
@RosarioMonroy Also note that while the use oftrim(username)is good to have, it's optional.
– Some programmer dude
Nov 15 '18 at 3:10
How do i call it from main?
– Rosario Monroy
Nov 15 '18 at 3:55
add a comment |
if (isalpha(userName[i]) || (userName[i] == ' '))
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
i = -1; //Reset check name
}
Try it!
Change unsigned int to int
1
Add explanation and comments to you code.
– Ketan Yekale
Nov 15 '18 at 2:49
Resettingito0won't work, because it will be incremented in the next loop so it will check from1, so if the first character was an invalid one this would still pass.
– Tas
Nov 15 '18 at 3:10
You definitely don't want to-1because it's anunsigned int, so it will break immediately from theforloop
– Tas
Nov 15 '18 at 3:37
add a comment |
Your Answer
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: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311410%2fvalidate-input-data-in-an-array-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Personally I would do something like
bool is_valid_username(std::string const& username)
{
// First trim the string of all leading and trailing white-space
trim(username);
if (username.length() == 0)
return false; // Input was empty or all spaces
return std::all_of(begin(username), end(username), (char const ch)
{
return std::isalpha(ch) || ch == ' '; // Only letters and spaces are allowed
});
}
std::string get_username()
{
std::string username;
do
{
std::cout << "Please enter username: ";
std::getline(std::cin, username);
} while (!is_valid_username(username));
return username;
}
[For the trim function please see this old answer]
The get_username function will continue to ask for a username forever if the input is either empty, all spaces, or contains non-letters or not a space.
Here's a reference for std::all_of.
Here's a reference about lambda expressions.
thank you, this one is seems to be working fine but the part i posted it only a part of my program, i dont know how i can join this way to the other part of my program. Thank you so much.
– Rosario Monroy
Nov 15 '18 at 3:04
1
@RosarioMonroy All the code to read and verify the name in your question could be removed and replaced with a single linestring userName = get_username();.
– Some programmer dude
Nov 15 '18 at 3:08
@RosarioMonroy Also note that while the use oftrim(username)is good to have, it's optional.
– Some programmer dude
Nov 15 '18 at 3:10
How do i call it from main?
– Rosario Monroy
Nov 15 '18 at 3:55
add a comment |
Personally I would do something like
bool is_valid_username(std::string const& username)
{
// First trim the string of all leading and trailing white-space
trim(username);
if (username.length() == 0)
return false; // Input was empty or all spaces
return std::all_of(begin(username), end(username), (char const ch)
{
return std::isalpha(ch) || ch == ' '; // Only letters and spaces are allowed
});
}
std::string get_username()
{
std::string username;
do
{
std::cout << "Please enter username: ";
std::getline(std::cin, username);
} while (!is_valid_username(username));
return username;
}
[For the trim function please see this old answer]
The get_username function will continue to ask for a username forever if the input is either empty, all spaces, or contains non-letters or not a space.
Here's a reference for std::all_of.
Here's a reference about lambda expressions.
thank you, this one is seems to be working fine but the part i posted it only a part of my program, i dont know how i can join this way to the other part of my program. Thank you so much.
– Rosario Monroy
Nov 15 '18 at 3:04
1
@RosarioMonroy All the code to read and verify the name in your question could be removed and replaced with a single linestring userName = get_username();.
– Some programmer dude
Nov 15 '18 at 3:08
@RosarioMonroy Also note that while the use oftrim(username)is good to have, it's optional.
– Some programmer dude
Nov 15 '18 at 3:10
How do i call it from main?
– Rosario Monroy
Nov 15 '18 at 3:55
add a comment |
Personally I would do something like
bool is_valid_username(std::string const& username)
{
// First trim the string of all leading and trailing white-space
trim(username);
if (username.length() == 0)
return false; // Input was empty or all spaces
return std::all_of(begin(username), end(username), (char const ch)
{
return std::isalpha(ch) || ch == ' '; // Only letters and spaces are allowed
});
}
std::string get_username()
{
std::string username;
do
{
std::cout << "Please enter username: ";
std::getline(std::cin, username);
} while (!is_valid_username(username));
return username;
}
[For the trim function please see this old answer]
The get_username function will continue to ask for a username forever if the input is either empty, all spaces, or contains non-letters or not a space.
Here's a reference for std::all_of.
Here's a reference about lambda expressions.
Personally I would do something like
bool is_valid_username(std::string const& username)
{
// First trim the string of all leading and trailing white-space
trim(username);
if (username.length() == 0)
return false; // Input was empty or all spaces
return std::all_of(begin(username), end(username), (char const ch)
{
return std::isalpha(ch) || ch == ' '; // Only letters and spaces are allowed
});
}
std::string get_username()
{
std::string username;
do
{
std::cout << "Please enter username: ";
std::getline(std::cin, username);
} while (!is_valid_username(username));
return username;
}
[For the trim function please see this old answer]
The get_username function will continue to ask for a username forever if the input is either empty, all spaces, or contains non-letters or not a space.
Here's a reference for std::all_of.
Here's a reference about lambda expressions.
edited Nov 15 '18 at 3:09
answered Nov 15 '18 at 2:56
Some programmer dudeSome programmer dude
301k25264424
301k25264424
thank you, this one is seems to be working fine but the part i posted it only a part of my program, i dont know how i can join this way to the other part of my program. Thank you so much.
– Rosario Monroy
Nov 15 '18 at 3:04
1
@RosarioMonroy All the code to read and verify the name in your question could be removed and replaced with a single linestring userName = get_username();.
– Some programmer dude
Nov 15 '18 at 3:08
@RosarioMonroy Also note that while the use oftrim(username)is good to have, it's optional.
– Some programmer dude
Nov 15 '18 at 3:10
How do i call it from main?
– Rosario Monroy
Nov 15 '18 at 3:55
add a comment |
thank you, this one is seems to be working fine but the part i posted it only a part of my program, i dont know how i can join this way to the other part of my program. Thank you so much.
– Rosario Monroy
Nov 15 '18 at 3:04
1
@RosarioMonroy All the code to read and verify the name in your question could be removed and replaced with a single linestring userName = get_username();.
– Some programmer dude
Nov 15 '18 at 3:08
@RosarioMonroy Also note that while the use oftrim(username)is good to have, it's optional.
– Some programmer dude
Nov 15 '18 at 3:10
How do i call it from main?
– Rosario Monroy
Nov 15 '18 at 3:55
thank you, this one is seems to be working fine but the part i posted it only a part of my program, i dont know how i can join this way to the other part of my program. Thank you so much.
– Rosario Monroy
Nov 15 '18 at 3:04
thank you, this one is seems to be working fine but the part i posted it only a part of my program, i dont know how i can join this way to the other part of my program. Thank you so much.
– Rosario Monroy
Nov 15 '18 at 3:04
1
1
@RosarioMonroy All the code to read and verify the name in your question could be removed and replaced with a single line
string userName = get_username();.– Some programmer dude
Nov 15 '18 at 3:08
@RosarioMonroy All the code to read and verify the name in your question could be removed and replaced with a single line
string userName = get_username();.– Some programmer dude
Nov 15 '18 at 3:08
@RosarioMonroy Also note that while the use of
trim(username) is good to have, it's optional.– Some programmer dude
Nov 15 '18 at 3:10
@RosarioMonroy Also note that while the use of
trim(username) is good to have, it's optional.– Some programmer dude
Nov 15 '18 at 3:10
How do i call it from main?
– Rosario Monroy
Nov 15 '18 at 3:55
How do i call it from main?
– Rosario Monroy
Nov 15 '18 at 3:55
add a comment |
if (isalpha(userName[i]) || (userName[i] == ' '))
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
i = -1; //Reset check name
}
Try it!
Change unsigned int to int
1
Add explanation and comments to you code.
– Ketan Yekale
Nov 15 '18 at 2:49
Resettingito0won't work, because it will be incremented in the next loop so it will check from1, so if the first character was an invalid one this would still pass.
– Tas
Nov 15 '18 at 3:10
You definitely don't want to-1because it's anunsigned int, so it will break immediately from theforloop
– Tas
Nov 15 '18 at 3:37
add a comment |
if (isalpha(userName[i]) || (userName[i] == ' '))
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
i = -1; //Reset check name
}
Try it!
Change unsigned int to int
1
Add explanation and comments to you code.
– Ketan Yekale
Nov 15 '18 at 2:49
Resettingito0won't work, because it will be incremented in the next loop so it will check from1, so if the first character was an invalid one this would still pass.
– Tas
Nov 15 '18 at 3:10
You definitely don't want to-1because it's anunsigned int, so it will break immediately from theforloop
– Tas
Nov 15 '18 at 3:37
add a comment |
if (isalpha(userName[i]) || (userName[i] == ' '))
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
i = -1; //Reset check name
}
Try it!
Change unsigned int to int
if (isalpha(userName[i]) || (userName[i] == ' '))
continue;
else
{
cout << "Error, Please enter Patient's name again, First Name: ";
getline(cin, userName);
i = -1; //Reset check name
}
Try it!
Change unsigned int to int
edited Nov 16 '18 at 9:39
answered Nov 15 '18 at 2:38
Lê Tuấn AnhLê Tuấn Anh
11
11
1
Add explanation and comments to you code.
– Ketan Yekale
Nov 15 '18 at 2:49
Resettingito0won't work, because it will be incremented in the next loop so it will check from1, so if the first character was an invalid one this would still pass.
– Tas
Nov 15 '18 at 3:10
You definitely don't want to-1because it's anunsigned int, so it will break immediately from theforloop
– Tas
Nov 15 '18 at 3:37
add a comment |
1
Add explanation and comments to you code.
– Ketan Yekale
Nov 15 '18 at 2:49
Resettingito0won't work, because it will be incremented in the next loop so it will check from1, so if the first character was an invalid one this would still pass.
– Tas
Nov 15 '18 at 3:10
You definitely don't want to-1because it's anunsigned int, so it will break immediately from theforloop
– Tas
Nov 15 '18 at 3:37
1
1
Add explanation and comments to you code.
– Ketan Yekale
Nov 15 '18 at 2:49
Add explanation and comments to you code.
– Ketan Yekale
Nov 15 '18 at 2:49
Resetting
i to 0 won't work, because it will be incremented in the next loop so it will check from 1, so if the first character was an invalid one this would still pass.– Tas
Nov 15 '18 at 3:10
Resetting
i to 0 won't work, because it will be incremented in the next loop so it will check from 1, so if the first character was an invalid one this would still pass.– Tas
Nov 15 '18 at 3:10
You definitely don't want to
-1 because it's an unsigned int, so it will break immediately from the for loop– Tas
Nov 15 '18 at 3:37
You definitely don't want to
-1 because it's an unsigned int, so it will break immediately from the for loop– Tas
Nov 15 '18 at 3:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311410%2fvalidate-input-data-in-an-array-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
May I suggest you do some rubber duck debugging on your code? Or use an actual debugger to step through the code line by line? That should help you figure out the error pretty quickly.
– Some programmer dude
Nov 15 '18 at 2:11
i think i found the program, break works fine too and the way you show me too but rejected = true; where should be that on at? i tried in 3 different places and in one seems to do nothing until you add a letter in the 2 to places seems to run only one time. Thank you
– Rosario Monroy
Nov 15 '18 at 3:07