Mar 25, 2015 at 3:50am UTC
How can i display a string per line on the .txt i made?
There are 2 .txt files, the one has:
John
Finn
Xach
The other is:
password1
password2
password3
Question is: How can i display, let's say, i input "John" so it will display something like this "John's password is password1" or if i input Xach, "Xach's password is password3"
When i use something like
1 2 3 4 5 6
while (getline(passwordFile, str))
{
cout<< username <<" password is " << str;
break ;
}
It will work for when i input "John", but it doesn't work if I input "Finn" or "Xach"
Last edited on Mar 25, 2015 at 3:55am UTC
Mar 25, 2015 at 5:22am UTC
Read the files in parallel.
Mar 26, 2015 at 4:30am UTC
1 2 3 4 5 6 7 8
while (getline(userFile, str1) && getline(passFile, str2))
{
if (str1 == userIn && str2 == passIn)
{
cout<< str1 << " password is " << str2;
break ;
}
}
It does not seem to work. I think i'm doing something wrong
Last edited on Mar 26, 2015 at 4:31am UTC
Mar 26, 2015 at 5:42am UTC
The code:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream userTxtFile("user.txt" );
ifstream passTxtFile("pass.txt" );
string userin, passin;
string str;
int i,j;
bool usercheck = false ;
bool passcheck = false ;
if (userTxtFile.is_open() && passTxtFile.is_open())
{
cout<<"Enter username: " ;
cin>>userin;
for (i = 0;getline(userTxtFile, str); i++)
{
if (str.compare(userin) == 0)
{
usercheck = true ;
break ;
}
}
if (usercheck)
{
cout<<"Enter password for " << userin << ": " ;
cin>>passin;
for (j = 0; getline (passTxtFile, str);j++)
{
if (str.compare(passin) == 0 && i == j)
{
passcheck = true ;
break ;
}
}
if (passcheck)
{
//This is the where my problem starts, dunno how to display the user and pass of the user input
}
else
{
cout<<"Password not found." << endl;
}
}
else
{
cout<<"No user found." << endl;
}
userTxtFile.close();
passTxtFile.close();
}
else
{
cout<<"File can't be opened." ;
}
}
}
Last edited on Mar 26, 2015 at 5:42am UTC