Simple password program with fstream

Im sort of newish I guess, I know basically what im doing, but this is one of my first projects with fstream.Im trying to make a program that writes a password to a txt file and then has to retrieve the info in the txt file and decide if it matches the user inputed password.there are no errors but the if statement does not work. please no unnecessary changes to the coding, im trying to keep it in this format... THANKS!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
using namespace std;
void passfn()
{
    char passguess[256];
    cout <<"Enter the password:";
    cin >> passguess;
    ofstream passfile;
    passfile.open("passtext.txt");
    if(passguess == passfile)
    {
        cout << "You win!";
    }
}
int main ()
{
  cout << "Enter a password:";
  char passs[256];
  cin >> passs;
  ofstream myfile;
  myfile.open ("c:\\++\\passtext.txt",ios::trunc);
  myfile << passs;
  myfile.close();
  passfn();


}
anybody??? 0.o
i get a little fuzzy when i tried to check your program (hahahhaah my bad) and the answer is you didn't give properly address of the passtext.txt
I'm not an expert but this works.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
void passfn()
{
    string line;
    char passguess[256];
    cout <<"Enter the password:";
    cin.getline(passguess,sizeof(passguess));
    ifstream passfile;
    passfile.open("passtext.txt");
    getline (passfile, line);
    if(passguess == line)
    {
        cout << "You win!";
    }
}
int main ()
{
  cout << "Enter a password:";
  char passs[256];
  ofstream myfile;
  cin.getline(passs,sizeof(passs));
  myfile.open ("passtext.txt", ios::trunc);
  myfile << passs;
  myfile.close();
  passfn();

}
Last edited on
string is good, but you should use it consistently:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void passfn()
{
  cout << "Enter the password: ";
  string passwordGuess;
  cin >> passwordGuess;
  ifstream passfile("passtext.txt");
  string realPassword;
  getline(passfile,realPassword);
  if (passwordGuess==realPassword)cout << "You win!" << endl;
}

int main()
{
  cout << "Enter a password:";
  string password;
  getline(cin,password);
  ofstream myfile("passtext.txt",ios::trunc);
  myfile << password << endl;
  myfile.close();

  passfn();
}
I'm pretty sure you HAVE to use a string in this case. Simple because a 'char' array of
{'p','a','s','s','w','o','r','d', '\0', NULL, NULL, NULL ,NULL, ... }
is distintly different from a 'char' array of
{'p', 'a', 's', 's', 'w', 'o', 'r', 'd', NULL, NULL, NULL, NULL, NULL, ... '/0'}
Last edited on
Hi...
I hope this help....


#include <iostream>
#include <fstream>
using namespace std;
void passfn()
{
    char passguess[256];
    cout <<"Enter the password:";
    cin >> passguess; 

    string input;
    ifstream passfile;
    //ifstream is used instead of ofstream cuz you wants to read
    //the information from a file
    //if ofstream is used, this mean that you want to write
    //the information into a file

    passfile.open("passtext.txt");
    passfile>>input;
    //input act as a variable
    //it will specify what type of information you will get
    //then , input is type string and passguess is char
    //they are the same since a string is basically is a char 
    //which have been group together
    //you can change it back it type char
    //maybe char input[8] @ input[4] @whatever is logic

    if(passguess == input)
    {
        cout << "You win!";
    }
}
int main ()
{
  cout << "Enter a password:";
  char passs[256];
  cin >> passs;

  ofstream myfile;
  myfile.open ("C:\\Users\\Guest\\Desktop\\m.txt",ios::trunc);
  myfile << passs;
  myfile.close();
  passfn();

system("pause");

}


So... i hope this will help u.
if im wrong, my bad...
Topic archived. No new replies allowed.