SIGN-UP/LOG-IN: Login While loop exits early

I'm sorry for passing through here again but i swear, I'm almost done.

The problem lies in the last part: the while loop for confirming the username/password. When I enter a wrong pass/username, it exits the while loop already even though it enters the "Invalid username/password", so it also says that "you are now logged in". Why does it exit early???

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


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

}        else{cout<<"*";}

    }

    cout<<endl;
return logpass;

}

int main ()
{
    string x,y
    ;
    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);
    string pass;
int cnt=0;
char z=0;
    cout << " enter  password: ";
    pass="";
while (z!=13)
    {
        z=getch();
        pass+=z;
        cnt++;
 if (z==8)
        {--cnt;

           if (cnt>0)
           {
              cout<<'\b'<<" "<<'\b';

           }
           else { continue;}
            --cnt;
        }
else if (z==13)
        {--cnt;

        break;
        cin.ignore();
        }
else{cout<<"*";}

    }
y=pass;
length=cnt;
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,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");

        }

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

    return 0;
}
It will be easier to just print it and see for yourself.
print, in hex, cun, x, z, and y in that loop. watch it exit. see why it exited.

it seems weird that you add invalid chars on input (eg ascii 8) and decrease 'num' whatever that is but do not remove the char from the string. It seems to me that if you get a bad char you would not add it to the string at all, and you would use string.length() instead of trying to keep track yourself. It could be right, I dunno, but it looks weird.
You need to replace that alphabet soup of single letter identifiers and other cryptics with meaningful names.

Some more functions to make main() less bloated wouldn't go amiss either.

Your indentation is also a mess.

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

#ifdef _WIN32
#include <conio.h>
#else
char getch(void);
char getche(void);
#endif

void pause();
void clear();

string getpass()
{
    string pass;
    for (char ch; ch != '\r' && ch != '\n'; )
    {
        ch = getch();
        if (ch == '\b' || ch == '\x7f')
        {
            if (pass.size() > 0)
            {
                cout << "\b \b" << flush;
                pass.pop_back();
            }
        }
        else if (ch != '\r' && ch != '\n')
        {
            pass += ch;
            cout << '*' << flush;
        }
    }
    cout << endl;
    return pass;
}

void signup(string& username, string& password)
{
    for (bool accepted = false; !accepted; )
    {
        clear();
        cout << "SIGN-UP FOR AN ACCOUNT\n";

        cout << "Enter username: ";
        getline(cin, username);

        cout << "Enter password: ";
        password = getpass();

        bool hasup = false, haslow = false, hasalpha = false;

        if (password.size() >= 6 && password.size() <= 10)
            for (size_t i = 0; i < password.size(); i++)
            {
                hasup    |= isupper(password[i]);
                haslow   |= islower(password[i]);
                hasalpha |= isalpha(password[i]);
            }
        
        if (haslow && hasup && hasalpha)
        {
            cout << "Sign in successful!\n";
            accepted = true;
        }
        else
            cout << "Password should have at least 1 uppercase and"
                    "lowercase letter, 1 digit and 6 to 10 characters.\n";
        pause();
    }
}

void login(const string& username, const string& password)
{
    string login_user, login_pass;
    while (login_user != username || login_pass != password)
    {
        clear();
        cout << "LOG IN TO YOUR ACCOUNT\n";

        cout << "Enter username: ";
        getline(cin, login_user);

        cout << "Enter password: ";
        login_pass = getpass();

        if (login_user != username || login_pass != password)
        {
            cout << "Invalid username or password\n";
            pause();
        }
    }
}

int main ()
{
    clear();
    cout << "You must first create an account\n";
    pause();

    string username, password;
    signup(username, password);
    login(username, password);

    clear();
    cout << "WELCOME TO CEA\n"
         << "You are now logged in!\n";
    pause();
    clear();
}


#ifdef _WIN32
#include <cstdlib>
void pause() { system("pause"); }
void clear() { system("cls"); }
#else
#include <termios.h>
#include <cstdio> // getchar
static struct termios old, current;
void initTermios(int echo) {
  tcgetattr(0, &old);
  current = old;
  current.c_lflag &= ~ICANON;
  if (echo) current.c_lflag |=  ECHO;
  else      current.c_lflag &= ~ECHO;
  tcsetattr(0, TCSANOW, &current);
}
void resetTermios(void) { tcsetattr(0, TCSANOW, &old); }
char getch_(int echo) {
  initTermios(echo);
  char ch = getchar();
  resetTermios();
  return ch;
}
char getch(void) { return getch_(0); }
char getche(void) { return getch_(1); }
void pause() {
    std::cout << "Press any key to continue...";
    getch();
}
void clear() { std::cout << "\x1b[H\x1b[J"; }
#endif 

Line 14: w is uninitialized
Line 26: why decrement num a second time? Actually, why do have num at all? It isn't used.
Line 31 is never reached.
Lines 65-89: This looks the same as b(). Don't duplicate code.
Line 90: Why make a copy of pass? Why not just use it directly?
Line 91 could just be length = pass.size(); Better yet, don't use length at all and use pass.size() instead.
Line 95: Standard C++ doesn't allow variable length arrays. Use a string instead.
Line 99: Why make a copy of y? You could use pass directly here too.
Line 114: This code runs after the if at line 94, so if the length is less than 6 or greater than 10, then it checks hasup, haslow and hasalpha when they are uninitialized. This is why it's so important to indent your code to reflect the actual block structure.

Topic archived. No new replies allowed.