Need Help using txt file.

i am trying to make a program that reads a username and password from a txt file
then makes you log in and checks to see if the username && password are correct. i am still confused on how to use txt files in C++. how to read from them the most is what i need. Thank you very much.

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
67
68
69
  #include "stdafx.h"
#include <iostream> // library that contains basic input output functions
#include <string> // library for c++ strings
#include <fstream> //helps open and close files

using namespace std;

int main()
{
	//Username and Password to validate credentials


	//strings in which user will enter username and password 
	ifstream inputStream; //ifstream means Input File stream
	string username, password;
	int passattempts = 0;
	inputStream.open("c:/filename.txt"); //if the file is in with the header files/folder of the initial program you do not need to make a pathname
	//Checking if user's entered credentials are equal to actual USERNAME and PASSWORD 
	string USERNAME, PASSWORD;
	inputStream >> USERNAME;
	inputStream >> PASSWORD;

	do{
		// Prompting user to input username
		cout << "Enter Username : ";
		cin >> username;

		//Checking if username length is less than 4 characters then display an error message
		if (username.length() < 4)
		{
			cout << "Username length must be atleast 4 characters long.";
		}
		else  //if username length is greater than 3
		{
			//promprting user for password
			cout << "Enter Password : ";
			cin >> password;
			//Checking if password length is less than 6 characters then display an error message
			if (password.length() < 6)
			{
				cout << "Password length must be atleast 6 characters long.";
			}
			if (username == USERNAME)//if password length is greater than 5
			{
				cout << "Checking Username.";
				system("pause");
			}
			else
			{
				//looking to check another username from the file.
			}
			{

				if (username == USERNAME && password == PASSWORD)
				{
					cout << "User credentials are correct!!!" << endl;
					break;
				}
				else
				{
					cout << "Invalid login details" << endl;
					++passattempts;
				}
			}
		}
	} while (passattempts != 3);
	system("pause");
	return (0);
}
Topic archived. No new replies allowed.