Password login string

I am new to this and am trying to figure out how to get my code to work. I have to use string can't use class or anything advanced. I pretty much have no idea what to do with my string namepassword function . I also need a way to condense my password registration. Just telling me the spots I am going wrong would be great.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
 

#include<iostream>
#include<iomanip>
#include<string>

using namespace std;

 


/***************************************************************************/
/***************prototypes**************************************************/
void pause ();
int menu ();
string namePassword (string &newuser, string &pass);
void verifylogin(string);
bool checkpassc(string);
int checkupperc(string);
int checklowerc(string);
int checknum(string);
int checkspace(string);

 

 

/****************************************************************************/
/******************main function*********************************************/

int main()
{
    int cont = 2;
   string *username;//ptr to dynamic array to hold names of products
   string *passwords; //ptr to dynamic array to hold prices of products
   int size =0;//hold the size of the dynamic array
   string s1;
   string s2;
    do        
   {// start do
      int choice=menu (); // call to the menu function
      switch (choice)
      { // Open switch statement
    
/******************************************************************************/
/**********switch case one register username password**************************/

         case 1: 
              
              //Get the information from the user for the product
              namePassword(s1,s2);
              cout<<"\n\n Enter a username:\n  ";
              cin>>s1;
              cout<< "\tEnter a password with the following criteria:\n";
              cout << "8 to 15 characters\n"; 
              cout<< "2 uppercase\n2 lowercase\n2 numbers\n2 special characters:\n";
           cin>>s1;
    
   int length;
    length = strlen();
    
 if(length >= 8 && length <= 15)
    {
        while (!checkpassc(s2))    
    {        
    cout << "Invalid Password. try again\n";        
    cin >> s2;    
    }    
    cout << "Your password: " << s2 << ", is valid\n";    
    }
    else
        cout << "The password entered must be between 8-15 characters long\n";            
     
 system("pause");    
 

            pause();
            break;
            
/*****************************************************************************/
/******************switch case 2  login **************************************/ 
           
      case 2://get  the login information from the user
            
 //char lookUp[user];
// char *strptr=null;
 //int index
// cout<<"enter username\n:";
// cin.getline(lookUp,user);
 
// for (index=0; index<user; index++)
// { 
//     strPtr=strstr(user[index],lookup);
// if (strPtr !=Null
// cout<<"incorrect username\n";
// }
// if(strPtr!
 

            pause();
            break;
      
/******************************************************************************/
/******************************************************************************/
                 
         case 3:cont=0;
                 break;
          } // Close switch statement
                
   }while (cont ==2);
   return 0;
}// STOP MAIN

/******************************************************************************/
/*******************void Pause function****************************************/

void pause ()
{//clears the last 80 on buffer
   cout << "Press enter to continue " << endl;
   cin.ignore (80, '\n');
}

/*****************************************************************************/
/**********int menu funtion********************************************/
         
int menu () 
{ // start int menu prototype
  
   int c = -1;
   while (c<1 || c>4)
   {  // start menu
      system ("cls");
      cout << " 1. Register\n\n\n"
           << " 2. Login\n\n\n"
           << " 3. Exit\n\n\n";
            
           
      cin >> c;
      cin.ignore (80, '\n');
   }//end of while loop
   return c;
}// close int menu prototype

/******************************************************************************/
/******************user name function**********************************************/
string namePassword (string &newuser, string &pass);
{

} 
/****************************************************************************/
/*******************password registration*************************************/
bool checkpassc(string str)
{   

 int length = strlen(str.c_Str()); //function to deterine the length of the cstring
    return checkupperc(str) && checklowerc(str) && checknum(str) && checkspace (str);
}
//test for upper case 
int checkupperc(string str)
{
 int user = 0;
 int length = strlen(str);
 
 for (int count = 0; count < length; count++)
 {
  if (!isupper(str[count]))
  user++;
    }
  return user;    
}
//test for lower case
int checklowerc(string str)
{
 int user = 0;
 int length = strlen(str);
 
 for (int count = 0; count < length; count++)
 {
 if (!islower(str[count]))
 user++;
 }
 return user;
}
//test if it is a number or alphabet
int checknum(string str)
{
 int user = 0;
 int length = strlen(str);
 
 for (int count = 0; count < length; count++)
 {
  if (!isdigit(str[count]))
        user++;
 }
  return user;
}
//test if the argument is a whitespace character
int checkspace (string str)
{
    int user=0;
    int length= strlen(str);
    
    for(int count=0; count<length; count++)
    {
            if(!isspace(str[count]))
            user++;
            }
            return user;
            }
   
