Not all control paths return a value?

I'm trying to create a code for password verification for a class. I thought I had it working but for some reason every time I input a seemingly correct password, it says it must have one lowercase letter. The only error I can find is "not all control paths return a value." Any help please?

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
  #include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

bool lengthTest(char []);
bool upTest(char []);
bool lowTest(char []);
bool numTest(char []);

int main()
{
	char password[20];

	// Explain password criteria and prompt user for password.
	cout << "The password must have:\n" <<
			"      at least 6 characters,\n" <<
			"      at least one uppercase character,\n" <<
			"      at least one lowercase character,\n" <<
			"      at least one numeric digit.\n" <<
			"Enter a password:\n";
	cin.getline(password,20);

	// Verify password.
	if (lengthTest(password))
	if (upTest(password))
	if (lowTest(password))
	if (numTest(password))
		cout << "The password is valid.\n";
	else
		cout << "The password is invalid.\n";

	return 0;

}

// *******************************************
//         Verifies password length.
// *******************************************
bool lengthTest(char custPass[])
{
	int length;
	length = strlen(custPass);
	
	if (length >= 6)
		return true;

	else
		cout << "The password must have a minimum of 6 characters.\n";
		return false;
}
// *******************************************
//         Verifies uppercase letter.
// *******************************************
bool upTest(char custPass[])
{	
	for (int i = 0; i < 20; i++)
		{	
			if (isupper(custPass[i]))		
			return true;

			else
			cout << "Must have at least one uppercase letter.\n";
			return false;
		}
}
// *******************************************
//         Verifies lowercase letter.
// *******************************************
bool lowTest(char custPass[])
{	
	for (int i = 0; i < 20; i++)
		{	
			if (islower(custPass[i]))		
			return true;

			else
			cout << "Must have at least one lowercase letter.\n";
			return false;
		}
}
// *******************************************
//             Verifies digit.
// *******************************************
bool numTest(char custPass[])
{	
	for (int i = 0; i < 20; i++)
		{	
			if (isdigit(custPass[i]))		
			return true;

			else
			cout << "Must have at least one digit.\n";
			return false;
		}
}
Could you copy and paste your errors?

Also, you should probably use { } for all of your if statements so they are easier to read.
warning C4715: 'upTest' : not all control paths return a value
warning C4715: 'lowTest' : not all control paths return a value
warning C4715: 'numTest' : not all control paths return a value

It says the build succeeded but it isn't working like how it should.

And by your suggestion, do you mean around the first collection of ifs?
Lets start with:
1
2
3
4
5
6
7
8
9
10
11
12
bool upTest(char custPass[])
{	
	for (int i = 0; i < 20; i++)
		{	
			if (isupper(custPass[i]))		
			return true;

			else
			cout << "Must have at least one uppercase letter.\n";
			return false;
		}
}

I add braces to make it more clear what you are doing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool upTest(char custPass[])
{	
  for (int i = 0; i < 20; i++)
  {
    if (isupper(custPass[i]))
    {
      return true;
    }
    else
    {
      cout << "Must have at least one uppercase letter.\n";
    }

    return false;
  }
}

But why have a loop at all, since there will be a return on first iteration:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool upTest(char custPass[])
{
  if (isupper(custPass[0]))
  {
    return true;
  }
  else
  {
    cout << "Must have at least one uppercase letter.\n";
  }

  return false;
}

Is that really what you want your function to do?
Last edited on
Topic archived. No new replies allowed.