String Verifier char loop wont work properly - c++

I can't get my password verify program to work. My loop seems to only iterate once, I just put "it" as output to see if it's constantly iterating, but its not. I'm not sure why, the booleans are working, but it just iterates once, and if my first letter is a lowercase, then it'll say I need an uppercase and a digit, and vice-versa if my first character is a digit or uppercase. This is a homework assignment, but I'm a little lost. Any help would be greatly appreciated.
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
const int LENGTH = 20;
char pass[LENGTH];
cout << "Enter a password, that's at least 6 characters long, one uppercase, one lowercase letter ";
cout << " and one digit." << endl;
cin.getline(pass,LENGTH);
bool isdig = true;
bool isdown = true;
bool isup = true;
bool correct = false;
for(int index = 0; correct == false; index++)
{
cout << "it" << endl;
if(isupper(pass[index]) == 0)
{isup = false;}
if(islower(pass[index]) == 0)
{isdown = false;}
if(isdigit(pass[index]) == 0)
{isdig = false;}
if(isdig == true && isup == true && isdown == true)
{correct = true;}
if(index = LENGTH - 1)
{
if(isdig == false)
{cout << "Your password needs a digit." << endl;}
if(isup == false)
{cout << "Your password needs an uppercase letter." << endl;}
if(isdown == false)
{cout << "Your password needs a lowercase letter." << endl;}
cout << "Re-enter another password. " << endl;
cin.getline(pass,LENGTH);
index = 0;
isdown = true;
isup = true;
isdig = true;
}
}
system("pause");
return 0;
}

The problem is probably this line:
if(index = LENGTH - 1)
Here you assign the value of LENGTH - 1 to index, so you are always asked to re-enter your password as that expression always is true.

You should enable your compiler warning (-Wall if you are using g++) and pay attention to the warnings:
es.cpp:52:30: warning: suggest parentheses around assignment used as truth value
This tells you that some condition (a==b) as probably been written as (a=b) which is an assignment. And indeed
if(index = LENGTH - 1)
should be written
if (index == LENGTH - 1)
Also for readability
if(isdig == true && isup == true && isdown == true)
could be replaced by
if (isdig and isup and isdown)
and
if(isdig == false)
by
if (not isdig)

Related

Password requirements

I'm trying to declare this string as Invalid but in an input like this:
59G71341 or 8pjf7h14sx13 or 60s1v344
My output is getting approved through my string if statement and is getting listed as Valid.
Could anyone guide me to why its passing through my if statement and labeling Valid!!
I haven't learned how to use a debugger yet so bare with me.
Task description:
Declare a Boolean variable named goodPasswd. Use goodPasswd to output "Valid" if secretStr contains no more than 5 digits and secretStr's length is greater than or equal to 5, and "Invalid" otherwise.
Ex: If the input is 80796, then the output is:
Valid
Ex: If the input is XBdg, then the output is:
Invalid
#include <iostream>
using namespace std;
int main()
{
string secretStr;
bool goodPasswd = false;
cin >> secretStr;
int counter = 0;
for (int i = 0; i < secretStr.length(); ++i)
{
if ((secretStr[i] >= 0) && (secretStr[i] <= 9))
{
++counter;
}
} //approves string if both true
if ((counter <= 5) && (secretStr.length() >= 5))
{
goodPasswd = true;
}
else
{
goodPasswd = false;
}
if (goodPasswd)
{
cout << "Valid" << endl;
}
else
{
cout << "Invalid" << endl;
}
return 0;
}
if ((secretStr[i] >= 0) && (secretStr[i] <= 9))
should be
if ((secretStr[i] >= '0') && (secretStr[i] <= '9'))
0 and 9 are integers, but you are comparing characters, so you need to use the characters '0' and '9', or you could just use the isdigit function.
if (isdigit(secretStr[i]))
isdigit is declared in #include <cctype>
Not related to your question but you don't need to goodPasswd variable. Simply
if (counter <= 5 && secretStr.length() >= 5)
{
cout << "Valid" << endl;
}
else
{
cout << "Invalid" << endl;
}
seems a bit cleaner to me.

How to break infinite loop in my password validation program? [duplicate]

