C++ how to detect whitespace - c++

I am creating a program that allows users to key in their user name and password. the problem is, when the system prompts to "Enter userName:" and I hit enter key, it will print out "Name cannot contain a blank"
but if I hit a FEW spaceBars and hit enter key and make it blank field after that ,it skips to prompt the user to enter the password without printing out "Name cannot contain a blank" and prompting the user to enter the userName again.
how should I change my codes to ensure that it will still prompt the user to enter the userName again even though I hit a spacebars and hit enter? please advise. thanks
code
string userName=" ";
string password;
cout << "Enter UserName:";
while (getline(cin, userName)) {
if (userName.empty()) {
cout << "Name cannot contain a blank."<< endl;
cout << "Enter userName:";
userName = userName;
//throw errorMsg;
}
if(!userName.empty()) {
cout << "Enter Password";
}
}

Username validation is not trivial, for many reasons. You don't want to be in the business of manufacturing what you "think" the user wanted to type, while at the same time it is clear you want to avoid a potentially long validation of what is going to be invalid.
In the end, I would suspect you can simply take the prospect entry, strip all the whitespace, and if there is anything left over, submit the original entry for validation. Don't bite off the business of validating something the user may have meant to type. I.e.
"\t\t "
should be grounds for a re-prompt, while
"John Smith"
"\t WillJohnson "
"Gary"
should all be submitted verbatim, and let the chips fall where they may.
That said,
bool isValidUserName(std::string name)
{
name.erase(std::remove_if(name.begin(), name.end(),
[](char c){ return std::isspace(static_cast<unsigned char>(c));}), name.end());
return !name.empty();
}
should do that for you. A sample is below:
int main()
{
std::cout << std::boolalpha << isValidUserName("\t \t ") << std::endl;
std::cout << std::boolalpha << isValidUserName("\t Will Johnson ") << std::endl;
return 0;
}
Output
false
true

Assuming a C++11 compliant compiler your test for spaces could be using std::find_if
if (std::find_if(userName.begin(), userName.end(), isspace))
!= userName.end())
or
if (std::find_if(userName.begin(), userName.end(),
[=](char c){return isspace(c);}) != userName.end())
Notice that several characters are space-like ' ' but also '\t' (tabulation) etc...

just consider the space as a character and increment it when you find it

The logic is to check if the starting character of the username is a blank space and also the last slot of the string is not a blank space.
example :
This should be accepted = "a b".
But
this should not be " a ".
or this should not be " a".
or this should not be "a ".
if (userName[0]!=" " && userName[userName.size()]!=" "){
//accept this case and move on.
}
else{
//reject all other cases.
}

#include <iostream>
#include <vector>
using namespace std;
int main(){
string username, password;
int un = 1;
int ps = 1;
while(un){
cout<<"Username:";
getline(cin,username);
int usize = username.size();
vector <string> v ;
v.insert(v.begin(), username);
if (usize==1){
if(username==" "){
cout<<"Username can not be blank"<<endl;
}
else{
un=0;
}
}
else{
int check=0; int trim = 0;
for (int i=0; i<usize; i++){
if (username[i]!=' '){
check=1;
trim = i;
break;
}
}
if(check==1){
un=0;
}
else{
}
}
}
while(ps){
cout<<"Password:";
getline(cin,password);
int usize = password.size();
vector <string> v ;
v.insert(v.begin(), password);
if (usize==1){
if(password==" "){
cout<<"Password can not be blank"<<endl;
}
else{
ps=0;
}
}
else{
int check=0;
for (int i=0; i<usize; i++){
if (password[i]!=' '){
check=1;
break;
}
}
if(check==1){
ps=0;
}
else{
}
}
}
cout<<endl;
cout<<"----------------------------------------------"<<endl;
cout<<"Username is: "<<username<<endl;
cout<<"Password is: "<<password<<endl;
return 0;
}

Check if the string is empty if so then do the empty validation
If the string is not empty then check if it has spaces i.e find_first_not_of("\t ") if this returns number greater than 0 we know the username has leading white/tab spaces followed by zero or more characters. i.e leading spaces then the username
Its upto you to now do the validation for a good username.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string userName = " ";
string password;
cout << "Enter UserName:";
while(getline(cin, userName)) {
if(userName.empty()) {
cout << "Empty String";
} else if(userName.find_first_not_of("\t ") > 0) {
cout << "Could contain spaces followed by username or just spaces";
} else {
cout << "User Name is Valid";
}
}
return 0;
}

Related

Does anyone have any idea how to make so the mask of the password is delayed by a second or two before it replace the password with '*'?

