Line 11: string(Pass);
What's this trying to do? Are you trying to create a string object, named Pass?
Line 19: testLength(string(Pass), valid);
Is this an attempt to call the function testLength?
Anyway, I see you're using the sizeof function to find the size of something. This is very much completely incorrect for your purposes. The sizeof function does not tell you how many characters are in a string object. Perhaps you're looking for something like this:
but even so, you're creating so many string objects here (every time you have string(Pass) you're creating a whole new, unnamed temporary string object) that I don't know what to expect and I'm surprised it compiles.
You need to go back and review how to create variables, and how to call functions.
This shouldn't even compile. Let's look at line 11.
string(Pass);
You're providing Pass (presumably another string object) to the constructor of an anonymous string. Since Pass doesn't exist, compilation should stop right there. Any following usage of string(Pass) is equally as invalid and cryptic.
This is how you instantiate a string with the identifier "Pass" :
std::string Pass;
In addition, don't use sizeof() to try and determine the number of characters in a string (or any standard container for that matter) - sizeof() generates the size of a variable or datatype (measured in the number of chars it would take on that system to store it). You're getting all kinds of data members and things you don't want. std::string has a size() method for that.
Take a look at this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <string>
int main() {
std::string string_long = "this is a test hello world!!!";
std::string string_short = "this is a test";
std::cout << "long.size():\t" << string_long.size() << std::endl;
std::cout << "sizeof(long):\t" << sizeof(string_long) << std::endl;
std::cout << "short.size():\t" << string_short.size() << std::endl;
std::cout << "sizeof(short):\t" << sizeof(string_short) << std::endl;
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
int testLength(string pass);
int testCaps(string pass);
int testNum(string pass);
int main()
{
string Pass;
bool valid = false;
while (!valid)
{
cout << "Please enter a password:";
cin >> Pass;
valid = testLength(Pass);
if (valid)
{
valid = testCaps(Pass);
if (valid)
{
valid = testNum(Pass);
if (valid)
{
break;
}
else
{
cout << "Passwords must include at least one number" << endl;
}
}
else
{
cout << "Passwords must include at least one uppercase letter" << endl;
}
}
else
{
cout << "Passwords must be at least 7 characters long" << endl;
}
}
cout << "Thank you, that is a valid password" << endl;
return 0;
}
int testLength(string pass)
{
if (pass.length() >= 7)
{
returntrue;
}
else
{
returnfalse;
}
}
int testCaps(string pass)
{
int i = 0;
while (i < pass.length())
{
if (pass[i] >= 'A' && pass[i] <= 'Z')
{
returntrue;
}
i = i + 1;
}
returnfalse;
}
int testNum(string pass)
{
int i = 0;
while (i < pass.length())
{
if (pass[i] >= '0' && pass[i] <= '9')
{
returntrue;
}
i = i + 1;
}
returnfalse;
}