#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main ()
{
int Choice = 0; //User Choices
ifstream Reader; //Reads Password.txt
ofstream Writer; //Writes in Password.txt
string UserPassword , PasswordCheck , NewPassword; //Strings for passwords
Reader.open ("C:\\Password.txt"); //Opens Password.txt
if (Reader.is_open ())
{
getline (Reader , UserPassword); //Reads from Password.txt
cout << "Please enter your exsiting password." << endl;
getline (cin , PasswordCheck);
cin.sync ();
if (PasswordCheck == UserPassword) //Checks PasswordCheck
{
cout << "Do you wish to change the password (1 = Yes 2 = No)" << endl;
cin >> Choice;
cin.sync ();
if (Choice == 1)
{
cout << "Please enter the new password." << endl;
getline (cin , NewPassword);
cin.sync ();
Writer.open ("C:\\Password.txt");
Writer << NewPassword << endl;
Writer.close ();
cout << "Please restart the program." << endl;
}
else
{
cout << "Hello World!" << endl; //Displays Hello World!
}
Reader.close ();
}
else
{
cout << "Incorrect password." << endl;
}
}
else
{
cout << "Please enter your new password." << endl; //Asks for new password
getline (cin , UserPassword);
cin.sync ();
Writer.open ("C:\\Password.txt");
Writer << UserPassword << endl;
Writer.close ();
cout << "Please restart the program." << endl;
}
cout << "Press ENTER to end the program." << endl;
cin.get ();
return 0;
}
That should give you this
//First Run
Please enter your new password.
Hello World
Please restart the program.
Press ENTER to end the program.
//Second Run
Please enter your exsiting password.
Hello World
Do you wish to change the password (1 = Yes 2 = No)
1
Please enter the new password.
Weezer
Please restart the program.
Press ENTER to end the program.
//Third Run
Please enter your exsiting password.
Weezer
Do you wish to change the password (1 = Yes 2 = No)
2
Hello World!
Press ENTER to end the program.