Limit input to alphabet only [a-z, A-Z,SPACE,PERIOD] - c++

I'm trying to limit user input to certain characters only [a-z,A-Z,SPACE,PERIOD], I can't get this function right?!
void setName()
{
char fname[20];
unsigned int ascii_val;
while(ascii_val<65 || ascii_val>90)
{
cout << "First Name: ";
cin.ignore().getline(fname,20);
for(int i=0; i<sizeof(fname); i++)
{
ascii_val = toupper(fname[i]);
if(ascii_val==32 || ascii_val==46 || ascii_val==0) //Exception to allow SPACE and PERIOD
{
ascii_val=65;
};
if(ascii_val<65 || ascii_val>90)
{
cout << "Only Alphabet [a-z,A-Z,SPACE,DOT] Allowed!\n";
break;
}
}
}
newfname = fname;
}

This finally worked! if you think this is not the best solution, post your alternative please.
bool charVerify(string word)
{
for (int i = 0; i < word.size(); i++)
{
int character = toupper(word[i]);
if(character == ' ' || character == '.')
{
character='A';
}
if (character < 'A' || character > 'Z')
{
return false;
}
}
return true;
}
void setFname()
{
string fname;
cout << "Enter Name: ";
getline(std::cin.ignore(),fname);
while(1)
{
if (charVerify(fname))
{
break;
}
else
{
cout << "\tInvalid Input! Only [a-z,A-Z,SPACE,PERIOD]" << endl;
}
cin.clear();
cout << "Enter Name: ";
getline(std::cin.ignore(),fname);
}
firstname = fname;
}

Related

Creating a Login and Register with username and password constraints and saving the data in a file. (C++)

