Write a c++ program that requests a password and verifies that it is a valid
password. To be valid the password must be at least 6 characters long and
contain at least one digit.
#include <iostream>
#include <string>
#include <iomanip>
usingnamespace std;
int main()
{
constchar array_size= 7;
char password[array_size]={"Tccd03"};
bool valid,upper;
char input;
int index,len;
cout << "Enter your password: " << endl;
cin >> input;
valid =true;
len =5;
if (*password<input)
{
valid =false;
cout << "The password must be at least 6 characters long" << endl; }
else
upper =false;
index =1;
while (!(index>len))
{
if (input >='A' && input <='Z')
{
upper =true;
}
else
index =index+1;
}
if (upper==true)
else
{
cout << "The password must have at least one upper case character." << endl;
valid =false;
}
if (*password=input)
{
if (valid==true)
{
cout << "That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
return 0;
}
Write a c++ program that requests a password and verifies that it is a valid
password. To be valid the password must be at least 6 characters long and
contain at least one digit.
Sample Output (inputs in bold)
Please enter a password: pass6
Passwords must be at least 6 characters long
Please enter a password: TarrantNW
Passwords must include at least one digit (1-9)
Please enter a password: Tccd03
Thank you, that is a valid password
It keeps telling me I have to have an upper case letter and it dont need to do that.
#include <iostream>
#include <cctype>
#include <string>
#include <ctype.h>
#include <stdio.h>
usingnamespace std;
int main()
{
bool valid,digit;
char userinput[1024];
cout << "Please enter your password: " << endl;
cin >> userinput;
// Below wants to test the userinput for any digits in the string
bool digit_yes=false;
int num = 0;
int len = strlen(userinput);
for (int count = 0; count < len; count++)
if (isdigit(userinput[count]))
digit_yes=true;
if (!digit_yes)
{
valid=false;
constchar password[]={'T','c','c','d','0','3'};
bool verifypass(char *userinput,bool valid) //password verification function
{
valid = (0 == strcmp (userinput,password));
if (valid)
{
cout << "Thanks. That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
}
return 0;
}
int verify_chars(char *userinput,bool valid)//verifies the number of characters
{
int length;
length = strlen(userinput); //checks for number of characters
if ((length <= 6) && (length >= 6))
{
valid=true;
}
else
{
valid =false;
cout << "The password must be at least 6 characters long" << endl;
}
return valid;
}
I am a beginner. Anyway, looking at your code I am not sure where your main is supposed to end. You have the for loop for (int count = 0; count < len; count++) which you probably meant to have curlies as well, {}. Immediately after that you have an if loop if (!digit_yes) which seems to have only half a curly pair. Your bool function bool verifypass(char *userinput,bool valid) returns 0 which I believe is valid, but maybe not what you wanted it to do. The int function int verify_chars returns a bool variable. Neither function has a function prototype before main, and your indenting is all over the place which makes it awkward to read.
#include <iostream>
#include <cctype>
#include <string>
#include <ctype.h>
#include <stdio.h>
usingnamespace std;
constchar password[]={'T','c','c','d','0','3'};
bool verifypass(char *userinput,bool valid) //password verification function
{
valid = (0 == strcmp (userinput,password));
if (valid)
{
cout << "Thanks. That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
}
return 0;
}
int verify_chars(char *userinput,bool valid)//verifies the number of characters
{
int length;
length = strlen(userinput); //checks for number of characters
if ((length <= 6) && (length >= 6))
{
valid=true;
}
else
{
valid =false;
cout << "The password must be at least 6 characters long" << endl;
}
return valid;
}
int main()
{
bool valid,upper;
char userinput[1024];
cout << "Please enter your password: " << endl;
cin >> userinput;
// Below wants to test the userinput for any digits in the string
bool digit_yes=false;
int num = 0;
int len = strlen(userinput);
for (int count = 0; count < len; count++)
if (isdigit(userinput[count]))
digit_yes=true;
if (!digit_yes)
{
valid=false;
cout <<"Passwords must include at least one digit (1-9)"<< endl;
}
else
{
valid=true;
}
verify_chars(userinput,valid);// fuction designed to test the number of characters
verifypass(userinput,valid); // Funtion designed to strcmp the password and userinput
return 0;
}
#include <iostream>
#include <ctype.h>
#include <cctype>
#include <string>
#include <stdio.h>
usingnamespace std;
constchar password[];
bool verifypass(char *userinput,bool valid)
//password verification function
{
valid = (0 == strcmp (userinput,password));
if (valid)
{
cout << "Thanks. That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
}
return 0;
}
int verify_chars(char *userinput,bool valid)
//verifies how many characters
{
int length;
length = strlen(userinput);
//checks for how many characters
if ((length <= 6) && (length >= 6))
{
valid=true;
}
else
{
valid =false;
cout << "The password must be at least 6 characters long" << endl;
}
return valid;
}
int main()
{
bool valid,upper;
char userinput[1024];
cout << "Please enter your password: " << endl;
cin >> userinput;
// This will test input for digits in the string
bool digit_yes=false;
int num = 0;
int len = strlen(userinput);
for (int count = 0; count < len; count++)
if (isdigit(userinput[count]))
digit_yes=true;
if (!digit_yes)
{
valid=false;
cout <<"This passworde has to have at least one digit (1-9)"<< endl;
}
else
{
valid=true;
}
// this function will test the number of characters
verifypass(userinput,valid);
return 0;
}
The reason that it keeps saying the password is invalid is that the verifypass function checks to see if userinput is equal to password, but password has nothing in it.
You don't need to call verifypass, if it has a digit, and its length is >= 6 then it is valid. You can check that in the main routine.
here is what I have I dont understand how to do the above stated items if anyone could please lend me a little help?
#include <iostream>
#include <cctype>
#include <string>
#include <ctype.h>
#include <stdio.h>
usingnamespace std;
constchar password[]={'T','c','c','d','0','3'};
bool verifypass(char *userinput,bool valid) //password verification function
{
valid = (0 == strcmp (userinput,password));
if (valid)
{
cout << "Thanks. That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
}
return 0;
}
int verify_chars(char *userinput,bool valid)//verifies the number of characters
{
int length;
length = strlen(userinput); //checks for number of characters
if ((length <= 6) && (length >= 6))
{
valid=true;
}
else
{
valid =false;
cout << "The password must be at least 6 characters long" << endl;
}
return valid;
}
int main()
{
bool valid,upper;
char userinput[1024];
cout << "Please enter your password: " << endl;
cin >> userinput;
// this section is to verify uppercase charaters in userinput string
{
bool upper = false;
constint l = strlen(userinput);
for(int i = 0; i < l; ++i)
{
upper = (isupper(userinput[i]) != 0);
if(upper)
break;
}
bool valid = upper;
if(!upper)
cout << "The password must have at least one upper case character." << endl;
}
// Below wants to test the userinput for any digits in the string
bool digit_yes=false;
int num = 0;
int len = strlen(userinput);
for (int count = 0; count < len; count++)
if (isdigit(userinput[count]))
digit_yes=true;
if (!digit_yes)
{
valid=false;
cout <<"Passwords must include at least one digit (1-9)"<< endl;
}
else
{
valid=true;
}
verify_chars(userinput,valid);// fuction designed to test the number of characters
verifypass(userinput,valid); // Funtion designed to strcmp the password and userinput
return 0;
Ok I am really stumped on this program if anyone could please help me I guess there is something I am not getting........ This is past do :(
here is the assiment Write a c++ program that requests a password and verifies that it is a valid
password. To be valid the password must be at least 6 characters long and
contain at least one digit.
Sample Output (inputs in bold)
Please enter a password: pass6
Passwords must be at least 6 characters long
Please enter a password: TarrantNW
Passwords must include at least one digit (1-9)
Please enter a password: Tccd03
Thank you, that is a valid password and here is what I got
#include <iostream>
#include <ctype.h>
#include <cctype>
#include <string>
#include <stdio.h>
usingnamespace std;
char password[6];
constint SIZE = 6;
bool verifypass(char *userinput,bool valid)
//password verification function
{
valid = (0 == strcmp (userinput,password));
if (valid)
{
cout << "Thanks. That is a valid password." << endl;
}
else
{
cout << "Invalid password" << endl;
}
return 0;
}
int verify_chars(char *userinput,bool valid)
//verifies how many characters
{
int length;
length = strlen(userinput);
//checks for how many characters
if ((length <= 6) && (length >= 6))
{
valid=true;
}
else
{
valid =false;
cout << "The password must be at least 6 characters long" << endl;
}
return valid;
}
int main()
{
bool valid,upper;
char userinput[1024];
cout << "Please enter your password: " << endl;
cin >> userinput;
// This will test input for digits in the string
bool digit_yes=false;
int num = 0;
int len = strlen(userinput);
for (int count = 0; count < len; count++)
if (isdigit(userinput[count]))
digit_yes=true;
if (!digit_yes)
{
valid=false;
cout <<"This passworde has to have at least one digit (1-9)"<< endl;
}
else
{
valid=true;
}
// this function will test the number of characters
verifypass(userinput,valid);
return 0;
}
I would completely remove your verifypass function. You are using strcmp to compare 2 strings when 1 of the strings (password) does not have anything inside of it, as mentioned earlier. You already have stuff checking for 1 digit and at least 6 characters anyway.
The problem is here if ((length <= 6) && (length >= 6))
That is basically saying if the length is equal or less than 6 'and' greater or equal to 6 (which isn't what you want it to do). Change that to if(length >= 6) Then all you need to do is call the verify_chars function in main (there is no need to pass a bool into it if you are returning a bool, you might as well define 'valid' in the function.
ok I changed the if ((length <= 6) && (length >= 6) to if(length >= 6) I took the verify password funciton out. I am not sure what exactly about no need to pass a bool into it if returning a bool. I am sorry I am not very good at this but I am tring.
It is stil telling me invalid password.
#include <iostream>
#include <ctype.h>
#include <cctype>
#include <string>
#include <stdio.h>
usingnamespace std;
char password[6];
constint SIZE = 6;
int verify_chars(char *userinput,bool valid)
//verifies how many characters
{
int length;
length = strlen(userinput);
//checks for how many characters
if(length >= 6)
{
valid=true;
}
else
{
valid =false;
cout << "The password must be at least 6 characters long" << endl;
}
return valid;
}
int main()
{
bool valid,upper;
char userinput[1024];
cout << "Please enter your password: " << endl;
cin >> userinput;
// This will test input for digits in the string
bool digit_yes=false;
int num = 0;
int len = strlen(userinput);
for (int count = 0; count < len; count++)
if (isdigit(userinput[count]))
digit_yes=true;
if (!digit_yes)
{
valid=false;
cout <<"This passworde has to have at least one digit (1-9)"<< endl;
}
else
{
valid=true;
}
return 0;
}
bool verify_chars(char *userinput)
{
bool valid; // variable returned to see if correct or not
int length= strlen(userinput);
if (length >= 6)
{
valid=true;
}
else
{
valid =false;
cout << "The password must be at least 6 characters long" << endl;
}
return valid;
}
int main()
{
bool valid,upper;
char userinput[1024];
cout << "Please enter your password: " << endl;
cin >> userinput;
bool digit_yes=false;
int len = strlen(userinput);
for (int count = 0; count < len; count++) // Make sure your for loop has braces to keep everything organized
{
if (isdigit(userinput[count]))
digit_yes=true;
}
if (!digit_yes)
{
valid=false;
cout <<"This passworde has to have at least one digit (1-9)"<< endl;
}
else
{
valid=true;
}
// Makes sure the value returned from verify_chars is true (it calls the function) and makes sure valid is true from above.
if((verify_chars(userinput) == true) && (valid == true))
{
cout << "Correct password";
}
return 0;
}
Of course this could be optimized (like isdigit could be moved into a function etc). But this is the general idea.
I would also read through some of the replies in the thread, as they do provide some solutions like mine.
You will probably want to add some while loops as well, so that it will keep repeating until they enter in the correct input.