So, I have to encrypt my console application with a password, i did something that's working but there is a problem, backsapce doesn't erase the character entered, it is also counted as a character, how can I make it to do its job, to erase the character?
This is the code:
void main()
{
char password[20], my_password[20] = "password";
int i;
char ch;
system("cls");
cout << "PASSWORD: ";
i = 0;
do
{
ch = _getch();
password[i] = ch;
if (ch != 27 && ch != 13 && ch != 9)
cout<<"*";
else
break;
i++;
} while (i < 19);
password[i] = '\0';
if (strcmp(password, my_password) != 0)
{
cout << "\n\nIncorrect password !!!";
cin.get();
return;
}
cout << "\n\nPassword is correct !";
cout <<"\n\nThe program is executed !";
cin.get();
}
void main()
{
char password[20], my_password[20] = "password";
int i;
char ch;
system("cls");
cout << "PASSWORD: ";
i = 0;
do
{
ch = _getch();
if (ch == 8)
{
i--;
cout << "\b \b";
continue;
}
password[i] = ch;
if (ch != 27 && ch != 13 && ch != 9)
cout << "*";
else
break;
i++;
} while (i < 19);
password[i] = '\0';
if (strcmp(password, my_password) != 0)
{
cout << "\n\nIncorrect password !!!";
cin.get();
return;
}
cout << "\n\nPassword is correct !";
cout << "\n\nThe program is executed !";
cin.get();
}
Not the cleanest code but it works. Decrement the counter to over write the previous character and output two backspace characters separated by a space.
"how can I make it to do its job, to erase the character?"
Use a curses library. Like ncurses.
You could check if the character received is a backspace, if it is decrement i which will effectively remove the last character.
i = 0;
do
{
ch = _getch(); // get the character
if(ch == DEL || ch == BS) // check for backspace
{
i--;
cout << BS;
}
else if(ch >= ' ' && ch <= '~') // check if its valid ASCII
{
password[i] = ch;
cout << "*";
i++;
}
else if (ch == 27 || ch == 13 || ch == 9) // check if entry is complete
{
break;
}
} while (i < 19);
password[i] = '\0';
somewhere else
#define BS '\b'
#define DEL 127
Related
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.
The program takes in a word given by the user and translates that to pig latin. I've gotten everything to work almost perfectly, but have run into two bugs. The first of which is when translating words that begin with consonants say "count", the output is "ounttcay" instead of "ountcay". The second bug is that when for three letter words like "egg" or "not" the output is "egg_\377ay" or "ottn\377ay". Is there a simple way to remove that duplicate character and get rid of those numbers?
Note - Unfortunately it has to be done using a Cstring
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int convertToPigLatin(char arr[50]);
bool isVowel(char ch);
int main() {
char userInput[50];
char answer = ' ';
do {
cout << "Enter a word to convert it to pig latin" << endl;
cin.getline(userInput, 50); //get user input
cout << "Your entered word is " << userInput << endl;
convertToPigLatin(userInput); //translate user's input into piglatin
cout << "Would you like to convert another word?" << endl;
cin >> answer;
cin.ignore(); //clear past user input
cin.clear();
} while (answer == 'Y' || answer == 'y');
return 0;
}
bool isVowel (char ch) {
switch (tolower(ch)) { //if the first character of the given input is a vowel
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
default:
return false;
}
}
int convertToPigLatin(char arr[50]) {
char newArr[50];
// string conjunctions[6] = {"and","but","for","nor","yet","the"}; //list of conjunctions not to be converted
size_t arrLength = strlen(arr); //holds length of input
for (int i = 0; i < arrLength; i++) { //make sure all characters in input are lower case for easier processing
newArr[i] = tolower(arr[i]);
}
char lastChar = newArr[0]; //save the first character in case it needs to be appended
if (atoi(arr) || arr[0] == '\0') { //if the input contains a number or begins with a null character print an error
cout << "Cannot translate inputs that contain numbers" << endl;
return -1;
} else if (arrLength <= 2) { // if the input is 2 or less characters
cout << newArr << endl; //print the input as is
cout << "Boring! Try somthing more than 2 characters long" << endl;
return 0;
} else if ((strstr(newArr, "and") && arrLength == 3) || (arrLength == 3 && strstr(newArr, "but")) || (arrLength == 3 && strstr(newArr, "for")) || (arrLength == 3 && strstr(newArr, "nor")) || (arrLength == 3 && strstr(newArr, "yet")) || (arrLength == 3 && strstr(newArr, "the"))) { //if the input is more than 2 characters long
cout << newArr << endl; //print the input as is
cout << "No conjucntions try again!" << endl;
return 0;
} else { //if the given input is three characters and is not a conjunction, being translation
if (isVowel(arr[0])) { //check if input's first character is a vowel
cout << "Your word in piglatin is "<< strcat(newArr, "ay") << endl; //print that string with 'ay' at the end (i.e. egg'ay')
return 0;
} else { //else if the given input starts with a consonant
for (int r = 1; r < arrLength; r++) {
newArr[r-1] = newArr[r];
newArr[arrLength] = lastChar;
}
cout << "Your word in piglatin is " << strcat(newArr, "ay") << endl;
return 0;
}
}
return 0;
}
You're not terminating newArr, and the last index of the input string is arrLength - 1.
int convertToPigLatin(char arr[50]) {
// Make sure newArr is properly terminated.
char newArr[50] = {0};
// [...]
} else { //else if the given input starts with a consonant
for (int r = 1; r < arrLength; r++) {
newArr[r-1] = newArr[r];
}
// Do this outside the loop.
newArr[arrLength-1] = lastChar;
// No need for strcat here.
cout << "Your word in piglatin is " << newArr << "ay" << endl;
}
}
return 0;
}
You need to add the '\0' at the end of newArr because strlen does not count it so you are not copying it. strcat replaces '\0' witn 'ay\0' but you have no '\0'.
for (int r = 1; r < arrLength; r++) {
newArr[r-1] = newArr[r];
newArr[arrLength] = lastChar;
}
newArr[arrLength+1] = '\0';
cout << "Your word in piglatin is " << strcat(newArr, "ay") << endl;
I am working on a project right now and when I try to run what I have below it gives me an error that says "uninitialized local variable 'userOption' used" on line 22, while (isValidOption(userOption) == true) {.
How do I fix that error? Thank you.
#include<iostream>
#include <string>
using namespace std;
char toupper(char ch) {
if (ch >= 'A'&&ch <= 'Z')
return(ch);
else
return(ch - 32);
}
bool isValidOption(char ch) {
if (ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X')
return(true);
else
return(false);
}
char getMainOption() {
string UserInput;
char userOption;
while (isValidOption(userOption) == true) {
cout << "Choose One of the following options\n";
cout << "I--List Our Inventory\n";
cout << "O--Make an Order\n";
cout << "L--List all Orders made\n";
cout << "X--Exit\n";
cout << "Enter an option: ";
getline(cin, UserInput);
userOption = toupper(UserInput[0]);
if (!isValidOption(userOption)) {
cout << "Invalid String\n";
cout << "Enter an option: ";
getline(cin, UserInput);
userOption = toupper(UserInput[0]);
}
if (userOption == 'I')
cout << "Listing Our Inventory\n";
else if (userOption == 'O')
cout << "Make an order\n";
else if (userOption == 'L')
cout << "Listing all orders\n";
}
return userOption;
}
int main() {
char choice;
choice = getMainOption();
system("pause");
return 0;
}
What the error is saying that you're trying to read from userOption before you've ever written to it. If a variable is uninitialized, its memory contents will be full of junk left behind by other functions and it can easily cause bugs. In your case, you'll want to read input from the user into userOption before you do any logic on it. This can be done with a do-while loop:
char userOption; // not yet initialized
do {
...
cin >> userOption; // userOption gets initialized here on first loop run
} while (isValidOption(userOption)); // no need for == true, that's a tautology :-)
// NOTE: perhaps you want to loop while the input is INvalid, as in
// while (!isValidOption(userOption)); ?
A couply code-review comments I would additionally give are:
std::toupper already exists in <cctype>. Docs are here
return is not a function call and it's better to write return ch; than return(ch);
if (ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X'){ return true; } else { return false; } is completely equivalent to the shorter return ch == 'I' || ch == 'O' || ch == 'L' || ch == 'X';
Also take a look at system(“pause”); - Why is it wrong?
Happy coding! Let me know if questions remain
I'm making a software as an ATM, so when the user try to enter the password the user only sees *******, but when trying to delete it doesn't delete a character. It just adds a new one. Here's my code:
string password2 = "";
cout << "PASSWORD: ";
ch = _getch();
while(ch != 13) //character 13 is enter
{
password2.push_back(ch);
cout << '*';
ch = _getch();
}
And also, I try to use pop_back(); but it doesn't work either. Can anybody help me please?
Just did a little magic and created this:
I must admit, I don't like the style... But it "works" !
#include<iostream>
#include<string>
#include <conio.h>
bool verify_pass(const std::string& pass)
{
std::string input = "";
char ch = '0';
while(true)
{
ch = getch();
if(ch == '\b')
{
if(input.size() > 0)
{
input.erase(input.begin() + input.size() - 1); // erase last char
std::cout << "\b \b";
}
}
else if(ch != '\r\n' && ch != '\n' && ch != '\r')
{
input += ch;
std::cout << '*';
}
else
break;
}
return input == pass;
}
int main()
{
std::string insecurePass = "1234";
std::cout << "Enter Password: ";
if(verify_pass(insecurePass))
std::cout << "\nCorrect!\n";
else
std::cout << "\nFalse!\n";
}
By the way, you can use the vector like I used the string, but use push_back instead of += and pop_back() should work too instead of my method with erase.
I'm a C++ beginner and wrote a program to check if two phrases are anagrams. The characters are being read one at a time and stored in an array. I have everything working, except in some cases, extra characters are being inserted into the array.
For example, if I enter the phrases aabb and abba, this is the output of the program:
Enter two lines that might be anagrams:
--> aabb
--> abba
String A is aabb
String B is abbai?
The two strings are NOT anagrams.
They should be anagrams, but for some reason, i? is added into the array, causing the phrases to not be anagrams. I'm probably overlooking a simple mistake in the code, but would really appreciate any feedback.
Here is the code:
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int check_anagram(char [], char []);
int main()
{
char ch, a[60], b[60];
int flag, i;
cout << "Enter two lines that might be anagrams:" << endl;
cout << "--> ";
cin.get(ch);
ch = tolower(ch);
i = 0;
while (ch != '\n')
{
if (ch > '#') {
a[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
cout << "--> ";
cin.get(ch);
ch = tolower(ch);
i = 0;
while (ch != '\n')
{
if (ch > '#') {
b[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
flag = check_anagram(a, b);
cout << "String A is " << a << endl;
cout << "String B is " << b << endl;
cout << "The two strings ";
if (flag == 1)
cout << "ARE";
else
cout << "are NOT";
cout << " anagrams." << endl << endl;
return 0;
}
int check_anagram(char a[], char b[])
{
int first[26] = {0}, second[26] = {0}, c = 0;
while (a[c] != '\0')
{
first[a[c]-'a']++;
c++;
}
c = 0;
while (b[c] != '\0')
{
second[b[c]-'a']++;
c++;
}
for (c = 0; c < 26; c++)
{
if (first[c] != second[c])
return 0;
}
return 1;
}
Thanks in advance!
You just need to terminate the two character arrays with '\0' because the logic in check_anagram treats both arrays as NULL-terminated.
..
while (ch != '\n')
{
if (ch > '#') {
a[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
a[i] = '\0'; // <<<<<<<< Add this line
cout << "--> ";
cin.get(ch);
ch = tolower(ch);
i = 0;
while (ch != '\n')
{
if (ch > '#') {
b[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
b[i] = '\0'; // <<<<<<<< Add this line
..
Here is the result:
Enter two lines that might be anagrams:
--> aabb
--> abba
String A is aabb
String B is abba
The two strings ARE anagrams.