I made a simple "enter your password" program using C++ language and the password is "Hello", I want to know if my way for coding is simple enough or is making things more complicated than it needs to be
#include <iostream>
using namespace std;
main()
{
int i ;
string P= "Hello" ; // P=password
string EP ; // EP=entered password
cout << "please enter your password :" ;
for(i=1;i<=5;i++)
{
cin >> EP ;
if (EP==P)
{
cout << '\n' << '\n' << "correct !! " << "welcome to the programe :D !!" ;
i=8 ; //I made it 8 to stop the For loop
}
else
{
if (i==5)
++i ; /*I put the plus 1 so I that when the user try 5 times it wont have "please try again" massage printed again
after the "please leave" one */
else
cout << "wrong password" << '\n' << "please try again :" ;
}
if(i==4) //this just a warning letter before the last try
cout << '\n' << '\n' <<"warning, you have one try left :" ;
}
if(i==7)
cout << '\n' << '\n' << "you've failed to log in, please leave" ;
}
\nwhen streamingbreakkeywordif (i == 5) i++;ias a status, you should either use an enum for multiple status or a clearly named variable, so that other coders can understand your intentions easilystd::endlat the end of execution, so the next commandline input prompt does not get added to the end of your program's last line of output.#include <iostream> using namespace std; int main() { string correctPassword = "Hello"; bool loginSuccessful = false; string userInput; cout << "Please enter your password: "; for (int i = 1; i <= 5; i++) { cin >> userInput; if (userInput == correctPassword) { loginSuccessful = true; break; } else { cout << "wrong password\nplease try again: "; } if (i == 4) // this just a warning note before the last try cout << "\n\nwarning, you have one try left: "; } if (!loginSuccessful) { cout << "\n\nyou failed to log in, please leave" << endl; return 0; } cout << "\n\ncorrect !! welcome to the programe :D !!"; // the password-secured program code here return 0; }