What do you need help on?
Ok first , notice the user name function is blank I am not familiar with strings and not sure what to put there. I need it to allow the user to input a username and password that is 8 to 15 in length . The password must have 2 upper 2 lower 2 digit and 2 special characters , then I need them to be able to log in with that username and password . Where I am currently stuck is trying to figure out how to get the string to work with the rest of my program. I know it has something to do with the way I am setting them up. I am just not sure how to set up the strings correctly. Yes I read the beginner forums on strings. I keep getting an error to do with strlen . I know it's got to be like an easy error. I just can't see it ...
Last edited on
There are a number of errors which the compiler should give useful messages to help you track down.

One thing, you don't need strlen() with std::string, instead use the member function length() or size().
change this:
int length = strlen(str);
to
int length = str.length();
http://www.cplusplus.com/reference/string/string/size/
Ok so I worked on It alot today and i have improved it a bunch but I cant get past this one part no matter what I do. it compiles to this line no further.
int length = str length function str; can anyone please help! here is my new code.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include<iostream>
#include<iomanip>
#include<string>

using namespace std;




/***************************************************************************/
/***************prototypes**************************************************/
void pause ();
int menu ();
string namePassword(string &newuser, string &pass);
void verifylogin(string);
bool checkPassword(string str);






/****************************************************************************/
/******************main function*********************************************/

int main()
{//open main
    int cont = 2;
   string *username;//ptr to dynamic array to hold names of products
   string *passwords; //ptr to dynamic array to hold prices of products
   int size =0;//hold the size of the dynamic array
   string s1;
   string s2;
    do        
   {// open do
      int choice=menu (); // call to the menu function
      switch (choice)
      { // Open switch statement
    
/******************************************************************************/
/**********switch case one register username password**************************/

         case 1: 
              
              
              namePassword(s1,s2);
              if (checkPassword(s2))
                  cout<<"Success..."<<endl;
                  else
                  cout<<"Fail..."<<endl;
 //            cout<<"\n\n Enter a username:\n  ";
//              cin>>pass;
//              cout<< "\tEnter a password with the following criteria:\n";
//              cout << "8 to 15 characters\n"; 
//              cout<< "2 uppercase\n2 lowercase\n2 numbers\n2 special characters:\n";
//	          cin>>newuser;
            
	    
	system("pause");    
	

            pause();
            break;
            
/*****************************************************************************/
/******************switch case 2  login **************************************/ 
           
      case 2://get  the login information from the user
 
  

            pause();
            break;
      
/******************************************************************************/
/******************************************************************************/
                 
         case 3:cont=0;
                 break;
          } // Close switch statement
                
   }while (cont ==2);//close 
   return 0;
}// STOP MAIN

/******************************************************************************/
/*******************void Pause function****************************************/

void pause ()
{//clears the last 80 on buffer open
   cout << "Press enter to continue " << endl;
   cin.ignore (80, '\n');
}//close

/*****************************************************************************/
/**********int menu funtion********************************************/
         
int menu () 
{ // start int menu prototype
  
   int c = -1;
   while (c<1 || c>4)
   {  // start while
      system ("cls");
      cout << " 1. Register\n\n\n"
           << " 2. Login\n\n\n"
           << " 3. Exit\n\n\n";
            
           
      cin >> c;
      cin.ignore (80, '\n');
   }//end of while loop
   return c;
}// close int menu prototype

/******************************************************************************/
/******************user name function**********************************************/
string namePassword(string &newuser, string &pass)
{
    //Get the information from the user for the product
              char user[30];
              cout<<"\n\n Enter a username:  ";
              cin.getline(user,30);
              cout<<user<<endl;
              
              	char passw[30];    
	cout << "Enter a password with the following criteria:\n";
    cout << "8 to 15 characters\n";
    cout << "2 uppercase\n2 lowercase\n2 numbers\n2 special characters:\n";
	cin >> passw; 
}

