How can I get a password that has been changed during the use of the program to be applied so that after the program is ended and started again, when it prompts for a password to start the program it wants the new password the user entered, not the original one? I know that sounds confusing but:

string o, password("oscar"), password2;
string password3, password4;

int main1()
{
 cin >> o;
 if(o == password)
 {cout << endl;
 main2();}
 else
 {cout << "\n\aIncorrect password.\n" << endl;
 main();}
   cin.clear();
   cin.ignore(255, '\n');
   cin.get();
   return 0;
}

int pWord()
{
cout << "\nEnter current password: \n" << endl;
 cin >> o;
 if(o == password)
 { 
  cout << "\nEnter new password:\n" << endl;
  cin >> password2;
  cout << "\nVerify new password:\n" << endl;
  cin >> password3;
  if(password2 == password3)
  {
   cout << "\nPassword change complete.\n" << endl;
   main2(); }
  else
  { cout << "\nPasswords do not match.\n" << endl; 
  pWord(); } 
  }
 else
  {
   cout << "\nIncorrect password.\n" << endl;
   pWord(); }
  }

Those are the two main functions in my program that involve what I am talking about. The first is the function that prompts you for the password to begin the program, the second is a function within the program that allows you (or at least is supposed to allow you) to change the password that is requested in the first function. Any ideas how to make that happen?

Edited 6 Years Ago by Rickay: syntax mistake

One easy solution is to use an fstream object. This can be used to write your password out to a text file, and then every time your program runs again, you can have it load your variable into a string for comparison.

#include <fstream.h>

void main
{
	ifstream file;
	char output[100];
	int x;
	
	file.open("file.txt");	//open a file
	
	file>>output;		//write to it
	cout<<output;		//read from it
	
	file.close();			//close it
}

Edited 6 Years Ago by Duki: n/a

In that program, would the file 'file' be created, or would the file already have to exist?

why doesn't this work?

int main()
{
 fstream file;
 char output[100], pasword[100];
 int a;
 const string x("Yes");
 const string y("No");
 string z;
 char b;
 cout << "\nEnter correct password to continue: \n" << endl;
 cin.getline(output, 15);
 if(output == "file.txt")
 {
  cout << "Change password? Type Yes or No. \n" << endl;
  cin >> z;
  if(z == x)
  {
   cout << "\nEnter current password: \n" << endl;
   cin >> b;
   if(b == "file.txt")
    { 
     cout << "\nEnter new password:\n" << endl;
     file.open("file.txt");
     file >> output;
     file.close();
     cout << "\nVerify new password:\n" << endl;
     file.open("file.txt");
     file >> pasword;
     file.close();
     if(output == pasword)
     {
      cout << "\nPassword change complete.\n" << endl;
      main2(); }
     else
     { cout << "\nPasswords do not match.\n" << endl; 
     main(); } 
     }}
    if(z == y)
    {
     main2();
    }
    else
    {
   cout << "\nIncorrect password.\n" << endl;
   main(); }
  }
 else
 {cout << "\nIncorrect password.\n" << endl;
 main();}
   cin.clear();
   cin.ignore(255, '\n');
   cin.get();
   return 0;
 main2();
 return 0;
}

(by the way main2() is the actual program, main in itself is basically the authentication function)

Edited 6 Years Ago by Rickay: syntax mistake

In other words, how can I make the contents of file "file.txt" the password to the program so that whatever is in the file is the password?

This article has been dead for over six months. Start a new discussion instead.