Creating a login if data does not exist.
Jan 16, 2013 at 10:54am UTC
Hi there. Being as this is my first post, I've tried my best to sort this problem through many books, Google searches and topics on file I/O reading and writing before posting - to no avail.
I've created a file which stores usernames & passwords and all is fine, but when I try to add the condition that returns a null value, (EG There is no existing data that matches in the file), I get one of two errors: A. Expected primary expression before ')' token. OR B. Username will always return true.
The piece code in question is in bold. Any help would be appreciated. Thank you!
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 <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string username;
string password;
char response = ('y' || 'n' );
cout << "THE DESCENDANT\n" ;
cout << "Welcome to the Game!\n\n\n" ;
cout << "Please enter your username: " ;
cin >> username;
cout << "Please enter your password: " ;
cin >> password;
ifstream inuserdata("USER_ID.txt" );
if (!inuserdata) {
cout << "Cannot open file.\n" ;
return 1;
}
inuserdata >> username;
inuserdata >> password;
inuserdata.close();
if (username = !)
{
cout << "No previous game data was detected. Would you like to start a new game? (Y/N)" ;
cin >> response;
if (response == 'y' )
{
cout << "Please enter a username: " ;
cin >> username;
cout << "Please enter a password: " ;
cin >> password;
ofstream outuserdata("USER_ID.txt" , ios::app);
if (!outuserdata) {
cout << "Cannot open file.\n" ;
return 1;
}
outuserdata << username;
outuserdata << "\n" ;
outuserdata << password;
outuserdata << "\n\n" ;
outuserdata.close();
}
}
else
{
cout << "Thanks for playing. Good bye.\n" ;
}
system("PAUSE" );
return 0;
}
Jan 16, 2013 at 11:10am UTC
As I'm not a master in C++ programming, maybe i shouldn't take voice but I think that you have to compare username with something.
If you want to chech whether this user has played this game you should create a variable or list containing previous username(s).
It should look like this:
1 2 3 4 5 6
string last_username //or list of usernames here
...
if (username != last_username) //or check usernames list items here
...
Hope it helps...
Last edited on Jan 16, 2013 at 11:11am UTC
Topic archived. No new replies allowed.