/****************************************************************************/
/*******************password registration*************************************/
bool checkPassword(string *str)

  { //open
  
	int length = str length function str; //function to deterine the length of the cstring
   
    if (length<8 ||length>15)
    {
    return false;
}
else
{
    int upper=0, lower=0,dig=0,spec=0;
    for(int i=0; i<length; i++)
    {
            if(str check the character for upper str)
             upper++;
             else if(str check the character for lower str)
             lower++;
             else if( str check the character for digit str)
             dig++;
             else if( str check the characterfor special str)
             spec++;
             }
             if(upper str 2||lower str 2||dig str 2||spec str 2)
             {
             returnfalse:
to make it 100 times easier, do

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

using namespace std;

int main(int argc, char *argv[])
{
    string enteredpassword;
    string enteredusername;
    string username;
    string password;
    username = "username"; //set your username here
    password = "pass"; //set password here
    cout << "Please enter username:\n";
    getline(cin,enteredusername); //stores the username you entered as a string named enteredusername
    cout << "Please enter password:\n"; 
    getline(cin,enteredpassword); //stores the password you entered as a string named enteredpassword
    if(enteredusername == username | enteredpassword == password) //checks to see if your entered pass & user matches the original
    {
    cout << "you have succsefully logged on!\n";
    }
    else
    {
        cout << "Wrong username or password\n";
    }
    system("pause");
    return EXIT_SUCCESS;
}


if you want to be able to change the password without editing the sourcecode, or add a user, we can do this using fstream and storing the credentials in a file anywhere on your computer.
Last edited on
This is what i suggested last time:
 
    int length = str.length();

but the line which is giving you trouble looks very different:
 
    int length = str length function str;

So as a starting point I'd get rid of the incorrect code and instead use my suggested version.

There are other errors, for example this prototype declaration:
bool checkPassword(string str);

however the function definition is not the same:
bool checkPassword(string *str)
They need to be the same, so get rid of the '*'

(there may be other errors, I didn't study every line).



thanks for the help I am getting there no . today I will be working on the rest of this but you helped me through some tight spots and I learned a lot. I will be trying to take the username and password that the user enters now and compare it to a login username and password the user enters . that is my next step. so I will need a login function to hold the new username and password then the ability to compare the new username and password to the one that was originally entered by the user. what would be the easiest way to do this? I seem to always do things the hard way in c++ . I wrote 100 lines of code before I found out I could condense it to about 10. anyhow thanks again. Oh here is my new code.
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<iostream>
#include<iomanip>
#include<string>
#include<cstring>
using namespace std;




/***************************************************************************/
/***************prototypes**************************************************/
void pause ();
int menu ();
string namePassword(string &newuser, string &pass);
void verifylogin(string);
bool checkPassword(string str);






/****************************************************************************/
/******************main function*********************************************/

int main()
{//open main
    int cont = 2;
   string *username;//ptr to dynamic array to hold names of products
   string *passwords; //ptr to dynamic array to hold prices of products
   int size =0;//hold the size of the dynamic array
   string s1;
   string s2;
    do        
   {// open do
      int choice=menu (); // call to the menu function
      switch (choice)
      { // Open switch statement
    
/******************************************************************************/
/**********switch case one register username password**************************/

         case 1: 
              
              
              namePassword(s1,s2);
              if (checkPassword(s2))
                  cout<<"Success..."<<endl;
                  else
                  cout<<"Fail..."<<endl;
 
            
	    
	system("pause");    
	

            pause();
            break;
            
/*****************************************************************************/
/******************switch case 2  login **************************************/ 
           
      case 2://get  the login information from the user
 
  

            pause();
            break;
      
/******************************************************************************/
/******************************************************************************/
                 
         case 3:cont=0;
                 break;
          } // Close switch statement
                
   }while (cont ==2);//close 
   return 0;
}// STOP MAIN

/******************************************************************************/
/*******************void Pause function****************************************/

void pause ()
{//clears the last 80 on buffer open
   cout << "Press enter to continue " << endl;
   cin.ignore (80, '\n');
}//close

/*****************************************************************************/
/**********int menu funtion********************************************/
         
int menu () 
{ // start int menu prototype
  
   int c = -1;
   while (c<1 || c>4)
   {  // start while
      system ("cls");
      cout << " 1. Register\n\n\n"
           << " 2. Login\n\n\n"
           << " 3. Exit\n\n\n";
            
           
      cin >> c;
      cin.ignore (80, '\n');
   }//end of while loop
   return c;
}// close int menu prototype

/******************************************************************************/
/******************user name function**********************************************/
string namePassword(string &newuser, string &pass)
{
    //Get the information from the user for the product
//              char user[30];
//              cout<<"\n\n Enter a username:  ";
//              cin.getline(user,30);
//              cout<<user<<endl;
              
//              	char passw[30];    
//	cout << "Enter a password with the following criteria:\n";
//    cout << "8 to 15 characters\n";
//    cout << "2 uppercase\n2 lowercase\n2 numbers\n2 special characters:\n";
//	cin >> pass;
 
}

/****************************************************************************/
/*******************password registration*************************************/
bool checkPassword(string str)

  { //open
  
	int length = str.length(); //function to deterine the length of the cstring
   
    if (length<8 ||length>15)
    {
    return false;
}
else
{
    int upper=0, lower=0,dig=0,spec=0;
    for(int i=0;i<length; i++)
    {         
            if (isupper(str[i]))
             upper++;
             else if(islower(str[i]))
             lower++;
             else if( isdigit(str[i]))
             dig++;
             else 
             spec++;
             
             if(upper < 2||lower < 2||dig < 2|| spec < 2)
    {
             return false;
    }
    }
             return true;
    }
    }
             
I wouldn't worry too much about doing things the 'easiest' way here, I think your code is broadly speaking heading in the right direction.

Purely as a matter of style, the indentation looks a bit inconsistent, and there are rather more comments than I would like. In the switch-case for example, I find the comments a distraction. Here's an example of a much more lightweight commenting style:
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
    do        
    {
        int choice = menu(); 
         
        switch (choice)
        {
            case 1: // ----------- Register ------------
                namePassword(s1,s2);
                if (checkPassword(s2))
                    cout << "Success..." << endl;
                else
                    cout << "Fail..."    << endl;
                    
                system("pause");    
                pause();
                break;

            case 2: // ----------- Login ---------------
                pause();
                break;

            case 3: // ----------- Exit ----------------
                cont=0;
                break;
        }

    } while (cont == 2);


But I think it's just a matter of working through things step by step, get the userid+password at the "register" stage, and save those values. Then at the "login" process, get the userid+password in different variables, then compare the old with the new.
Last edited on
so I am not getting anywhere because I apparently do not understand how to create an array properly. I could get this to work if i could do (char*); but I need to do is use cstrings and i have to get an array to store the username and password then be able to compare that to the login information the user provides. uggh this is crazy I know... i been here for hours. so if there is an example of how to do this please . like char name[30]; gave me error and i do not know another way to write it other than try to write a dynamic array. which I am not sure that is the best idea for this.
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include<iostream>
#include<iomanip>
#include<string>
#include<cstring>
using namespace std;






void pause ();//------------------prototypes----------
int menu ();
void namePassword(string *sptr,string newuser, int size);
void verifylogin(string);
bool checkPassword(string str);









int main()//-----------------main------
{//open main
    int cont = 2;
  
   string *username;
   string *password; 
   int size =0;
   string s1;
   string s2;
    do        
   {// open do
      int choice = menu (); //--- call to the menu function---------
      switch (choice)
      { // Open 
    


         case 1: //-------------register-----------------------------------
              
              namePassword(s1,s2);
              if (checkPassword(s2))
                  cout<<"Success..."<<endl;
                  else
                  cout<<"Fail..."<<endl;
 
 	system("pause");    
	pause();
    break;
             
         case 2://--------------login-------------------------------------------

            pause();
            break;
      


                 
         case 3://---------------------Exit----------------------------------
              cont=0;
                 break;
          } // Close switch statement
                
   }while (cont ==2);//close 
   return 0;
}// STOP MAIN



void pause ()//----------------void pause---------------------------
{//clears the last 80 on buffer open
   cout << "Press enter to continue " << endl;
   cin.ignore (80, '\n');
}//close



         
int menu ()//---------------menu function-------------------------------- 
{ 
  
   int c = -1;
   while (c<1 || c>4)
   {  
      system ("cls");
      cout << " 1. Register\n\n\n"
           << " 2. Login\n\n\n"
           << " 3. Exit\n\n\n";
            
           
      cin >> c;
      cin.ignore (80, '\n');
   }
   return c;
}


void namePassword(string *sptr,string newuser, int size)//---------register-- function---
{
           
             string *user =new string[size+1];
             for (int i=0; i<size; i++)
     *(user+i)=*(sptr+i);
       
   *(user+size)= newuser;
   if (size>0)
   delete sptr; 

    return ptr; 
              cout<<"\n\n Enter a username:  ";
              cin.getline(user,16);
              cout<<user<<endl;
              
              	string password;    
	cout << "Enter a password with the following criteria:\n";
    cout << "8 to 15 characters\n";
    cout << "2 uppercase\n2 lowercase\n2 numbers\n2 special characters:\n";
	cin >> password; 
}
void verifylogin(string,string);//----------------login-----function--------------------
{
 
     
}


bool checkPassword(string str)//---------------check the password--------

  { //open
  
	int length = str.length(); 
   
    if (length<8 ||length>15)
    {
    return false;
}
else
{
    int upper=0, lower=0,dig=0,spec=0;
    for(int i=0;i<length; i++)
    {         
            if (isupper(str[i]))
             upper++;
             else if(islower(str[i]))
             lower++;
             else if( isdigit(str[i]))
             dig++;
             else 
             spec++;
             
             if(upper < 2||lower < 2||dig < 2|| spec < 2)
    {
             return false;
    }
    }
             return true;
    }
    }
             
Last edited on
I'm a bit lost here. You have these two includes:
1
2
#include <string>
#include <cstring> 

The first is for C++ std::string.
The second is for C-style character arrays.

Do you have a specification for this project that clearly state which one is to be used, or are you allowed to use either?

The C++ version is generally easier and safer to work with. But a knowledge of c-strings can be useful too.

Let's say you want to input and compare two strings.
First using std::string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string first;
    string second;
    
    cout << "Enter first string:  ";
    cin >> first;
    cout << "Enter second string: ";
    cin >> second;
    
    if (first == second)
        cout << "The strings are the same." << endl;
    else
        cout << "The strings are different." << endl;
    
    return 0;
}



Now with c-strings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char first[30];
    char second[30];
    
    cout << "Enter first string:  ";
    cin >> first;
    cout << "Enter second string: ";
    cin >> second;
    
    if (!strcmp(first,second))
        cout << "The strings are the same." << endl;
    else
        cout << "The strings are different." << endl;
    
    return 0;
}

Note - the second version is risky, as the user can enter more than 30 characters and cause a buffer overflow with unpredictable (but bad) consequences.
You might fix that by using cin.get instead of cin >> like this:
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
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char first[30];
    char second[30];
    
    cout << "Enter first string:  ";
    cin.get(first, 30);
    cin.ignore(1000, '\n');
    cout << "Enter second string: ";
    cin.get(second, 30);
    cin.ignore(1000, '\n');

    if (!strcmp(first,second))
        cout << "The strings are the same." << endl;
    else
        cout << "The strings are different." << endl;
        
    cout << "first:  " << first  << " length: " << strlen(first)  << endl;
    cout << "second: " << second << " length: " << strlen(second) << endl;
    
    return 0;
}

(Note, I also added code to display the contents of the string and its length).

Finally, if you want to pass a c-string to a function, declare it like this:
void myfunction(char * str);
Well thank you all a bunch . This was a heck of a lot of fun the project is coming to a close . I got it almost finised the only thing it dosent do in correctly check the login information . like if you enter the name and password it storers it but then when you go to login it always says wrong username and pass no matter what .. so im done.. for now here is the code its working.

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <iomanip>
using namespace std;



    
void pause ();//-------------function prototypes---------------------/
int menu ();
string *addUsername (string *, string, int);
string *addPassword (string *, string, int);
bool checkName(string);
bool checkPassword(string);
void getCred(string *, string *);




int main()  
{
   int cont = 2;
   string *usernames;    
   string *passwords;    
   int size = 0;        
   string s1;
   string s2;
   
   do        
   {
      int choice = menu (); 
      switch (choice)
      { 
         case 1: //-----------------register------------------------/
                          char s1[16];
                cout<<"The Username you enter must be 5 to 15 "
                  <<"alphanumeric characters long.\n"
                  <<"your username may contain_and-special characters\n\n"
                  <<"\n\nPlease Enter a Username:\n\n";
                  cin.getline(s1,16);
               while (!checkName(s1))
                 {
                 cout << "Enter a valid Username: " << endl;
                 cin.getline(s1, 16);
                 };
         
              
         
              char s2[16];
               cout<<"Passwords must meet the following criteria:\n"
                   <<"They must contain 2 uppercase letters, 2 lowercase letters,\n"
                   << "2 numbers, and 2 special characters \n\n"
                   << "Please enter your password now:\n\n";
              cin.getline(s2,16);
              while (!checkPassword(s2))
                 {
                 cout << "Enter a valid Password: " << endl;
                 cin.getline(s2, 16);
                 };
                 
                 
                 
              
              usernames = addUsername(usernames, s1, size);
              passwords = addPassword(passwords, s2, size);
              size++;
              
              
              for (int i=0; i<size; i++)
                cout<<i+1<<": "<<*(usernames+i)<<"\t\t"<<*(passwords+i)<<endl;
                cout << "\n\n";
               
            pause();
            break;
            
              
             
              
         
         
           
                 
         case 2://-----------------login--------------------------
    
               cout<<"LOGIN\n\n";
               char l1[16];
               cout << "Please enter username:\n";
               cin.getline(l1,16);
                char l2[16];
               cout << "Please enter your login password:\n";
               cin.getline(l2,16);
   if(l1==s1||l2==s2)
    {
    cout << "you have succsefully logged on!\n";
    }
    else
    {
        cout << "Wrong username or password\n";
    }
  
   
 pause();
         
                break;
               
            
         case 3://-------------exit--------------------------------/
               cont=0;
              break;
            
      } 
                
   }while (cont ==2);
   
   return 0;
   
}// STOP MAIN



void pause ()//----------------pause function-------------------------/
{
   cout << "Press enter to continue " << endl;
   cin.ignore (80, '\n');
}



int menu () //-----------menu function---------------------------------/
{ 
  
   int c = -1;
   while (c<1 || c>3)
   {  
      system ("cls");
      cout << " 1. Register\n\n\n"
           << " 2. Login\n\n\n"
           << " 3. Exit\n\n\n"
           << " Selection: ";
           
      cin >> c;
      cin.ignore (80, '\n');
   }
   return c;
}



string *addUsername (string *unptr, string s1, int size)//----username array-/
{
       
   string *ptr = new string[size+1];
   
   
   for (int i=0; i<size; i++)
      *(ptr+i)=*(unptr+i);
         
   *(ptr+size)=s1;
   
   
    if (size>0)
       delete [] unptr; 
       unptr = 0;
    
    return ptr;
    
                
               
}
string *addPassword (string *pptr, string s2, int size)//-----password array
{
       
   string *ptr1 = new string[size+1];
   
   
   for (int i=0; i<size; i++)
      *(ptr1+i)=*(pptr+i);
         
   *(ptr1+size)=s2;
   
  
    if (size>0)
       delete [] pptr; 
       pptr = 0;
   
    return ptr1;
}



bool checkPassword(string chkp)//---------password check--------------------/

  { 
  
	int length = chkp.length(); 
   
    if (length<8 || length>15)
    {
    return false;
    }

else
{
    int upper=0, lower=0,dig=0,spec=0;
    for(int i=0;i<length; i++)
    {         
            if (isupper(chkp[i]))
             upper++;
             else if(islower(chkp[i]))
             lower++;
             else if (isdigit(chkp[i]))
             dig++;
             else 
             spec++;
             }
             
             if(upper < 2 || lower < 2 || dig < 2 || spec < 2)
             return false;
             
             else
             return true;
 }   
 }



bool checkName(string uname)//--------------check username -----/

  { //open
	int length = uname.length(); 
   
    if (length<5 || length>15)
    {
    return false;
    }
    else
    {
    int str = 0;
    for(int i=0; i<length; i++)
    {         
    if (isalnum(uname[i]))
    str++;
    else 
             
    if(str < length)
    {
    return false;
    }
    }
    return true;
    }
}


Topic archived. No new replies allowed.