LOGIN

Apr 10, 2009 at 2:35pm
please i was wondering if anyone could help me with my code, i feel its kinda too long and a good program should be as brief as possible... please would be very grateful. appreciated, cheers
int _tmain(int argc, _TCHAR* argv[])
{
string Username;
string Password;
int terminator = 0, terminate = 0;
char encrypt;

while (terminate < 2)
{
cout << "Enter Username: ";
getline(std::cin, Username);
if (Username == "Raymond")

{
terminate = terminate + 3;

while (terminator < 2) {
cout << "\nEnter Password: ";
encrypt = _getch();


while(encrypt != 13)

{
Password.push_back(encrypt);
cout << '*';
encrypt = _getch();
}

if (Password == "123456")
{
cout <<"\n\n\t\t******************************";
cout << "\n\t\t* *";
cout << "\n\t\t* *";
cout << "\n\t\t* Welcome Raymond * ";
cout << "\n\t\t* *";
cout <<"\n\t\t******************************"<< endl<<endl ;
terminator = terminator + 3;
}

else
{
cout << "\nInvalid password. Please enter valid password." << std::endl;
cout << "\a\n";
terminator = terminator + 1;
}
}

} else {
cout << "Invalid username. Please enter valid username." << std::endl;
cout << "\a\n";
terminate = terminate + 1;
}
}


return 0;

}
Apr 10, 2009 at 3:47pm
i cant givea proper reply as im on a phonebutfrom first glance... lines like

terminate= terminate + 3 can be shortened to terminate += 3

EDIT**

Hmm this might just be me but your program doesn't seem to make sense. You seem to jump from asking for the username to asking for the password without coming out of the first while loop. Personally I would use something like:

1
2
3
4
5
6
7
8
9
while (Username !="Raymond")
  {
      cout << "Enter Username: "; 
      getline(std::cin, Username); 
      terminate++;
      if(terminate==2){
          cout << "too many invalid attempts. Quiting...";
          return 0; }
   }


This kind of thing would work better, bear in mind that there is no point in doing this for the username, only the password
Last edited on Apr 10, 2009 at 6:20pm
Apr 10, 2009 at 10:16pm
It appears "long" because you're sugarcoating the program with extras to make it look nice and complicated (a common mistake). A small, brief login program would be this (I mixed it up a bit):

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

using namespace std;

int main()
{
checkpoint:
string username;
string password;
string passwordcheck;
int selection;
int terminate;
cout<<"1. Create account\n2. Login\n";
cin>>selection;

switch (selection)
 {
      case 1:
                   cout<<"Enter account username: ";
                   getline(cin, username);
                   cout<<"Enter account password: ";
                   getline(cin, password);
                   cout<<"Please re-enter your password: ";
                   getline(cin, passwordcheck);
                   if (password == passwordcheck)
                  {
                       cout<<"Very well. Your account has been created.\n";
                   }
                   else {
                                cout<<"The two passwords don't match.\n";
                                goto checkpoint;
                           }
         break;
         case 2:
                      // code to log in; enter username and password and check comparison to previous 
                     // login details, if any
 }

etc. etc.
Apr 11, 2009 at 1:06am
Well that go to its just not right way to get out in C++ (more of C) and validate.... I'll prefer to do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
switch (selection)
 {
       case 1:
                   cout<<"Enter account username: ";
                   getline(cin, username);
                   cout<<"Enter account password: ";
                   getline(cin, password);
                   cout<<"Please re-enter your password: ";
                   getline(cin, passwordcheck);

                   if (password == passwordcheck)
                  {
                       cout<<"Very well. Your account has been created.\n";
                   }
                   else {
                               while (password != passwordcheck)
                                 {
                                 cerr <<"The two passwords don't match.\n";

                                 cout <<"Re-enter your password: ";
                                 getline (cin, password);
                                }
                            }
Last edited on Apr 11, 2009 at 1:09am
Topic archived. No new replies allowed.