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)


I basically want the contents of the file 'file' to be the password, so that the password can be changed remotely, from someone using the program, not the programmer themself.

Recommended Answers

All 7 Replies

>>if(output == "file.txt")
You can not compare two character arrays like that. You have to use strcmp() if( strcmp(output, "file.txt") == 0) >>if(b == "file.txt")
That is trying to compare a single character with a character array. And the line above it is asking you to enter a string of characters, not a single character. Declare b as a charcter array, then use strcmp() as previously mentioned.

what does

if( strcmp(output, "file.txt") == 0)

do?

You can not make more functions that way. What you can do is for example:
EDIT:

void function1()
{
   cin.clear();
   cin.ignore(255, '\n');
   cin.get();
   exit(1);
}

...
     if(output == pasword)
     {
      cout << "\nPassword change complete.\n" << endl;
      main2(); //Here you can call function1()
...
    if(z == y)
    {
     main2(); //Here you can call function1()
...

Just use strings.

std::string input;
std::string pass = "file.txt";
cin >> input;
if(input == pass){ /* success */ }
else { /* fail */ }

what does

if( strcmp(output, "file.txt") == 0)

do?

I don't intend to sound mean, but look up strcmp() in your textbook or google for it. If you can't undedrstand that then you are attempting to code stuff that is over your head. You need to start over and read your text book again from page 1, do all exercises, or read online tutorials more thoroughly.


>>Just use strings.
That doesn't help learning character arrays.

Yeah your right. I Just started learning it. The problem is you learn one thing and try to write a program for it and then that requires other things and some of those things are more complicated =D but your right thanks

No one said programming is easy. It might be easy for some people, but for us mear mortals learning it can be difficult and very very time consuming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.