here is some fragment of my code. Can anyone help me? On how to make so the mask of the password is delayed by a second or two before it replaces the password with '*'?
struct adminInfo
{
string adminID;
string adminName;
string adminPassword;
};
void create_admin (adminInfo ad [], int &count)
{
char pass = 0;
const char BACKSPACE = 127;
const char RETURN = 10;
cout << " **************************************" << endl;
cout << " REGISTER ADMINISTRATOR" << endl;
cout << " **************************************" << endl;
cout << " Enter admin registration number (ID) : "; cin >> ws;
getline (cin, ad[count].adminID);
cout << " Enter admin full name : "; cin >> ws;
getline (cin, ad[count].adminName);
cout << " Please create your password : ";// cin >> ws;
//getline (cin, ad[count].adminPassword)
while ((pass=getch(void)) != RETURN)
{
if (pass == BACKSPACE)
{
if (ad[count].adminPassword.length() != 0)
{
cout << "\b \b";
ad[count].adminPassword.resize(ad[count].adminPassword.length() - 1);
}
}
else
{
ad[count].adminPassword += pass;
cout << "*";
}
}
count++;
}
Side note: Should std::endl always be used?
Also it might be easier for me to answer if I knew what libraries you were using. For the sake of simplicity I will presume you use the standard C++ library iostream for cout etc conio.h for getch() string for string and namespace std.
If you don't mind the last character being seen, you could literally just wait for the user input like this:
while ((pass = getch()) != RETURN) {
/* Making length variable so I don't have to call length() function multiple times and it looks cleaner */
int length = ad[count].adminPassword.length()
/* Using bigger than zero just to make it more explicit */
if (length > 0) {
/* Change last character to asterisk */
cout << "\b \b*";
}
if (pass == BACKSPACE) {
if (length > 0) {
cout << "\b \b";
ad[count].adminPassword.resize(length - 1);
}
} else {
ad[count].adminPassword += pass;
/* Instead of asterisk as that is now changed at every keypress after first input */
cout << pass;
}
}
If you wish to actually wait you could just include <windows.h> (or unix equivalent) and use Sleep(milliseconds) function like this in the else statement:
cout << pass;
ad[count].adminPassword += pass;
Sleep(Time in milliseconds)
cout << "\b \b*";
But this will wait the time in milliseconds to print out the next value and will give a pretty confusing and questionable output if you type above 3 wpm.
This is the best that I could think of, I'm not that knoweledgeable about C++ so sorry if I could not answer your question well enough.
I'm presuming you could do something with multithreading to make it wait while taking input. But as I said I do not know much about C++ so I will leave that to someone smarter than me ;D.

C++ program stuck in an infinite loop

