Program not running after log-in / censoring password

Hello everyone! I'm having trouble writing my program which is a simple string scanner, i've receently added a log-in screen to it which works fine.. although i would like for the password to be censored when the user types in the password or something. As of now, it is displayed in plaintext as the user types in the password...

Also! A little trouble getting my program to run after the user has actually typed in the password, i cannot understand why my program just closes after the user has logged in. Say, if i remove my log-in part the program would work just fine!

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

int main () {
    string userName;
    string userPassword;
    int loginAttempt = 0;

    while (loginAttempt < 5)
    {
        cout << "Please enter your user name: ";
        cin >> userName;
        cout << "Please enter your user password: ";
        cin >> userPassword;

        if (userName == "Test" && userPassword == "Test")
        {
            cout << "Welcome!\n";
            break;
        }
        else
        {
            cout << "Invalid login attempt. Please try again.\n" << '\n';
            loginAttempt++;
        }
    }
    if (loginAttempt == 5)
    {
            cout << "Too many login attempts! The program will now terminate.";
            return 0;
    }

    cout << "Thank you for logging in.\n";
    system("CLS");
    cin.get ();

    {
        ifstream input;
		size_t pos;
              string line;

		input.open("t.txt");
		if(input.is_open())
		{
			while(getline(input,line))
			{
			 pos = line.find("Hey");
			  if(pos!=string::npos)
        {
            cout <<"Found";
            break;
        }
			}
		}
		{
			while(getline(input,line))
			{
			 pos = line.find("Ho");
			  if(pos!=string::npos)
        {
            cout <<"\nFound";
            break;
        }
			}
		}
		{
			while(getline(input,line))
			{
			 pos = line.find("Ho");
			  if(pos!=string::npos)
        {
            cout <<"\nFound demon xd";
            break;
        }
			}
		}
		{
			while(getline(input,line))
			{
			 pos = line.find("Ho");
			  if(pos!=string::npos)
        {
            cout <<"\nFound";
            break;
        }
			}
		}
		{
			while(getline(input,line))
			{
			 pos = line.find("Ho");
			  if(pos!=string::npos)
        {
            cout <<"\nFound";
            break;
        }
			}
		}
    cin.get ();
}}
Last edited on
Hello Moobman,

Although I do not recommend using "conio.h", as it is outdated and not every compiler has this file available, this is a solution that can give you an idea of what to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	while (loginAttempt < 5)
	{
		cout << "Please enter your user name: ";
		cin >> userName;
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires heder file <limits>.
		
		cout << "Please enter your user password: ";
		char ch{};
		ch = _getch();

		while (ch != 13)
		{
			//cin >> userPassword;
			std::cout << '*';
			userPassword += ch;
			ch = _getch();
		}

                .
                .
                .

Unfortunately C++ does not have an equivalent to "_getch()", but I have seen a function that will mimic this. At the moment I am not sure what the best approach is to do this.

Line 38 is a problem. After a successful login the program clears the screen and leaves you staring at a blank screen with no idea what to do.

It is best not to use "system" anything as this could leave your program vulnerable to attack. Also "system" may not be available to every compiler.

Line 40 and what should be line 103 the {}s are not really needed, but OK. Also lines 81 and 91 along with all the other around the while loop. Unless your intention was to limit scope.

Line 46. the if statement needs an else statement. Here is a suggestion:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
		if (input.is_open())
		{
			while (getline(input, line))
			{
				pos = line.find("Hey");
				if (pos != string::npos)
				{
					cout << "Found";
					break;
				}
			}
		}
		else
		{
			std::cout << "\n File \"t.txt\" did not open\n";
			std::this_thread::sleep_for(std::chrono::seconds(2));  // Requires header files "chrono" and "thread"
			exit(1);
		}


Anything after that none of the while loops will be reading form the beginning of the file and if "EOF" was reached in the first while loop all the rest will fail. To reset the file pointer you could use inFile.seekg(0, std::ios::beg); before each successive while loop.

At the end of the block there is a "cin.get()" with no prompt, so the user has no idea what to do.

At the end of main "return 0;" is good form and programming but not actually necessary.

Hope that helps,

Andy
Topic archived. No new replies allowed.