Compare user input and data in file

File name: myfile.txt which contains a series of usernames and passwords for each username.

Data in text file:

username1 password1
username2 password2
username3 password3
username4 password4
username5 password5


The program should take the user input for username and compare it with all the usernames in the file.
If username is matched then display a msg that username already exist and exit the program.
If username does not match with the username in the file then the program ask for a password.

I am having difficulty solving this type of problem...
Show us what youve made and what youre having trouble with
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
	string username, password;
	char filename[] = "myfile.txt";

	ofstream Login(filename, ios::app);

	cout << "Enter username: ";
	getline(cin, username);
	/*
	 * while (username entered by user == username found in file){
	 *     cout << "Username already exist!! Enter username: ";
	 *     getline(cin, username);
	 * }
	 *
	 * */
	Login << username << " ";
	cout << "Enter password: ";
	getline(cin, password);
	Login << password << "\n";

	Login.close();
	return 0;
}

Here is my code. The one in the comment is my problem. I don't know how to loop into the file and compare the user input with each usernames in the file and display a message that the username already exist if it's matched and ask to choose another username.
Topic archived. No new replies allowed.