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