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!
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'}
#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...