Hello DaniWeb,

This is my first real assignment and am having problem with it. The program must accept a userID and pass from user. The program must also read the userID and pass from file and if it matches the one on file, the program must display success message.

The data file consists of this: "user@name.com pass" sans quotes.

So far, I know I have to take into account the white space in the middle and need two strings for it. I've gone this far but no clue how to proceed.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main ()
{
// Reading user and pass from data file
ifstream datafile("lab1.data") // name of data file
dataFIle >> str; // I have no idea how to take two strings from the file here with the white space char in // the middle

Help please!

The ifstream should automatically parse on white space. That is:

dataFIle >> user >> password;

should do the trick.

Many thanks David, here is what I came up with after an hour of head scratching and 2 cans of coca cola.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main (int argc, char * const argv[]) // I'm coding in xcode
{
	int user;
	int password;

    ifstream dataFile("lab1.data");
	dataFile >> user >> password;
	dataFile.close();
	
	int user1;
	int password1;
	cout << "Please enter your username followed by a space then your password" << endl;
	cin >> user1 >> password1;
	
	if (user1 == user) // How can I also make it check for pass? If user passes but pass fails how can I let it go to the else statement?
		cout << "Success, you are signed in!" << endl;
	else 
		cout << "Username and/or password is incorrect. Please try again.";
	

    return 0;
}

The program compiles w/o errors but when I run it, whenever I type in anything, it executes the IF statement. Thanks in advance.

Why are you reading them as ints? You should change:

int user;
int password;

to

std::string user;
std::string password;

and

if (user1 == user)

to

if (user1.compare(user) == 0)

Thanks David! I needed to set two conditionals in 1 IF statement so here's the whole thing I used.

// This program will accept user ID and password from user and match 
// with one on file lab1.data If pass and user match then program
// will display a success message.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main ()
{
string user;
string password;
	// Read username and pass from data file
	ifstream dataFile("lab1.data");
	dataFile >> user >> password; //hopefully this will parse white  
	dataFile.close();

	// Get username and pass from user
string user1;	
string password1;
	cout << "Please enter your username" << endl;
	getline (cin, user1);	
	cout << "Please enter your password" << "\n";
	getline (cin, password1);

	// Now we compare user/password with one from file
	if ((user1 == user) && (password1 == password))

	cout << "Success, you are signed in!" << endl;

	else
	cout << "Username and/or password incorrect. Try again." << 
"\n";

return 0;
}

Program runs flawlessly. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.