This question already has answers here:
Breaking out of an infinite loop
(2 answers)
do ... while loop not breaking c++
(2 answers)
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I'm just starting C++ and creating a simple password validation program. [edit] My goal is to display each messages if there is a lack of uppercase, lower case, digits, and/or special characters.
If there is only special character the output will be like this:
Enter your password: !
Password must contain a lower case!
Password must contain an upper case!
Password must contain a digit!
Enter your password:
what i want if all the requirements are met:
Enter your password: 1qQ+
your password is created
program is finished
But what i got is infinite loop of "your password is created".
Any solution or alternative to make my code better/efficient?
Sorry for bad intendation.
#include <iostream>
#include <string>
using namespace std;
void passcheck(string& password)
{
bool hasUpper = false, hasLower = false, hasDigit = false, hasSpecialchar = false;
for (int i = 0; i < password.length(); ++i)
{
if (islower(password[i]))
{
hasLower = true;
}
else if (isupper(password[i]))
{
hasUpper = true;
}
else if (isdigit(password[i]))
{
hasDigit = true;
}
else if (password.find(" !#$%&'()*+,-.:;<=>?#[]^_`{|}~"))
{
hasSpecialchar = true;
}
}
do
{
if (!hasLower)
{
cout << "Password must contain a lower case!"<< endl;
}
if (!hasUpper)
{
cout << "Password must contain an upper case!"<< endl;
}
if (!hasDigit)
{
cout << "Password must contain a digit!"<< endl;
}
if(!hasSpecialchar)
{
cout << "Password must contain special char!"<< endl;
}
else
{
cout << "your password is created" << endl;
}
}while(hasSpecialchar && hasDigit && hasLower && hasUpper);
}
int main(int argc, char const *argv[])
{
string password;
do{
cout << "Enter your password: ";
getline(cin, password);
passcheck(password);
}while(true);
cout << "program is finished" << endl;
cin.get();
return 0;
}
You want to execute the loop until all the boolean expressions are true I assume. This would evaluate to
do{
//your stuff
} while(!(hasSpecialchar && hasDigit && hasLower && hasUpper))`
But depending on what exactly you're trying to do there's probably a better approach then a do-while loop. Where do you set these booleans? If you're using a single thread, then this values aren't gonna change inside the loop and the whole loop doesn't make any sense.
you can use break keyword, like this
while (1) {
...
if (cond) {
break
}
}

Testing word is in language L = {a^n-1b^n} using stack

I am writing a C++ function that implements a stack to test if a word is in language L = {a^n-1b^n}. Words that exist include b, abb, aabbb.
I have seen many postings with regards to testing a word being in a language where L = {a^nb^n} and I am just wondering how to go about this when the n's are not equal.
The code that I have written works but I feel that I am taking the easy way out with regards to making sure the word is not in a format like aba bababa abababa etc. I have implemented this test outside of the stack but feel there should be a way to do it in the stack.
The reason I say this is because I later have to take the function bool isInLanguageL(string w) and use the following header instead bool isInLanguageL(queueType &w). I have been having trouble trying to convert the code I previously had with string w to queue w.
//function prototype
bool isInLanguageL(string w);
//function to see if the word exists in the language
bool isInLanguageL(string w) {
//creating counters for the a's and b's
int countPush = 0;
int countPop = 0;
reverse(w.begin(), w.end()); //putting the b's infront
//creating the charachter stack
stack<char> charStack;
//checking the word for an illegal format such as aba or bab
for (int i = 0; i < w.length(); i++) {
if ((w[i] == 'a' && w[i + 1] == 'b' && w[i+2] == 'a') || (w[i] == 'b' && w[i + 1] == 'a' && w[i + 2] == 'b')) {
cout << "This is an illegal format of a word in language L and therefore: ";
return false;
}
else { //if the word is a legal format then continue onto stack creation
//adding the b's to the stack and counting them simultaneously
if (w[i] == 'b') {
charStack.push(w[i]);
countPush++;
}
//popping off the stack everytime there is an a and counting simultaneously
else if (w[i] == 'a') {
charStack.pop();
countPop++;
}
}
}
//if the stack is not empty and a = b-1 then the word exists
if (!charStack.empty() && countPop == (countPush - 1))
return true;
else
return false;
}
int main() {
//introduction
cout << "The language rule is L = {a^n-1b^n}" << endl;
//creating an empty string for the word and condition variable for exit
string word = " ";
int exit = 0;
while (exit == 0) {
//getting word from the user
cout << "\nWord: ";
cin >> word;
//calling function to check if word exists in L
if (isInLanguageL(word)) {
cout << "The word exists in language L" << endl << endl;
}
else
cout << "The word doesn't exist in language L" << endl << endl;;
cout << "Would you like to exit (1-yes/0-no): ";
cin >> exit;
}
system("pause");
return 0;
}
If anyone can just lead me in the right direction with regards to testing for whether the word is of a legal format (not aba abababa bababa), it would be greatly appreciated.

c++ Validating a Double in a String

Please excuse any amateur mistakes, I'm still quite new to this. For my class we need to convert a double input to a string to use in a different part of the project. The verification for the integers worked just fine and I attempted using some of the code from this previous question Validating a double in c++
though, much to my chagrin it did not work.
Here is my code:
string input;
bool valid;
double num;
//Verification of valid inputs
do
{
valid = true;
cout << "What is the " << name << " rate of the population? (% per year): ";
getline(cin, input);
num = input.length();
if (num == 0)
{
cout << "\nNo data was entered, please enter a number.\n";
valid = false;
}
else
{
for (double i = 0; i < num; i++)
{
if (input.at(i) < '0' || input.at(i) > '9')
{
cout << "\nPlease enter a valid, positive number.\n";
valid = false;
break;
}
}
}
} while (valid == false);
return stod(input);
Thanks.
Edit:
I just found this How do i verify a string is valid double (even if it has a point in it)? and I can safely say I have no idea what is going on.
If you are really keen to do it manually, take a look at the following methods:
How to manually parse a floating point number from a string
https://www.daniweb.com/programming/software-development/code/217315/c-function-stod-string-to-double-convert-a-string-to-a-double
and read the comments. It's not quite straightforward, but it should work. Or even better, check the code of stod from C++.
Why are you using a double as an index for the string? Also, I'd skip using a ++ for increment a double.
Figured it out.
Just had to add something to an if statement.
if ((input.at(i) < '0' || input.at(i) > '9') && input.at(i) != '.')
{
cout << "\nError! illegal character was entered.\n";
valid = false;
break; // 12w345
if (input.at(i) == '.')
ct++;
if (ct > 1)
{
cout << "Error! Only one dot allowed.";
valid = false;
With ct being an integer with value 0 to count the dots and ensure only one was entered.

Getting expected unqualified-id error with C++ when trying to complie

I'm working on an assignment for class and keep getting an "expected unqualified-id" for the "{" after defining my bool types in the function. I can't figure out why I'm getting this error and it is making it hard to get my assignment done without being able to run my program. Can anyone tell me why I'm getting this error? Here is my code
//Page 825 Problem 12
#include <iostream>
#include <string>
using namespace std;
//Function Prototype
bool testPassword(char []);
const int passLength = 21;
char password[passLength];
int main()
{
//Ask user to enter password matching the following criteria
cout << "Please enter a password at six characters long. \n"
<< "Password must also contain at least one uppercase and one lowercase letter. \n"
<< "Password must also contain at least one digit. \n"
<< "Please enter your password now \n";
cin.getline (password, passLength);
if (testPassword(password))
cout << "Password entered is of the correct format and has been accepted.";
else
cout << "Password does not meet criteria \n";
return 0;
}
//*******************************
//**Function to test password ***
//**to determine if it meets ***
//**criteria listed ***
//*******************************
//Test password to determine if it is at least six characters long
bool testPassword (char password[]);
bool lower;
bool upper;
bool digit;
bool length;
{
if (strlen(password) < 6)
length = true;
else
length = false;
cout << "Password must be at least 6 characters long.\n";
for (int k = 0; k < passLength; k++)
{
if (islower(password[k])
lower = true;
else
lower = false;
cout << "Password must contain a lowercase letter.\n";
if (isupper(password[k])
upper = true;
else
upper = false;
cout << "Password must contain an uppercase letter.\n";
if (isdigit(password[k])
digit = true;
else
digit = false;
cout << "Password must contain a digit.\n";
}
if (lower && upper && digit && length == true)
return true;
else
return false;
}
testPassword: has a ";" and the end of line that it shouldn't have.
The bool variables need to be inside the first "{", not before it.
It sounds like you actually want this:
bool testPassword (char password[])
{
bool lower;
bool upper;
bool digit;
bool length;
if (strlen(password) < 6) {
length = true;
}
else {
length = false;
cout << "Password must be at least 6 characters long.\n";
}
...
NOTES:
"testPassword()" with a ";" is a FUNCTION PROTOTYPE (not an actual function definition)
Unlike Python, just indenting doesn't make a conditional block. If you want more than one line in the block, you need curly braces.
This part is placed within global scope:
bool testPassword (char password[]); // <-- declaration of function
bool lower; // <-- global variables
bool upper;
bool digit;
bool length;
{ // <-- start of the scope? what does it belong to?
...
and it is invalid, you can not place program's logic in global scope... functions can not be just called "out of nowhere"... if it was supposed to be the body of testPassword function already, it should be:
bool testPassword (char password[])
{
bool lower;
bool upper;
bool digit;
bool length;
...
}