N/A

N/A
Last edited on
There is a lot of misunderstanding in this code.

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:

http://www.cplusplus.com/reference/string/string/size/

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.
Last edited on
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;
}


Edit* - Moschops the ninja
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>

using namespace 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)
	{
		return true;
	}
	else
	{
		return false;
	}

}

int testCaps(string pass)
{
	int i = 0;

	while (i < pass.length())
	{
		if (pass[i] >= 'A' && pass[i] <= 'Z')
		{
			return true;
		}
		i = i + 1;
	}
	return false;

}

int testNum(string pass)
{
	int i = 0;

	while (i < pass.length())
	{
		if (pass[i] >= '0' && pass[i] <= '9')
		{
			return true;
		}
		i = i + 1;
	}
	return false;
}
Topic archived. No new replies allowed.