How to display a string per line (fstream)

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
Read the files in parallel.
If i read them in parallel, it will display all passwords

The output would be:

John password is password1 password2 password3
In parallel, not nested inside each other.
Something like:
1
2
3
4
5
std::ifstream usernames("users.txt");
std::ifstream passwords("pass.txt");
std::string username, password;
while(std::getline(usernames, username) && getline(passwords, password))
    std::cout << username << " password is " << password;
It iterates the username, so when i input John, it will display :

Finn password is password1


But it does not iterate the password.

Thinking of having a condition, but have zero idea so far
1
2
3
4
if(username == "John")
{
    std::cout << username << " password is " << password;
}


Obviously, "John" would be the variable that contains the user's input.
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
Obviously, "John" would be the variable that contains the user's input.

ummmm "John" is a literal.

zanmato: would you mind pasting your whole code?
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
Still need help D:
Topic archived. No new replies allowed.