stumped on c++ program


Write a c++ program that requests a password and verifies that it is a valid
password. To be valid the password must be at least 6 characters long and
contain at least one digit.



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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


int main()
{
	const char array_size= 7;
	char password[array_size]={"Tccd03"};
	bool valid,upper;
	char input;
    int index,len;

   cout << "Enter your password: " << endl;
   cin >> input;

   valid =true;
   len =5;
   if (*password<input)
   {
      valid =false;
      cout << "The password must be at least 6 characters long" << endl;   }
   else
  
   upper =false;
   index =1;
   while (!(index>len))
   {
      if (input >='A' && input <='Z')
      {
         upper =true;
      }
      else
      
      index =index+1;
   }
   if (upper==true)
  
   else
   {
      cout << "The password must have at least one upper case character." << endl;   
	  valid =false;
   }
   if (*password=input)
   {
      if (valid==true)
      {
         cout << "That is a valid password." << endl;
	  }
      else
      {
		  cout << "Invalid password" << endl;
     
   return 0;
 }
There is no question here.
you need to go though your program to add {} after each if and else so it will compile.
Write a c++ program that requests a password and verifies that it is a valid
password. To be valid the password must be at least 6 characters long and
contain at least one digit.
Sample Output (inputs in bold)
Please enter a password: pass6
Passwords must be at least 6 characters long
Please enter a password: TarrantNW
Passwords must include at least one digit (1-9)
Please enter a password: Tccd03
Thank you, that is a valid password

It keeps telling me I have to have an upper case letter and it dont need to do that.



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
#include <iostream>
#include <cctype>
#include <string>
#include <ctype.h>
#include <stdio.h>

using namespace std;

int main()
{
	bool valid,digit;
	
	char userinput[1024];
	cout << "Please enter your password: " << endl;
   cin >> userinput; 
	
 
   
	
   // Below wants to test the userinput for any digits in the string
bool digit_yes=false;

			int num = 0;
			int len = strlen(userinput);
			for (int count = 0; count < len; count++)
				
				if (isdigit(userinput[count]))      
				digit_yes=true;

			if (!digit_yes)
			{
				valid=false;

const char password[]={'T','c','c','d','0','3'};

bool verifypass(char *userinput,bool valid) //password verification function
{
   valid = (0 == strcmp (userinput,password));

      if (valid)
      {
         cout << "Thanks. That is a valid password." << endl;
	  }
      else
      {
		  cout << "Invalid password" << endl;
      }
		

	return 0;
}
int verify_chars(char *userinput,bool valid)//verifies the number of characters
{
	int length;

	length = strlen(userinput); //checks for number of characters
   if ((length <= 6) && (length >= 6))
   {
       valid=true;
   }
   else
   {
	   valid =false;
      cout << "The password must be at least 6 characters long" << endl; 
   }
   return valid;
}
Last edited on
I am a beginner. Anyway, looking at your code I am not sure where your main is supposed to end. You have the for loop for (int count = 0; count < len; count++) which you probably meant to have curlies as well, {}. Immediately after that you have an if loop if (!digit_yes) which seems to have only half a curly pair. Your bool function bool verifypass(char *userinput,bool valid) returns 0 which I believe is valid, but maybe not what you wanted it to do. The int function int verify_chars returns a bool variable. Neither function has a function prototype before main, and your indenting is all over the place which makes it awkward to read.
ok thank you here gos the code after I worked on it .




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
#include <iostream>
#include <cctype>
#include <string>
#include <ctype.h>
#include <stdio.h>

using namespace std;
const char password[]={'T','c','c','d','0','3'};

bool verifypass(char *userinput,bool valid) //password verification function
{
   valid = (0 == strcmp (userinput,password));

      if (valid)
      {
         cout << "Thanks. That is a valid password." << endl;
	  }
      else
      {
		  cout << "Invalid password" << endl;
      }
		

	return 0;
}
int verify_chars(char *userinput,bool valid)//verifies the number of characters
{
	int length;

	length = strlen(userinput); //checks for number of characters
   if ((length <= 6) && (length >= 6))
   {
       valid=true;
   }
   else
   {
	   valid =false;
      cout << "The password must be at least 6 characters long" << endl; 
   }
   return valid;
}
int main()
{
	bool valid,upper;
	
	char userinput[1024];
	cout << "Please enter your password: " << endl;
   cin >> userinput; 
	
  
	
   // Below wants to test the userinput for any digits in the string
bool digit_yes=false;

			int num = 0;
			int len = strlen(userinput);
			for (int count = 0; count < len; count++)
				
				if (isdigit(userinput[count]))      
				digit_yes=true;

			if (!digit_yes)
			{
				valid=false;
				cout <<"Passwords must include at least one digit (1-9)"<< endl;
			}
			else
			{
				valid=true;
			}
	verify_chars(userinput,valid);// fuction designed to test the number of characters

	verifypass(userinput,valid);  // Funtion designed to strcmp the password and userinput 

   return 0;
   }
I am still getting errors here is the errors


1
2
3
4
1
1>  lab 9 c++.cpp
1>c:\users\rolena\documents\visual studio 2010\projects\lab 9 c ++\lab 9 c ++\lab 9 c++.cpp(9): error C2734: 'password' : const object must be initialized if not extern
1>c:\users\rolena\documents\visual studio 2010\projects\lab 9 c ++\lab 9 c ++\lab 9 c++.cpp(9): error C2133: 'password' : unknown size
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

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
#include <iostream>
#include <ctype.h>
#include <cctype>
#include <string>
#include <stdio.h>

using namespace std;

const char password[];

bool verifypass(char *userinput,bool valid) 

	//password verification function

{
   valid = (0 == strcmp (userinput,password));

      if (valid)
      {
         cout << "Thanks. That is a valid password." << endl;
	  }
      else
      {
		  cout << "Invalid password" << endl;
      }
		

	return 0;
}


int verify_chars(char *userinput,bool valid)

	//verifies how many characters
{
	int length;

	length = strlen(userinput); 

	//checks for how many characters

   if ((length <= 6) && (length >= 6))
   {
       valid=true;
   }
   else
   {
	   valid =false;
      cout << "The password must be at least 6 characters long" << endl; 
   }
   return valid;
}
	
int main()
{
	bool valid,upper;
	
	char userinput[1024];
	cout << "Please enter your password: " << endl;
    cin >> userinput; 

	
   // This will test input for digits in the string

	bool digit_yes=false;

			int num = 0;
			int len = strlen(userinput);
			for (int count = 0; count < len; count++)
				
				if (isdigit(userinput[count]))      
				digit_yes=true;

			if (!digit_yes)
			{
				valid=false;
				cout <<"This passworde has to have at least one digit (1-9)"<< endl;
			}
			else
			{
				valid=true;
			}

	

	// this function will test the number of characters

	verifypass(userinput,valid); 

	

   return 0;

   }
1
2
const int SIZE = 50; //Or whatever number suits your purpose
char password[SIZE];


Worked for me
How about something like this... it's a little shorter too:

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
#include <iostream>
#include <string>

using namespace std;

int main()
{

	string aPassword;
	bool goodPassword = false;

	while (goodPassword == false)
	{
		//Accept a conditionally valid password from the user
		cout << "Enter a password: ";
		cin >> aPassword;
		goodPassword = true;

		//Make sure the length is at least 6
		if (aPassword.length() < 6) {
			goodPassword = false;
			cout << "\nYour password is too short; must be 6+ characters\n";
			continue;
		}

		//Verify it contains at least 1 digit
		goodPassword = false;
		for (int i=0; i<aPassword.length(); ++i)
		{
			switch (aPassword[i]) {
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				goodPassword = true;
				break;
			}
		}

		//If false, it doesn't contain any digits
		if (goodPassword == false)
		{
			cout << "\nYour password must contain at least 1 digit\n";
			continue;
		}
	}

	//Password passes all the tests! TA-DAH!
	cout << "Your password is: " << aPassword << "\n\n";

  system("pause");
	return 0;
}
Last edited on
The reason that it keeps saying the password is invalid is that the verifypass function checks to see if userinput is equal to password, but password has nothing in it.
You don't need to call verifypass, if it has a digit, and its length is >= 6 then it is valid. You can check that in the main routine.

here is what I have I dont understand how to do the above stated items if anyone could please lend me a little help?

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
#include <iostream>
#include <cctype>
#include <string>
#include <ctype.h>
#include <stdio.h>

using namespace std;
const char password[]={'T','c','c','d','0','3'};

bool verifypass(char *userinput,bool valid) //password verification function
{
   valid = (0 == strcmp (userinput,password));

      if (valid)
      {
         cout << "Thanks. That is a valid password." << endl;
	  }
      else
      {
		  cout << "Invalid password" << endl;
      }
		

	return 0;
}
int verify_chars(char *userinput,bool valid)//verifies the number of characters
{
	int length;

	length = strlen(userinput); //checks for number of characters
   if ((length <= 6) && (length >= 6))
   {
       valid=true;
   }
   else
   {
	   valid =false;
      cout << "The password must be at least 6 characters long" << endl; 
   }
   return valid;
}
int main()
{
	bool valid,upper;
	
	char userinput[1024];
	cout << "Please enter your password: " << endl;
   cin >> userinput; 
	
   // this section is to verify uppercase charaters in userinput string
   {
      bool upper = false;
  const int l = strlen(userinput);
  for(int i = 0; i < l; ++i)
  {
    upper = (isupper(userinput[i]) != 0);
    if(upper)
      break;
  }
  bool valid = upper;
  if(!upper)
    cout << "The password must have at least one upper case character." << endl;
   }
   
	
   // Below wants to test the userinput for any digits in the string
bool digit_yes=false;

			int num = 0;
			int len = strlen(userinput);
			for (int count = 0; count < len; count++)
				
				if (isdigit(userinput[count]))      
				digit_yes=true;

			if (!digit_yes)
			{
				valid=false;
				cout <<"Passwords must include at least one digit (1-9)"<< endl;
			}
			else
			{
				valid=true;
			}
	verify_chars(userinput,valid);// fuction designed to test the number of characters

	verifypass(userinput,valid);  // Funtion designed to strcmp the password and userinput 

   return 0;
Last edited on
Ok I am really stumped on this program if anyone could please help me I guess there is something I am not getting........ This is past do :(
here is the assiment Write a c++ program that requests a password and verifies that it is a valid
password. To be valid the password must be at least 6 characters long and
contain at least one digit.
Sample Output (inputs in bold)
Please enter a password: pass6
Passwords must be at least 6 characters long
Please enter a password: TarrantNW
Passwords must include at least one digit (1-9)
Please enter a password: Tccd03
Thank you, that is a valid password and here is what I got
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 <ctype.h>
#include <cctype>
#include <string>
#include <stdio.h>

using namespace std;

char password[6];

const int SIZE = 6;

bool verifypass(char *userinput,bool valid) 

	//password verification function

{
   valid = (0 == strcmp (userinput,password));

      if (valid)
      {
         cout << "Thanks. That is a valid password." << endl;
	  }
      else
      {
		  cout << "Invalid password" << endl;
      }
		

	return 0;
}


int verify_chars(char *userinput,bool valid)

	//verifies how many characters
{
	int length;

	length = strlen(userinput); 

	//checks for how many characters

   if ((length <= 6) && (length >= 6))
   {
       valid=true;
   }
   else
   {
	   valid =false;
      cout << "The password must be at least 6 characters long" << endl; 
   }
   return valid;
}
	
int main()
{
	bool valid,upper;
	
	char userinput[1024];
	cout << "Please enter your password: " << endl;
    cin >> userinput; 

	
   // This will test input for digits in the string

	bool digit_yes=false;

			int num = 0;
			int len = strlen(userinput);
			for (int count = 0; count < len; count++)
				
				if (isdigit(userinput[count]))      
				digit_yes=true;

			if (!digit_yes)
			{
				valid=false;
				cout <<"This passworde has to have at least one digit (1-9)"<< endl;
			}
			else
			{
				valid=true;
			}

	

	// this function will test the number of characters

	verifypass(userinput,valid); 

	

   return 0;

   }
You are pretty close to having it completed.

I would completely remove your verifypass function. You are using strcmp to compare 2 strings when 1 of the strings (password) does not have anything inside of it, as mentioned earlier. You already have stuff checking for 1 digit and at least 6 characters anyway.

The problem is here
if ((length <= 6) && (length >= 6))

That is basically saying if the length is equal or less than 6 'and' greater or equal to 6 (which isn't what you want it to do). Change that to
if(length >= 6) Then all you need to do is call the verify_chars function in main (there is no need to pass a bool into it if you are returning a bool, you might as well define 'valid' in the function.
ok I changed the if ((length <= 6) && (length >= 6) to if(length >= 6) I took the verify password funciton out. I am not sure what exactly about no need to pass a bool into it if returning a bool. I am sorry I am not very good at this but I am tring.
It is stil telling me invalid password.

here is what I have got
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
#include <iostream>
#include <ctype.h>
#include <cctype>
#include <string>
#include <stdio.h>

using namespace std;

char password[6];

const int SIZE = 6;


int verify_chars(char *userinput,bool valid)

	//verifies how many characters
{
	int length;

	length = strlen(userinput); 

	//checks for how many characters

   if(length >= 6) 
   {
       valid=true;
   }
   else
   {
	   valid =false;
      cout << "The password must be at least 6 characters long" << endl; 
   }
   return valid;
}
	
int main()
{
	bool valid,upper;
	
	char userinput[1024];
	cout << "Please enter your password: " << endl;
    cin >> userinput; 

	
   // This will test input for digits in the string

	bool digit_yes=false;

			int num = 0;
			int len = strlen(userinput);
			for (int count = 0; count < len; count++)
				
				if (isdigit(userinput[count]))      
				digit_yes=true;

			if (!digit_yes)
			{
				valid=false;
				cout <<"This passworde has to have at least one digit (1-9)"<< endl;
			}
			else
			{
				valid=true;
			}



   return 0;

   }
Last edited on
You will want to do something like this (only has minor changes to your code, feel free to ask if you don't understand any of it)

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
 
bool verify_chars(char *userinput)
{
   bool valid;  // variable returned to see if correct or not
   int length= strlen(userinput); 

   if (length >= 6)
   {
       valid=true;
   }

   else
   {
       valid =false;
       cout << "The password must be at least 6 characters long" << endl; 
   }

   return valid;
}
	
int main()
{
	bool valid,upper;
	char userinput[1024];

	cout << "Please enter your password: " << endl;
       cin >> userinput; 

	bool digit_yes=false;
	int len = strlen(userinput);

	for (int count = 0; count < len; count++)  // Make sure your for loop has braces to keep everything organized
	{
		if (isdigit(userinput[count]))      
		digit_yes=true;
	}

	if (!digit_yes)
	{
		valid=false;
		cout <<"This passworde has to have at least one digit (1-9)"<< endl;
	}
	else
	{
		valid=true;
	}

        // Makes sure the value returned from verify_chars is true (it calls the function) and makes sure valid is true from above.
	if((verify_chars(userinput) == true) && (valid == true))
	{
		cout << "Correct password";
	}

   return 0;
}


Of course this could be optimized (like isdigit could be moved into a function etc). But this is the general idea.
I would also read through some of the replies in the thread, as they do provide some solutions like mine.

You will probably want to add some while loops as well, so that it will keep repeating until they enter in the correct input.
Last edited on
Thank you SO VERY MUCH for you help :)
Topic archived. No new replies allowed.