Alright, I have a code that now works.
But like always I have a couple problems I'm still seeking help with on it.
1. Every time I want to login I have to register a new account. And after I do that and login, If I try to login again it says "Invalid username or password." It's like the file is deleting any information that is sent to it.
So I'm trying to find a way to permanently store data once I have registered an account so that anytime I run the program I can always just login with an account I already made.
Here is my header code:
Code:#ifndef CHARACTERINFO_H_INCLUDED #define CHARACTERINFO_H_INCLUDED class Character { public: char name[16]; char pword[10]; void sendNAME(); void sendPWORD(); }; #endif // CHARACTERINFO_H_INCLUDED
Here is my source code:
Code:#include <iostream> #include <fstream> #include "CharacterInfo.h" using namespace std; void Character::sendNAME() { cout << "Enter your desired username(max 16 characters): "; cin >> name; cin.ignore(numeric_limits<streamsize>::max(), '\n'); }; void Character::sendPWORD() { cout << "Enter your password(max 10 characters): "; cin >> pword; cin.ignore(numeric_limits<streamsize>::max(), '\n'); }; int startMenu() { int option; cout << "Welcome to the login menu..." << endl; cout << "\tPress '1' to login." << endl; cout << "\tPress '2' to register." << endl; cout << "Your option: "; cin >> option; cin.ignore(numeric_limits<streamsize>::max(), '\n'); return option; }; int main() { ifstream readFile ("example.txt"); ofstream writeFile ("example.txt"); Character charInfo; do { int option = startMenu(); cout << endl; if(option == 1) { string username; string password; cout << "Enter Name: "; cin >> username; cout << "Enter Password: "; cin >> password; cout << endl; string temp; while(readFile>>temp) { if(temp == charInfo.name) { readFile>>temp; if(temp == charInfo.pword) { cout << "Welcome " << username << "!" <<endl; break; } } else { cout << "Invalid username or password." << endl; } } } cout << endl; if(option == 2) { charInfo.sendNAME(); cout << endl; charInfo.sendPWORD(); cout << endl; if (writeFile.is_open()) { writeFile << charInfo.name << ' ' << charInfo.pword << endl; writeFile << charInfo.pword; writeFile.close(); } else { cout << "Unable to open file"; } } }while(1); return 0; }


Reply With Quote
Bookmarks