Hey guys I have been at this for a while and I keep running into trouble on verifying if a number is contained within a given string.
Could you guys please kick my butt in the right direction or hint.
#include <iostream>
#include <string>
using namespace std;
int validation(string password)
{
if (password.size() < 7)
{
return 2;
}
char characters;
int valid = 0;
for (int i = 0; i < password.size(); ++i)
{
characters = password[i];
if (toupper(characters) == password[i])
{
valid = 1;
}
}
if (valid == 0)
{
return 3;
}
valid = 0;
///****THIS IS WHERE I AM TRYING TO INPUT A VALIDATION FOR THE NUMBER******
return 1;
}
int main()
{
string password;
while (true)
cout << "Please enter a password: ";
cin >> password;
if (validation(password) == 1)
{
cout << "Thank you, that is a valid passowrd." << endl << endl;
}
else if (validation(password) == 2)
{
cout << "ERROR: Password must be at least 7 characters long. Please, try again." << endl << endl;
}
else if (validation(password) == 3)
{
cout << "ERROR: Password must include at least one uppercase letter. Please, try again." << endl << endl;
}
else if (validation(password) == 4)
{
cout << "ERROR: Password must contain at lease one number. Please, try again." << endl << endl;
}
return 0;
}
#include <iostream>
usingnamespace std;
int main (){
// get user input
string user_input;
cout << "Please enter a password" << endl;
cin >> user_input;
// loop over string char by char and keep count of loop
int str_pos = 0;
for(char& c : user_input) {
// if a digit do things..
if(isdigit (c)){
// printing out to prove it works but this might be weird if you have more than one digit
cout << "There is a digit in your password" << endl;
cout << "It is located in postion [" << str_pos <<"] of your input" << endl;
}
str_pos++;
}
return 0;
}