SIGN-UP/LOG-IN: "enter password" gets erased

Hi, I have another problem I'm hoping someone could help me with. We were tasked to make a sign-up program where only the password has the validation: atleast 1 uppercase, lowercase and 6-10 characters.
I used the \b char. But when I backspace my password, even the "enter password" gets erased even though it's in an outside function..

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
#include <iostream>
#include <conio.h>
#include <string>
#include <cstdlib>
using namespace std;


string pw()
{ string pass,length;
int cnt;
char z;
    cout << " enter  password: ";
    pass="";
while (z!=13)
    {
        z=getch();
        pass+=z;
 if (z==8)
        {
           cout<<'\b'<<" "<<'\b';
           if (pass.length()<0)
           {
               continue;
           }
        }
 else if (z==13)
 {
     break;
     cin.ignore();

 }
        else{cout<<"*";}

    }


    return pass;
}

string b()
{string logpass;
    char w;
    cout << "Enter password: ";
logpass="";
    while (w!=13)
    {
        w=getch();
        logpass+=w;
 if (w==8)
        {
           cout<<'\b'<<" "<<'\b';
        }
  else if (w==13)
 {
     break;
     cin.ignore();

}        else{cout<<"*";}

    }

    cout<<endl;
return logpass;

}

int main ()
{
    string user,password, x,y,count;
    char p;
    int length,acc=1,hasup,haslow,hasalpha;
    int ctr = 0;

    cout<<"You must first create an account"<<endl;
    system("pause");
    system("cls");

while (acc!=0)
{

    cout<<"SIGN-UP FOR AN ACCOUNT"<<endl;
    cout << "Enter username: ";
   getline(cin,x);
    y=pw();
    length=y.length()-1;
//cout<<length;

         if (length>=6&&length<=10)
        {  char v[length];
        int i;

            for (int i = 0; i < length; i++)
            { v[i]=y[i];

            }
            hasup=0;
            haslow=0;
            hasalpha=0;

            for (int i = 0; i < length and v[i]; i++)
            {if (isupper(v[i]))
            {hasup=1;}
            else if (islower(v[i]))
            {haslow=1;}
            if (isalpha(v[i]))
                {hasalpha=1;}
            }
            }if (haslow==1 && hasup==1 &&hasalpha==1)
            {

                    cout<< "Sign in successful!"<<endl;
                acc=0;
                system("pause");
                system("cls");
            }
            else
            {
                 cout<<"Password should have at least 1 uppercase and lowercase letter, 1 digit and 6 to 10 characters."<<endl;
        system("pause");
        system("cls");
            }
        }



string cun,logpass,z;
    while (cun!=x && z!=y)
    {
        cout<<"LOG IN TO YOUR ACCOUNT"<<endl;
        cout<<"Enter username: ";
        getline(cin,cun);
        z=b();
        if (cun!=x ||z!=y)
        {
            cout<<"Invalid username or password"<<endl;
            system("pause");
            system("cls");
        }
        else{
            continue;
        }
    }

        cout<<"WELCOME TO CEA"<<endl<<"You are now logged in! "<<endl<<"ENJOY YOUR FREE STAY"<<endl;
         system("pause");
        system("cls");

    return 0;
} 




Last edited on
Please edit your post to include code tags.
http://www.cplusplus.com/articles/jEywvCM9/
I'm sorry, I've edited it now :)
@WywrdAD,

I can't reproduce your problem. Explain what input is causing difficulties.

Please take a look at your post: do you seriously think that its indentation is meaningful?


You haven't initialised char z, and I can't believe that these lines ever do anything except take up screen space:
1
2
3
4
           if (pass.length()<0)
           {
               continue;
           }
Last edited on
I think that is where the problem is.

if (pass.length()<0)
{
continue;
}

I meant to limit the ability of the backspace to continue when there is no password to erase. But when I press backspace when it asks me for a password, even the words " enter password: " gets backspaced.

I can't see how pass.length()<0 would ever be true, so I suspect those lines do precisely nothing.

When I pressed backspace it backspaced one character. So, I simply can't reproduce what you are claiming.
Oh i get it now, I'm sorry. My wording should be: But when I continually press backspace after inputting a char/ password, even the words " enter password: " gets backspaced.
Well, only allow it to backspace if pass.length() is strictly greater than 0.
Like this? Still won't work

1
2
3
4
5
6
7
8
 if (z==8)
        {
           if (pass.length()>0)
           {
              cout<<'\b'<<" "<<'\b';
           }
          
        }
Yes, well you have back-spaced on the screen ... but you haven't deleted the characters in your password!
Oh! Thank you for that! But how can I avoid backspacing on the screen as well? i tried putting a cout<<"enter password: " in the else block but no dice :( I'm sorry, I am really slow.
Your method will probably work IF you match pass.length() with the number of asterisks on the screen. However, unless you delete the end characters from pass at the same time as back-spacing then this will not be the case.

I can see some backspacing, but I see nothing in any of your code samples that is deleting chars from the back of pass.
Last edited on
You're right! I just added a counter for the characters and it worked. Thank you so much!
Topic archived. No new replies allowed.