While Loop not working as expected.

When I put while (valid = true) the loop prompts me to input Pass but then the program does nothing. When I put while (valid = false) the program doesn't prompt for the input and immediately ends. How do I get this loop to execute properly? It is a bit confusing when a loop contains this many if statements.

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
96
97
  #include <iostream>
#include <string>
using namespace std;

void testLength(int Pass[], int valid);
void testCaps(int Pass[], int valid);
void testNum(int Pass[], int valid);

int main()
{
	int Pass, valid;

	
	while (valid = true)
	{
		cout << "Please enter a password:";
		cin >> Pass;

		int Pass[sizeof(Pass)];
		
		testLength(Pass, valid);
		if (valid = true)
		{
			testCaps(Pass, valid);
			if (valid = true)
			{
				testNum(Pass, valid);
				if (valid = true)
				{
				}
				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;
		}
		if (valid = true) break;
	}
	cout << "Thank you, that is a valid password" << endl;
	return 0;
}

void testLength(int Pass[], int valid)
{
	
	if (sizeof(Pass) >= 7)
	{
		valid = true;
	}
	else valid = false;

	return void(valid);

}

void testCaps(int Pass[], int valid)
{
	valid = 0;
	int i = 0;

	while (i < sizeof(Pass))
	{
		if (Pass[i] >= 'A' && Pass[i] <= 'Z')
		{
			valid = true;
		}
	}
	i = i + 1;

	return void(valid);
}

void testNum(int Pass[], int valid)
{
	valid = 0;
	int i = 0;

	while (i < sizeof(Pass) - 1)
	{
		if (Pass[i] >= '0' && Pass[i] <= '9')
		{
			valid = true;
		}
		
	}
	i = i + 1;
	
	return void(valid);
}
Last edited on
Hi,

= is assignment, == is for equality.

However, more concisely written: while (vaild) and the opposite of that is while (!valid)

valid should be of type bool, int still works, but bool conveys better meaning IMO
Last edited on
1
2
3
int Pass, valid;

while (valid = true)


Also valid has a random value. It's always a good idea to initialize your vars when you declare them.
Topic archived. No new replies allowed.