Continue program after login?

Hey, ive been trying to get my actual program to work after this login screen here. But i simply cannot figure out how to do it, can anybody point me in the right direction? Thank you!

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

int main()
{
    START:
    system("cls");
    cout<<"\nEnter Password  : ";
    char pass[32];
    int i = 0;
    char a;
    for(i=0;;)
    {
        a=getch();
        if((a>='a'&&a<='z')||(a>='A'&&a<='Z')||(a>='0'&&a<='9'))

        {
            pass[i]=a;
            ++i;
            cout<<"*";
        }
        if(a=='\b'&&i>=1)

        {
            cout<<"\b \b";
            --i;
        }
        if(a=='\r')
        {
            pass[i]='\0';
            break;
        }
    }
    if (pass == "Test") {
        cout<<"\nYou have successfully logged in!";
        getch();
        goto PROGRAM;
    } else {
    getch();
    goto START;
    }

PROGRAM:
{
  cout << "Hello World!";
}
}
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
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <conio.h>
using namespace std;

//======================================================================

void getPassword( int maxTry )
{
   string pass;
   string correct = "Test";
   int ntry = 0;
   char a;

   while( ntry < maxTry )
   {
      ntry++;
      pass = "";
      cout << "\nEnter password: ";

      while ( ( a = getch() ) != '\r' )
      {
         if ( isprint( a ) )           // your current code effectively used isalnum()
         {
            pass += a;
            cout << "*";
         }
         else if ( a == '\b' && pass.size() )
         {
            pass.pop_back();
            cout << "\b \b";
         }
      }

      if ( pass == correct )
      {
         cout << "\nYou have successfully logged in! Press any key ...\n\n";
         getch();
         return;             // goto is ... NO, I'm NOT entering that debate !!!!
      }
      else
      {
         cout << "\nNah, try again\n\n";
      }
   }

   cout << "Too many tries. Go away.";
   exit( 1 );
}

//======================================================================

void doSomething()
{
  cout << "Hello World!";
}

//======================================================================

int main()
{
   getPassword( 3 );
   doSomething();
}

//====================================================================== 

Last edited on
Line 36 is wrong. It compares the address of the pass array with the address of the string literal "Test" and clearly they are never the same.

To compare the contents of the pass array with "Test", use strcmp:
if (strcmp(pass, "Test") == 0) {

But really you should use std::string for pass instead of a 32 byte array. At the very least, guard against the user entering more than 31 characters. Otherwise your code has a classic buffer overflow security bug.
Topic archived. No new replies allowed.