I am a beginner at coding and learning C++. I just wrote a code that asks the user to log in, Register, or exit.
I thought doing it through functions would be easier. While checking for the constraints of the username and password for registration(referred to as "Rusr" and "Rpwd" here) the code I've written to check if lowercase, uppercase, and digits are not working. I tried to do it with character array as well but that didn't work. Would really appreciate some help with this.
Here's my code below for further reference:
#include <fstream>
#include <iostream>
#include <string.h>
using namespace std;
bool isvalidName(string Rusr)
{
if(Rusr.length() > 5 && Rusr.length() < 20) // length constraint
return true;
else
cout << "Invalid username" << endl;
{
for(int i = 0; i < Rusr.length(); i++) // check if digits are present
{
if(isdigit(Rusr[i]) == true)
return true;
if(true)
break;
else
cout << "Invalid username";
}
}
{
for(int i = 0; i < Rusr.length(); i++) // check for lowercase
{
if(islower(Rusr[i])) {
return true;
}
if(true)
break;
else
cout << "Invalid username";
}
}
{
for(int i = 0; i < Rusr.length(); i++) // check for uppercase
{
if(isupper(Rusr[i])) return true;
if(true)
break;
else
cout << "Invalid username";
}
}
}
int isvalidPwd(string Rpwd) {
{
if(Rpwd.length() > 8 && Rpwd.length() < 20)
return true;
else
cout << "Invalid password " << endl;
}
{
for(int i = 0; i < Rpwd.length(); i++) {
if(isdigit(Rpwd[i]) == true) return true;
if(true)
break;
else
cout << "Invalid password";
}
}
{
if(!((Rpwd.find("#") == string::npos) ||
(Rpwd.find("#") == string::npos) ||
(Rpwd.find("!") == string::npos) ||
(Rpwd.find("$") == string::npos) ||
(Rpwd.find("%") == string::npos) ||
(Rpwd.find("^") == string::npos) ||
(Rpwd.find("&") == string::npos) ||
(Rpwd.find("*") == string::npos) ||
(Rpwd.find("(") == string::npos) ||
(Rpwd.find(")") == string::npos) ||
(Rpwd.find("_") == string::npos) ||
(Rpwd.find("+") == string::npos) ||
(Rpwd.find("|") == string::npos) ||
(Rpwd.find(">") == string::npos) ||
(Rpwd.find("<") == string::npos) ||
(Rpwd.find("?") == string::npos) ||
(Rpwd.find("/") == string::npos) ||
(Rpwd.find("~") == string::npos) ||
(Rpwd.find(".") == string::npos) ||
(Rpwd.find(",") == string::npos)))
return true;
else
cout << "should contain special characters" << endl;
}
for(int i = 0; i < Rpwd.length(); i++) {
if(islower(Rpwd[i])) return true;
if(true)
break;
else
cout << "should contain lower case";
}
{
for(int i = 0; i < Rpwd.length(); i++) {
if(isupper(Rpwd[i])) return true;
if(true)
break;
else
cout << "Should contain upper case";
}
}
}
int main() {
string Lusr, Lpwd, Rusr, Rpwd, name, pwd;
while(1) {
cout << "___________________________________" << endl;
cout << "Chose 1 to Register or 2 to Login" << endl;
cout << "___________________________________" << endl;
cout << "1.Register" << endl;
cout << "2.Login" << endl;
cout << "3.Exit" << endl;
cout << "___________________________________" << endl;
int choice;
cin >> choice;
if(choice == 1) // register
{
ofstream of("register.txt");
if(!of.is_open()) {
cout << "file not exist" << endl;
}
do {
cout << "Username should contain capital,small letters and "
"numbers. "
<< endl;
cout << "______________________________________" << endl;
cout << "Enter new username:" << endl;
cin.ignore();
getline(cin, Rusr);
isvalidName(Rusr);
} while(isvalidName(Rusr) == true);
do {
cout << "Password should contain capital,small letters,special "
"characters and numbers. "
<< endl;
cout << "_______________________________________" << endl;
cout << "Enter new passsword:" << endl;
getline(cin, Rpwd);
isvalidPwd(Rpwd);
} while(isvalidPwd(Rpwd) == true);
of << Rusr;
of << '\n';
of << Rpwd;
of.close();
}
else if(choice == 2) {
ifstream f("register.txt");
if(!f.is_open()) {
cout << "file not open" << endl;
}
getline(f, name, '\n');
getline(f, pwd, '\n');
f.close();
cout << "Enter username:" << endl;
cin.ignore();
getline(cin, Lusr);
cout << "Enter passsword:" << endl;
getline(cin, Lpwd);
if(Lpwd == pwd && Lusr == name) {
cout << "Welcome " << Lusr << endl;
;
break;
}
cout << "Wrong name and password" << endl;
}
else if(choice == 3) {
return 1;
} else {
cout << "Invalid input!Try again." << endl;
}
}
return 0;
}
With your code, some of your logic is wrong. When you say this
if(Rusr.length() > 5 && Rusr.length() < 20) // length constraint
return true;
else
cout << "Invalid username`enter code here`" << endl;
Any string that is between 5 and 20 characters will be accepted as a valid username. To fix this, you need to go through all your requirements, then return true. If any of the requirements fail, return false. This makes it so that the only way that the username will be valid is if it has already passed all the requirements.
bool isvalidName(string Rusr)
{
if(Rusr.length() < 5 || Rusr.length() > 20) // length constraint
{
std::cout << "invalid username length" << std::endl;
return false;
}
for(int i = 0; i < Rusr.length(); i++) // check if digits are present
{
if(isdigit(Rusr[i])) // if a digit is present, continue
{
break;
}
if(i == Rusr.length() - 1) // No digits were found
{
return false;
}
}
for(int i = 0; i < Rusr.length(); i++) // check for lowercase
{
if(islower(Rusr[i])) {
break;
}
if(i == Rusr.length() - 1) // No lowercase letters were found
{
cout << "Invalid username";
return false;
}
}
for(int i = 0; i < Rusr.length(); i++) // check for uppercase
{
if(isupper(Rusr[i]))
{
break;
}
if(i == Rusr.length() - 1) // No uppercase letters were found
{
cout << "Invalid username";
return false;
}
}
return true;
}
This way, the only way you return true is if it gets past all the constraints.
For the password, you would want to follow this model and adjust for those constraints.
Some other things to watch out for are the extra braces you have included. Best practice is to not change the scope further in than you need to, so you should avoid the braces in almost every case.
Both your functions fail to return the value you've declared that they should return in all branches, so your program has undefined behavior.
Also, the logic is flawed. Example:
bool isvalidName(string Rusr) {
if(Rusr.length() > 5 && Rusr.length() < 20) // length constraint
return true;
else
cout << "Invalid username`enter code here`" << endl;
// .. the rest ...
}
Here you check that the length is [6, 19] characters long - and return true; - that's all it takes to get Rusr approved. None of the other tests in your function will be executed if the name has an approved length.
Your code is riddled with similar mistakes.
for(int i = 0; i < Rusr.length(); i++) // check if digits are present
{
if(isdigit(Rusr[i]) == true) return true;
The first digit you encounter makes isvalidName return true - so now you have a name with an invalid length that contains a digit - approved!
Similar issue below. A name with an illegal length, without digits, that contains a lowercase letter is immediately approved:
for(int i = 0; i < Rusr.length(); i++) // check for lowercase
{
if(islower(Rusr[i])) {
return true;
And finally, if a name with an illegal length, without digits and without lowercase letters contains an uppercase letter, it's approved:
for(int i = 0; i < Rusr.length(); i++) // check for uppercase
{
if(isupper(Rusr[i])) return true;
If none of the above applies - you don't return anything (causing undefined behavior).
Your second function follows this pattern.

C++ with menu system: How to eliminate input buffers when pressing arrow keys

I was able to make a program that has a menu on it. At first, the program is running smoothly. The problem starts to appear when, at a certain point in the program, the user starts inputting something and then navigating the menus again.
Sometimes it will print out integers/characters that were previously entered by the user when pressing the ENTER key in the menu.
I managed to block the inputs with cin.ignore(). But how can I eliminate the display of the (I don't know if its excess or garbage) inside the input buffer whenever the user presses the LEFT and RIGHT arrow keys? Or I think there's even one for UP and DOWN arrow key?
int main()
{
color(FG_WHITE);
bool progFinished = false;
while (!progFinished) {
cout << "[ACTIVITY FOR 26 FEBRUARY 2018]\n";
vector<string> menu = { "Currency Converter","Student Information System" };
int choice = menu_std(menu, FG_YELLOW, false);
bool currencyFinished = false;
bool studentFinished = false;
switch (choice) {
case 0:
while (!currencyFinished) {
cout << "\n\n[CURRENCY CONVERT]\n";
vector<string> curr_menu = { "PHP TO OTHERS","USD TO PHP","YEN TO PHP","EUR TO PHP","BHD TO PHP" };
int currency = menu_std(curr_menu, FG_YELLOW, false);
double value = validation("\nEnter the value", 0, false);
CurrencyConverter convert(currency, value);
cout << "\n\nConvert again? ";
currencyFinished = !menu_yn(FG_YELLOW);
}
system("cls");
break;
case 1:
Studentv2 stud;
stud.LoadFile();
while (!studentFinished) {
cout << "\n\n[STUDENT INFO SYSTEM]\n";
vector<string> stud_menu = { "Add Student","View All Students" };
int stud_choice = menu_std(stud_menu, FG_YELLOW, false);
switch (stud_choice) {
case 0:
stud.AddStudent();
cout << "\n\nDo you want to continue with the Student Info System? ";
stud.SaveFile();
studentFinished = !menu_yn(FG_YELLOW);
break;
case 1:
stud.DisplayStudentInformation();
cout << "\n\nDo you want to continue with the Student Info System? ";
studentFinished = !menu_yn(FG_YELLOW);
break;
}
}
system("cls");
break;
}
cout << "\nContinue using the program? ";
progFinished = !menu_yn(FG_YELLOW);
system("cls");
}
cout << "Press any key to exit.\n";
cin.get();
return 0;
}
Code snippets for some of the functions I used inside my program.
Function menu_std():
int menu_std(std::vector<std::string> &menu, WORD atrribute, bool argument)
{
sleep_for(milliseconds(150));
int elements = menu.size();
int elements_max_size = menu.size() - 1;
int menu_position = 0;
COORD position = getcoordinates();
while (true) {
for (int i = 0; i < elements; i++) {
move(position.X, position.Y + i);
if (i == menu_position) {
color(atrribute);
std::cout << menu[i];
color(FG_WHITE);
}
else {
std::cout << menu[i];
}
}
while (true) {
if (GetAsyncKeyState(VK_UP) < 0 && menu_position != 0) {
menu_position--;
break;
}
if (GetAsyncKeyState(VK_DOWN) < 0 && menu_position != elements_max_size) {
menu_position++;
break;
}
if (GetAsyncKeyState(VK_RETURN) < 0) {
std::cin.ignore(10000, '\n');
if (argument == true) {
sleep_for(milliseconds(100));
COORD previous = getcoordinates();
std::cout << "\n\nAre you sure? ";
bool sure = menu_yn(atrribute);
if (sure) {
clear_output(previous);
return menu_position;
}
else {
clear_output(previous);
break;
}
}
else {
return menu_position;
}
}
sleep_for(milliseconds(50));
}
sleep_for(milliseconds(175));
}
}
Function menu_yn():
bool menu_yn(WORD attribute)
{
sleep_for(milliseconds(150));
COORD currentpos = getcoordinates();
int pos = 0;
while (true) {
move(currentpos);
switch (pos) {
case 0:
color(attribute);
std::cout << "YES";
color(FG_WHITE);
std::cout << " NO";
break;
case 1:
std::cout << "YES";
color(attribute);
std::cout << " NO";
color(FG_WHITE);
break;
}
while (true) {
if (GetAsyncKeyState(VK_RIGHT) < 0 && pos != 1) {
pos++;
break;
}
if (GetAsyncKeyState(VK_LEFT) < 0 && pos != 0) {
pos--;
break;
}
if (GetAsyncKeyState(VK_RETURN) < 0) {
std::cin.ignore(10000, '\n');
if (pos == 0) {
return true;
}
else if (pos == 1) {
return false;
}
}
}
sleep_for(milliseconds(175));
}
}
Function validation()
double validation(std::string question, double limit, bool argument)
{
double userInput;
while (true) {
std::cout << question << " >> ";
while (!(std::cin >> userInput)) {
std::cout << "Error: Non-integer input. " << question << " >> ";;
std::cin.clear();
std::cin.ignore(256, '\n');
}
std::cin.ignore(10000, '\n');
while (userInput > limit && limit != 0) {
std::cout << "Error: Input out-of-bounds. " << question << " >> ";
std::cin >> userInput;
std::cin.ignore(10000, '\n');
}
if (argument) {
return (userInput <= 0) ? 1 : userInput;
}
else {
return userInput;
}
}
}
Screenshot of the problem:
There are numbers appearing whenever I press enter on the menu. I suspect that it comes whenever the user presses the arrow keys before entering the enter.

Check space in a string in two loops

In the proccess of creating a user register, I see a double do {} while loop with a same condition. The condition is if detected a space in the strings, the reading will not become available.
void createAccount()
{
unsigned short int i = 0;
bool space = false;
cin.ignore();
cout << "FIRST NAME: ";
getline(cin, fullName[0]);
do {
cout << "MIDDLE NAME: ";
getline(cin, fullName[1]);
for (i = 0; i < fullName[1].size(); i++)
{
if (fullName[i][1] == 32) {
space = true;
break;
}
else {
space = false;
break;
}
}
} while(space);
/*Reset values for the same loop again (I would not like to write 2 times all of this)
i is reseted at loop-for, which helps.*/
space = false;
do {
cout << "MIDDLE NAME: ";
getline(cin, fullName[1]);
for (i = 0; i < fullName[1].size(); i++)
{
if (fullName[i][1] == 32) {
space = true;
break;
}
else {
space = false;
break;
}
}
} while(space);
fullName[3] = fullName[0] + string(" ") + fullName[1] + string(" ") + fullName[2];
}
I really don't know how can I put this together in a same loop. I'm mind broken.
#edit: I'm roger that the right thing is to put fullName[1][i] and that the breaks conditions are wrong.
#edit²: The result:
class BankAccount
{
private:
string fullName[5];
char accountAddress[10];
unsigned short int cards;
float money;
bool visa, mastercard, americanExpress;
void checkName(string name, string typeName, bool exception)
{
unsigned short int errorVar, i;
errorVar = i = 0;
bool space = false;
do {
if (errorVar > 0)
cout << "Enter only the name purposed." << endl << endl;
if (exception)
cout << typeName << " NAME (type no if you haven't): ";
else
cout << typeName << " NAME: ";
getline(cin, name);
for (i = 0; i < name.size(); i++)
{
if (name[i] == ' ')
{
space = true;
break;
}
else
space = false;
}
errorVar++;
} while (space);
if (name.compare("no") == 0)
name = "NULL";
}
public:
void createAccount()
{
cout << endl << "FIRST NAME: "; /* First name has no checks (it can be a compound name) */
getline(cin, this->fullName[0]);
checkName(this->fullName[1], "SECOND", false);
checkName(this->fullName[2], "THIRD", true);
checkName(this->fullName[3], "LAST", false);
if (this->fullName[2].compare("NULL") == 0)
{
this->fullName[4] = this->fullName[0] + string(" ") + this->fullName[1] + string(" ") + this->fullName[3]; //NOT FULL NAME
cout << this->fullName[4];
}
else
{
this->fullName[4] = this->fullName[0] + string(" ") + this->fullName[1] + string(" ") + this->fullName[2] + string(" ") + this->fullName[3]; //NOT FULL NAME
cout << this->fullName[4];
}
}
BankAccount() {/* Constructor */}
~BankAccount() {/* Deconstructor */}
};
Logically it's a simple stuff. Thanks for helping and improve the code also in other areas.
The obvious and clean solution is to put the do-loop in a function (or method), and call it twice, once with fullname[1] and then again with fullname[2].

Password String compare [C++]

Trying to do a password validation method in order to allow a user to use my program.
I was wondering how would someone would implement such a code that allows the program to compare two strings, going down all the letters and numbers to make sure everything matches, and then allows a user to access program controls after successfully entering in a password.
So, for example, if the password is "h3llo" and someone typed in "he" the program will output an error message saying that the password is incorrect.
Here is what I started with:
void checkPassword() {
cout << "Enter password: ";
string password = "H3110W0r1d";
int length = password.length();
int i;
string input;
cin >> input;
for (i = 0; i < length; i ++) {
if ((input[i].compare(length[i]))) == 1) {
if ((input[i].compare(length[i])) == 0) {
cout << "Error! Wrong password!";
} else {
cout << "Welcome!";
}
}
I've tried so many different ways, however, I can't seem to get it working. Any help on what I am doing wrong?
Simply just compare one by one.
void checkPassword() {
cout << "Enter password: ";
string password = "H3110W0r1d";
int length = password.length();
int i;
string input;
bool ok = true;
cin >> input;
if (input.length() != password.length()) {
ok = false;
} else {
for (i = 0; i < length; i ++) {
if (input[i] != password[i]) {
ok = false;
break;
}
}
}
if (ok) {
cout << "Welcome!";
} else {
cout << "Error! Wrong password!";
}
}
Or more simply
void checkPassword() {
cout << "Enter password: ";
string password = "H3110W0r1d";
string input;
cin >> input;
if (input == password) {
cout << "Welcome!";
} else {
cout << "Error! Wrong password!";
}
}

Vaildator for a string on user input

Hey i want to stop the user from entering integers when i ask the user to input a name. I have achieved this for an integer and a char. Can anyone help me adapt my code for a string.
int getNum()
{
int num;
std::cout << "\nWhat is your age? ";
while (!(std::cin >> num))
{
// reset the status of the stream
std::cin.clear();
// ignore remaining characters in the stream
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter an *integer*: ";
}
std::cout << "You entered: " << num << std::endl;
return num;
}
char getChar(string q)
{
char input;
do
{
cout << q.c_str() << endl;
cin >> input;
}
while(!isalpha(input));
return input;
}
string q = "This is a test123";
for(string::iterator i = q.begin(); i != q.end(); i++)
{
if((*i < 'A' || *i > 'z') && (*i != ' '))
{
return false;
}
}
would also be an option, if you allow for spaces and other characters.
Edit: updated for checking a single char:
char c;
bool finished = false;
printf("Please enter your sex, M/F?\n");
while(!finished)
{
cin >> c;
if(!(c == 'm' || c == 'M' || c== 'f' || c=='F'))
{
printf("Please try again...\n");
}
else
{
finished = true;
}
}
Note that c is only input, char by char, when Enter is pressed, before that the line feed does not happen.
If you plan on using std:string, then you can use this to find if the entered string has any digits or not:
if (std::string::npos != s.find_first_of("0123456789"))
{
std::cout << "digit(s)found!" << std::endl;
}