I'm creating a program that is password protected. I have a function with a validation loop called getPassword() that allows the user to type in password attempts as many times as they want, but now I want to limit them to only 3 attempts. Would I be able to do this in the getPassword() function or do I have to create another function?
I've tried making the getPassword() into a do while loop and used a for-loop to inside the do while to count how many times the user attempts entering the password and tried to get it to break when the counter reached 3, but that doesn't seem to get me out of the do while loop. Any suggestions?
void getPassword()
{
int i = 0;
string password = "sup";
string userInput;
int wrongPasswords = 0;
for (int i = 0; i < 3; i++)
{
cout << "Please enter your password: " << endl;
cin >> userInput;
cin.ignore(1000, 10);
while (true)
{
if (userInput != password)
{
cout << "Invalid. Please try again. You can only attempt
password 3 times." << endl;
wrongPasswords++;
break;
}//if
if (wrongPasswords == 3)
break;
}//while
}//for
}//getPassword
Edited code:
void getPassword()
{
string password = "sup";
string userInput;
for (int i = 0; i < 3; i++)
{
cout << "Please enter your password: " << endl;
cin >> userInput;
cin.ignore(1000, 10);
if (userInput == password && i < 3)
break;
}
}//getPassword
You can also try with bool instead of void function. Return true if password was correct, exit program after failing three times. For example,
bool getPassword() {
for ( int attempts = 0; attempts < 3; ++attempts ) {
std::string password;
std::cout << "Enter your password: " << password << std::endl;
std::getline(std::cin, password);
if ( password == "1" ) {
std::cout << "Welcome!";
return true;
}
}
return false;
}
In your main function, call getPassword() function,
int main() {
if ( !getPassword() )
return true;
std::cout << std::endl;
}
This seems more elegant and would have used bool instead of void, unless you have reason not to.
Yet another snippet:
bool tryLogin() {
string pwd = "hello";
string inp;
int tries = 1;
while (true) {
cout << "\nEnter password ";
cin >> inp;
if (inp.compare(pwd) == 0) return true;
++tries;
if (tries > 3) {
cout << "\n Max number of trials exceeded\n";
break;
}
}
return false;
}
int main()
{
cout << endl << (tryLogin() ? "Login successful" : "Can't login") << endl;
}
I'm trying to take user inputted notes and store them in an array. The validation works fine but when I input the last value in the loop I get:
Debug Assertion Failed!
Expression: "(_Ptr_user & (_BIG_ALLOCATION_ALIGNMENT - 1))==0"&&0
An invalid parameter was passed to a function that considers invalid parameters fatal.
I'm struggling to understand where the issue is and how I can fix it.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
typedef string noteName;
noteName getNoteName(int i)
{
bool flag = true;
noteName Namein;
do
{
cout << "Please enter note name no. " << i + 1 << ": ";
cin >> Namein;
cout << "------------------------------------\n";
if (Namein.length() > 3 || Namein.length() < 2)
{
cout << "Sorry, a note name must be 2 or 3 characters long. Please try again.\n";
flag = false;
}
else if (Namein.length() == 3 && Namein[1] != '#')
{
cout << "Sorry, the second character of a sharp note name must be #. Please try again.\n";
flag = false;
}
else if ((Namein[0] < 'a' || Namein[0] > 'g') && (Namein[0] < 'A' || Namein[0] > 'G'))
{
cout << "Sorry, the first character of a note name must be a letter between A and G. Please try again.\n";
flag = false;
}
else if (isdigit(Namein.back()) == false)
{
cout << "Sorry, the last character of a note name must be a number. Please try again.\n";
flag = false;
}
else
{
flag = true;
}
} while (flag == false);
return Namein;
}
int main()
{
const int numNotes = 4;
noteName NoteNames[numNotes];
cout << "Hello\n";
for (int i = 0; i <= numNotes; i++)
{
NoteNames[i] = getNoteName(i);
}
cout << "Thank you, the note names and lengths you entered were: \n\n";
for (int i = 0; i <= numNotes; i++)
{
cout << i << ". " << NoteNames[i] << "\n";
}
cout << "Done!";
return 0;
}
I want to say it's something to do with getNoteName() having a string return type as I haven't had this issue with any of my other functions that return int.
noteName NoteNames[numNotes]; defines an array where NoteNames[numNotes - 1] is the largest element you can access.
You go one further than this. The behaviour on doing that is undefined which is manifesting itself as the crash that you observe.
Replace your loop limits with for (int i = 0; i < numNotes; i++), or similar.
(You also have your CamelCase conventions for class names and variable names switch round from what's normal, which makes your code confusing to read.)
(I'd also rather see constexpr int numNotes = 4;: Google that for more details.)
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <stdlib.h>
#include <string>
using namespace std;
int main()
{
string text[39] = {"A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9","0","Stop",",","?"};
string code[39] = {".-","-...","-.-.","-..",".","..-","--.","....","..",".---","-.-",".-..","--",
"-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",
".----","..---","...--","....-",".....","-....","--....","---..","----.","-----",".-.-.-","--..--","..--.."};
string English, Morse, output_string;
int option, string_size = 0, location;
char again = 'y', letter;
while(again == 'y')
{
system("cls");
cout << "1 - Encode(Text to Morse)\n";
cout << "2 - Decode(Morse Code to Text)\n";
cout << "3 - Display the Morse Code\n";
cout << "4 - Quit\n";
cout << "Enter 1,2,3 or 4:";
cin >> option;
cin.ignore(256,'\n');
system("cls");
switch(option)
{
case 1:
cout << "\nEnter a string with multiple words to encode:";
getline(cin, English);
system("cls");
cout << "\nThe target string to be translated is:" << "\n";
cout << English << "\n";
string_size = English.length();
for(int n = 0; n <= string_size-1; n++)
{
letter = (char)English.at(n);
if(letter != ' ')
{
for(int t = 0; t <=39; t++)
{
if(letter == text[t])
{
cout << code[t] << " ";
break;
}
}
}
else if(letter == ' ')
{
cout << "\n";
}
}
getch();
break;
}
}
}
I didn't finish it yet, but I don't know why I can't run if(letter == text[t]), it says it's an error. how can I fix it? And I have no idea to write the code that Morse to English. how can I know the position of the array that the user entered?
Error message:
error: no match for 'operator==' (operand types are 'char' and 'std::string {aka std::basic_string}')|
You are trying to compare between strings and char.
You need to write the array like that (if you want to use just characters):
char text[39] = {'A','B','C','D','E','F','G','H','I','J','K','L','M'};
and not:
string text[39] = {"A","B","C","D","E","F","G","H","I","J","K","L","M"};
for (int t = 0; t <= 39; t++)
You have 39 items starting at zero index, therefore your loop should go up to (but not including) 39
for (int t = 0; t < 39; t++)
{
...
}
You can declare a temporary string to copy each letter to string. You would also need to make sure text is upper case:
letter = (char)English.at(n);
if (letter != ' ')
{
for (int t = 0; t < 39; t++)
{
std::string temp;
temp = toupper(letter);
if (temp == text[t])
{
cout << code[t] << " ";
break;
}
}
}
If you want the array to be string - then use strcmp() function.
if(strcmp(text[t],letter)==0)
{
cout << code[t] << " ";
break;
}
Have a good luck!
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!";
}
}
So I was to create a program that would verify a password is created correctly. To pass verification, the user must enter 6 or more characters, have at least one lower case letter, one upper case letter, and one digit. The catch is I had to use only the cstring (and of course cctype) library and not the string library. The program runs fine if the user creates an incorrect password, if they successfully create a good password then it sometimes (not sure why sometimes) it crashes. An answer to this, will help me solidify my understanding more on pointers an allocation of memory in terms of it being dynamic. With that here is the program in question.
#include <iostream>
#include <cctype>
#include <cstring>
bool isVerifyAccepted(char*, int);
using namespace std;
int main() {
const int SIZE = 6;
char *userPass = new char[SIZE];
cout << "Verify you have a good password\na good password has to be at least six characters long\n"
<< "have at least on uppercase and one lowercase letter and at least one digit" <<endl <<endl;
cout << "enter a password below" <<endl <<endl;
cin >> userPass;
int userSizePass = strlen(userPass);
char testPassWord[userSizePass];
int count = 0;
while (count < userPass[count]) {
testPassWord[count] = userPass[count];
count++;
}
isVerifyAccepted(testPassWord, userSizePass);
delete[] userPass;
userPass = NULL;
//system("pause");
return 0;
}
bool isVerifyAccepted(char *pass, int size){
bool verify[3];
if(size <= 6){
cout << "Password is too short "<<endl;
return false;
}
for(int i = 0; i<size; i++){
if(islower(pass[i])){
verify[0] = true;
break;
}else{
verify[0] = false;
}
}
for(int i = 0; i<size; i++){
if(isupper(pass[i])){
verify[1] = true;
break;
}else{
verify[1] = false;
}
}
for(int i = 0; i<size; i++){
if(isdigit(pass[i])){
verify[2] = true;
break;
}else{
verify[2] = false;
}
}
if(verify[0] == false){
cout << "You need at least one lowercase letter" << endl;
}
if(verify[1] == false){
cout << "You need at least one uppercase letter" << endl;
}
if(verify[2] == false){
cout << "You need at least one digit" << endl;
}
if((verify[0] == true) && (verify[1] == true) && (verify[2] == true)){
cout << "You have a good password, you met the criteria " <<endl;
}
return verify;
}
Because of this line according to the debugger : const int SIZE = 6.
Increase the size to 32 or some number larger than 6.
Some passwords are longer than 6 characters.