Please note that I am a complete beginner at C++. I'm trying to write a simple program for an ATM and I have to account for all errors. User may use only integers for input so I need to check if input value is indeed an integer, and my program (this one is shortened) works for the most part.
The problem arises when I try to input a string value instead of an integer while choosing an operation. It works with invalid value integers, but with strings it creates an infinite loop until it eventually stops (unless I add system("cls"), then it doesn't even stop), when it should output the same result as it does for invalid integers:
Invalid choice of operation.
Please select an operation:
1 - Balance inquiry
7 - Return card
Enter your choice and press return:
Here is my code:
#include <iostream>
#include <string>
using namespace std;
bool isNumber(string s) //function to determine if input value is int
{
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;
return true;
}
int ReturnCard() //function to determine whether to continue running or end program
{
string rtrn;
cout << "\nDo you wish to continue? \n1 - Yes \n2 - No, return card" << endl;
cin >> rtrn;
if (rtrn == "1" and isNumber(rtrn)) { return false; }
else if (rtrn == "2" and isNumber(rtrn)) { return true; }
else {cout << "Invalid choice." << endl; ReturnCard(); };
return 0;
}
int menu() //function for operation choice and execution
{
int choice;
do
{
cout << "\nPlease select an operation:\n" << endl
<< " 1 - Balance inquiry\n"
<< " 7 - Return card\n"
<< "\nEnter your choice and press return: ";
int balance = 512;
cin >> choice;
if (choice == 1 and isNumber(to_string(choice))) { cout << "Your balance is $" << balance; "\n\n"; }
else if (choice == 7 and isNumber(to_string(choice))) { cout << "Please wait...\nHave a good day." << endl; return 0; }
else { cout << "Invalid choice of operation."; menu(); }
} while (ReturnCard()==false);
cout << "Please wait...\nHave a good day." << endl;
return 0;
}
int main()
{
string choice;
cout << "Insert debit card to get started." << endl;
menu();
return 0;
}
I've tried every possible solution I know, but nothing seems to work.
***There is a different bug, which is that when I get to the "Do you wish to continue?" part and input any invalid value and follow it up with 2 (which is supposed to end the program) after it asks again, it outputs the result for 1 (continue running - menu etc.). I have already emailed my teacher about this and this is not my main question, but I would appreciate any help.
Thank you!
There are a few things mixed up in your code. Always try to compile your code with maximum warnings turned on, e.g., for GCC add at least the -Wall flag.
Then your compiler would warn you of some of the mistakes you made.
First, it seems like you are confusing string choice and int choice. Two different variables in different scopes. The string one is unused and completely redundant. You can delete it and nothing will change.
In menu, you say cin >> choice;, where choice is of type int. The stream operator >> works like this: It will try to read as many characters as it can, such that the characters match the requested type. So this will only read ints.
Then you convert your valid int into a string and call isNumber() - which will alway return true.
So if you wish to read any line of text and handle it, you can use getline():
string inp;
std::getline(std::cin, inp);
if (!isNumber(inp)) {
std::cout << "ERROR\n";
return 1;
}
int choice = std::stoi(inp); // May throw an exception if invalid range
See stoi
Your isNumber() implementation could look like this:
#include <algorithm>
bool is_number(const string &inp) {
return std::all_of(inp.cbegin(), inp.cend(),
[](unsigned char c){ return std::isdigit(c); });
}
If you are into that functional style, like I am ;)
EDIT:
Btw., another bug which the compiler warns about: cout << "Your balance is $" << balance; "\n\n"; - the newlines are separated by ;, so it's a new statement and this does nothing. You probably wanted the << operator instead.
Recursive call bug:
In { cout << "Invalid choice of operation."; menu(); } and same for ReturnCard(), the function calls itself (recursion).
This is not at all what you want! This will start the function over, but once that call has ended, you continue where that call happened.
What you want in menu() is to start the loop over. You can do that with the continue keyword.
You want the same for ReturnCard(). But you need a loop there.
And now, that I read that code, you don't even need to convert the input to an integer. All you do is compare it. So you can simply do:
string inp;
std::getline(std::cin, inp);
if (inp == "1" || inp == "2") {
// good
} else {
// Invalid
}
Unless that is part of your task.
It is always good to save console input in a string variable instead of another
type, e.g. int or double. This avoids trouble with input errors, e.g. if
characters instead of numbers are given by the program user. Afterwards the
string variable could by analyzed for further actions.
Therefore I changed the type of choice from int to string and adopted the
downstream code to it.
Please try the following program and consider my adaptations which are
written as comments starting with tag //CKE:. Thanks.
#include <iostream>
#include <string>
using namespace std;
bool isNumber(const string& s) //function to determine if input value is int
{
for (size_t i = 0; i < s.length(); i++) //CKE: keep same variable type, e.g. unsigned
if (isdigit(s[i]) == false)
return false;
return true;
}
bool ReturnCard() //function to determine whether to continue running or end program
{
string rtrn;
cout << "\nDo you wish to continue? \n1 - Yes \n2 - No, return card" << endl;
cin >> rtrn;
if (rtrn == "1" and isNumber(rtrn)) { return false; }
if (rtrn == "2" and isNumber(rtrn)) { return true; } //CKE: remove redundant else
cout << "Invalid choice." << endl; ReturnCard(); //CKE: remove redundant else + semicolon
return false;
}
int menu() //function for operation choice and execution
{
string choice; //CKE: change variable type here from int to string
do
{
cout << "\nPlease select an operation:\n" << endl
<< " 1 - Balance inquiry\n"
<< " 7 - Return card\n"
<< "\nEnter your choice and press return: ";
int balance = 512;
cin >> choice;
if (choice == "1" and isNumber(choice)) { cout << "Your balance is $" << balance << "\n\n"; } //CKE: semicolon replaced by output stream operator
else if (choice == "7" and isNumber(choice)) { cout << "Please wait...\nHave a good day." << endl; return 0; }
else { cout << "Invalid choice of operation."; } //CKE: remove recursion here as it isn't required
} while (!ReturnCard()); //CKE: negate result of ReturnCard function
cout << "Please wait...\nHave a good day." << endl;
return 0;
}
int main()
{
string choice;
cout << "Insert debit card to get started." << endl;
menu();
return 0;
}

Assigning the "Enter Key" value to a string [C++]

