NOTE: If you could help me out with just one of these, then that would be greatly appreciated!
NOTE: Code is at bottom!
Goal:
Create a user account and password for game
Problems:
(1) File shows the username/password I created, as well as a bunch of weird characters.
(2) Each record (username and password) is appended to the end of line 1, versus to a new line each time.
(3) File search algorithm(s) that I have tried do not work correctly
-------------------------------------------------------------------------------
(1) I figure that my file shows the following:
abc IIIIIIIIIIIIIIIIdef IIIIIIIIIIIIIIII
Because I specified the array as a certain amount of characters, and the user inputted less than that max character limit.
I want to either (A) Delete these alien character from my file, or (B) Know that they will not affect an algorithm that searches the file for the username string.
-------------------------------------------------------------------------------
(2) Each new username simply just goes to the end of the first line.
I want each new username created to be appended to a new line. Can anyone give me insight into this?
-------------------------------------------------------------------------------
(3) I tried a couple of search algorithms to search my file for a username, and tell the user that they entered a username is already in use, but it hasn't worked out so far.
Can anyone suggest an algorithm that can search and/or replace a string in a file that might work with my code?
Pseudo code:
Get input (username)
If that username is found Then
Keep prompting for input until it does not match
Note: A problem that can occur though is that each time the user enters a new username, then I want to search the whole file to see if it is found ANYWHERE, not just where the current read position is and on forward...
------------------------------------------------------------------------------
Here is the relevant code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
const int USERNAME_SIZE = 20, PASSWORD_SIZE = 20;
struct UserAccount
{
char username[USERNAME_SIZE];
char password[PASSWORD_SIZE];
};
void create_account(UserAccount &u)
{
string s;
fstream accounts("user_accounts.txt",ios::app|ios::binary);
cout << "Create your username:\n";
cin >> u.username;
cout << "\n\nCreate your password:\n";
cin >> u.password;
accounts.write(reinterpret_cast<char *>(&u),sizeof(u));
accounts.close();
}
|