how do i check a char for length whether its alpha bate and numeric?

Hi all. this is what i have done but have some errors, it goes in infinite while loop if i enter all numbers..any ideas appreciated!!.
TIA for 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
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
      void vectoring::cpw(string usr, char psw[20]){
    string a;
    string b;
    string c;
    int counter = 0;
    int rows = 0;
    vector<string>un;
    vector<string>pw;
    vector<string>lock;
    vector<datastorage> dss;
    datastorage tempds;
    

     ifstream data("cashier.txt");
     while (data.good()){
         data >> a >> b >> c;
        if(data.good()){
         un.push_back(a);
         pw.push_back(b);
         lock.push_back(c);
         rows++;
         }
    }
     data.close();

     for(int i=0; i < rows ; i++){
         tempds.setusername(un[i]);
         tempds.setpassword(pw[i]);
         tempds.setlock(lock[i]);
         dss.push_back(tempds);
    }
     for (int h = 0; h < dss.size(); h++){
         if (usr == dss.at(h).getusername()){
             if (dss.at(h).getlock() == "yes"){
                 cout << "\naccount is locked\n";
                 counter = 3;
             }
         }
     }

     bool t = false;
     bool y = false;

     while (counter < 2){
         string pss;
         pss = pp.encryption(psw);
      for (int x = 0; x < dss.size(); x++)
        {
        if (usr == dss.at(x).getusername()){
            t = true;
            for (int o = 0; o < dss.size(); o++){
            if (pss == dss.at(o).getpassword()){
                y = true;
                break;
            }
        }
        }
        }
     if(t == true){
         if(y == true){
             char newpw[20];
             string a;
             string b;
             int pcount = 0;
             cout << "\nlogin sucessful\n";
             while(pcount ==0){
             cout << "\nplease type in your new password\n";
             cin >> newpw;
             b = newpw;
             int i = 0;
             int o = 0;
             while(newpw[i]){
                 if(isalpha(newpw[i])){
                     n = true;
                     //while(isdigit(newpw[o])){
                         if(isdigit(newpw[o] = 0)){
                             o++;

                         }
                         else{
                             m = true;
                             break;
                         }

                     
                 }
                 else{
                     i++;
                 }

    }

             if(b.length()>9 && n==true && m==true){

             a = pp.encryption(newpw);

             //cs.encryption(newpw);
             ofstream writedata("cashier.txt");
              for(int i=0; i < rows; i++){
              if (usr == dss.at(i).getusername()){

                  writedata << dss.at(i).getusername()+" "<< a+" " << dss.at(i).getlock()+"\n";
              }
              else{
              writedata << dss.at(i).getusername()+" "<< dss.at(i).getpassword()+" " << dss.at(i).getlock()+"\n";
              }
               
          }
             writedata.close();
             cout << "password changed!\n";
             counter = 10;
             pcount = 10;

             }
             else{
                 cout << "Password must contain at least 8 character\n";
                 cout << "Password must contain at least one letter\n";
                 cout << "Password must contain at least one numeric letter\n";

             }
             }

             }
         else{
             cout << "\nwrong password please try again\n";
             cin >> psw;
             counter++;
             
         }

     }
     else
     {
         cout << "\nwrong username please try again\n";
         cin >> usr;
     }
      if(counter == 2){
          cout << "\naccount is locked\n";
          ofstream writedata("cashier.txt");
              for(int i=0; i < rows; i++){
              if (usr == dss.at(i).getusername()){

                  writedata << dss.at(i).getusername()+" "<< dss.at(i).getpassword()+" " << "yes\n";
              }
              else
              writedata << dss.at(i).getusername()+" "<< dss.at(i).getpassword()+" " << dss.at(i).getlock()+"\n";
          }
          writedata.close();
      }

}
     ds.flush();
}


hi my apologise im really new to this forum and c++ i have pasted my whole code so maybe you guys would understand..

as the requirements require me to do this..

"Password must contain at least 8 character\n";
"Password must contain at least one letter\n";
"Password must contain at least one numeric letter\n"
Thanks!
Last edited on
Would you mind editing your post and putting the source inside code tags? It will make it a lot more legible and folks here will be more likely to look at it.
Why do you use a char array and C++ strings? Why not use strings for reading in?
string newPassword;
Then also instead of using cin >> newPassword; use getline(cin, newPassword); so users can use spaces in their password and it gathers everything up to the newline (\n) character.

I have problems going through the rest of your code. What are you doing with all these integers? (pcount, i, o) and bools? (n, m). Give more descriptive names!
I think you wrapped everything in the while with

1
2
3
4
5
if ( isalpha(newpw[i]) ) {
 // stuff
} else {
  i++;
}


so if it's a digit, then it will never add 1 to i,,,

plus, why are you doing if(isdigit(newpw[o] = 0)){

newpw[o] here will always be a null char, because you set it to that...


-- edit: oh, and pcount is never changed in the loop, so it will always be 0
Last edited on
@Cranium: That last code you did, regarding the newpw[o]=0, I believe that's the typical beginner mistake: I believe he meant to say:

if(isdigit(newpw[o] == 0)){
It doesn't make sense either.
newpw[o] == 0 will return true or false. Why ask if that is a digit?
i have edited the post... sry im kinda new and noob in c++ +_+
too long. Try to make just one thing per function and comment your code

"Password must contain at least 8 character\n";
"Password must contain at least one letter\n";
"Password must contain at least one numeric letter\n"
You could encapsulate those requirements in one function password.is_good()

line 14-31: Are you trying to check if the user and password are valid? Let a function take care of that.
Maybe you should be logged to try to change your password
1
2
if( user.is_logged() )
  user.change_password();


line 32-39 and 44-58: What is the purpose?

btw: if( var==true ) is equivalent to if( var )

Edit: instead of reading and writing to the file every time you could load all the information to the memory. Make a commit function to write the changes.
You could use binary files, so you will not update the info that didn't change
Last edited on
Topic archived. No new replies allowed.