In this rather simple exercise I have to receive an user input, store said input into a string, pass the string to a function by reference and finally modify the string so that every character is "parsed" by the toupper() function.
However, should the user insert 'q' as input, the program stops saying "Bye" OR if he just presses the Enter Key, the program is supposed to say something like "Hey, this string is empty".
Now the real problem here is in the last part since my code won't manage the case where the user inputs only the Enter Key value (to be honest, even if I just text a bunch of spaces followed by the Enter Key, nothing happens)
void uppercase(std::string &);
int main(){
using namespace std;
string ex2;
cout << "Exercise 2" <<endl;
while(ex2!="Bye"){
cout << "Enter a string(q to quit): ";
cin >> ex2;
cout << "Was: " << ex2 << endl << "Now is: ";
uppercase(ex2);
}
return 0;
}
void uppercase(std::string &str){
using namespace std;
if(str[0]=='\n')
cout <<"Empty string dude!" << endl;
else{
if(str.length()==1 && str[0]=='q'){ //press 'q' to exit program
str="Bye";
cout << str;
}
else{ //uppercase
for(int i=0;i<str.length();i++){
str[i]=(toupper(str[i]));
}
cout << str <<endl;
}
}
}
I also tried the compare() function and even to compare the whole string to null (pointless, but still worth a shot) and to the string "";
Sorry for the bad interpretation of your problem, trying
if( (str.length()==1 && str[0]=='q') || str.length() == 0)
{}
May help you out of the problem

Show * while I input (Hide/mask input)

I am in the process of getting password as input.
I have gone through various examples but they either used while loop or SETCONSOLE method. Both had issues.
Implementing while loop printed 1 * before I even entered a char. The other method used echo to HIDE the characters while I typed whereas I want to be printed. I would appreciate helping me masking my input with a * using SETCONSOLE method. I would be greatly obliged. The code's attached !
void signup(){
gotoxy(10, 10);
string n, p1,p2;
cout << "Enter your username: " << endl; // TEST if username already exists
gotoxy(31, 10);
cin >> n;
lp:
gotoxy(10, 11);
cout << "Enter your password: " << endl; // TEST if username already exists
gotoxy(31, 11);
getline(cin, p1);
system("cls");
gotoxy(10, 10);
cout << "Re-Enter your password to confirm: " << endl; // TEST if username already exists
gotoxy(45, 10);
getline(cin, p2);
if (p2!=p1)
{
system("cls");
gotoxy(10, 10);
cout << "Passwords donot match! Please enter again!";
goto lp;
}
}
Here a simple example using getch. YES it c method, not c++, but it is very efficient.
It can be extended to block spaces, tabs, etc.
Also see the comments in the code...
#include <iostream>
#include <string>
#include<conio.h>
using namespace std;
int main(){
string res;
char c;
cout<<"enter password:";
do{
c = getch();
switch(c){
case 0://special keys. like: arrows, f1-12 etc.
getch();//just ignore also the next character.
break;
case 13://enter
cout<<endl;
break;
case 8://backspace
if(res.length()>0){
res.erase(res.end()-1); //remove last character from string
cout<<c<<' '<<c;//go back, write space over the character and back again.
}
break;
default://regular ascii
res += c;//add to string
cout<<'*';//print `*`
break;
}
}while(c!=13);
//print result:
cout<<res<<endl;
return 0;
}
You probably want to use getch() (#include <conio.h>) to read a character without its being echoed to the screen. Then when you've validated that it's a character you want to accept, you can print out a * at the correct position.

Invalid array assignment?

When I run the code below I get the error : invalid array assignment on lines 14 and 26. I am fairly new (1 week) to c++ so I am a bit confused. I searched and could not find an answer to solve my problem.
#include <iostream>
int main()
{
using namespace std;
char usrname[5];
char psswrd[9];
cout << "Please enter your username:";
cin >> usrname;
if (usrname = "User")
{
cout << "Username correct!";
}
else
{
cout << "Username incorrect!";
}
cout << "Please enter your password:";
cin >> psswrd;
if (psswrd = "password")
{
cout << "The password is correct! Access granted.";
}
else
{
cout << "The password is incorrect! Access denied.";
}
return 0;
}
You can't assign arrays, and
usrname = "User"
does just that. Don't.
You meant
usrname == "User"
which is a comparison, but won't compare your strings. It just compares pointers.
Use std::string instead of char arrays or pointers and compare with ==:
#include <string>
//...
std::string usrname;
cin << usrname;
if (usrname == "User")
// ^^
// note == instead of =
Side question - what's the point of shortening "username" to "usrname"... you're saving a single character...
you need to use strcmp or similar.
if (!strcmp(usrname, "User"))
{
cout << "Username correct!";
}
what you are doing is assigning a value not comparing values.