Advertisement Jump to content
  • Advertisement

Boooke

Member
  • Content Count

    46
  • Joined

  • Last visited

Community Reputation

104 Neutral

About Boooke

  • Rank
    Member
  1. I started with C++, still a beginner, and I only recently began learning it "again". My code is messy, and lots of times things could've been done more effeciently from what I have done. With Python (Both the "new" and "old" standard), which I started reading about after discovering C++, was much more forgiving to me, and I did not have as much trouble solving problems in Python. Python helped me grasp subjects that seemed vague to me in C++. But I really enjoy C++, so I chose to continue studying it, and learn about programming concepts through C++ instead of Python, which was much fun as well. Well, actually, I did mess with a bit of Visual BASIC before I started using C++, but I never really programmed anything in VB - I was a little kid amazed of the drag and drop feature.
  2. True. Copying is actually allowed. Anyone else but me and two friends do this, since our teacher has been unable to teach anyone anything, and we are the only ones with experience from before. But I do prefer making it myself, yes, and I will. Your code has certainly helped me understand, and it inspired me to include some functions for the extraction of data. Thank you very much!
  3. Hey again. I really like your code, and I think I understand it. However, there are some few alterations I have done, because my project requires me to do do. I need to have the users vector inside of a class. Probably, this class should contain your functions aswell. The const string filename, you use to contain the name of the text file to be read, will complain. I klugded this by just writing somthing like ifstream input("name_of_file.txt"). I implemented your code into mine at first, I will try just running yours alone later, when I'm near the code again. In fact, I would actually prefer rewriting my code based on yours, if you don't mind.
  4. Thank you for the reply. I will read through it when schools done for today. I understand the fool-proofnes of your code, and that was what I was aiming for first when I started programming my project, but i am running out of time, and functionality equals fulfilling the requirements of my project. I realized one of the errors, when the code overwrites the second user when registrating, might be that in my code the elements of the vector (If I read userlist.txt on startup) will be removed since I used push_back inside of the while loop. I did not realize that, but then the problem would also be preserving the elements of the vector somehow. But i already thought it would be preserved if I created the class of the vector outside of a function, which I did (last lines of dbmani.h)?
  5. Oops, sorry for double post, but I seem to have forgotten to attach the source. It was created as a Code::Blocks project. I doesn't seem like you can attach a file when editting an older post. Regards, Boooke EDIT: I'm sure its an error (One of them at least) in how the program reads the .txt into the program. If I create a new database by opening the program without any userlist.txt, it will work fine until I will create a new account. Or, if I close the program and then open it again. This will make the program read the userlist.txt instead of creating it anew if the userlist.txt exist. This will bug out, when I log in.
  6. Hello, I have a problem making a function work correctly for my school project. At times, I feel the output I receive is very random, and most of the time it should not act like it does. I just don't see why it won't work, and now I have grown so frustrated, that I fear for my computer screen if I continue trying to solve this. So I would really like anyone here to help me see what the problem is. I attached all of the code, which has been developed using Codeblocks with GNU, but I'll try also posting the important things here. I need every member of a vector object printed out in a text file like this in userlist.txt: ID:1 created:Sun May 13 16:04:31 2012 name:admin password:Admin1 security level:2 status:active Now, this particular user is created in main.cpp, #include "preproc.h" #include "dbmani.h" #include "session.h" int main() { system("mkdir messages"); ifstream inFile; inFile.open("userlist.txt"); if(inFile.good()) { // ADD DATE OF MODIFICATION cout << "USERLIST FOUND, READING USERS.\n\n"; userstats tempBuffer; int userCount = -1; int overCount = 0; string buffer; while(getline(inFile, buffer)) { if (0 == buffer.find("ID:")) { userCount++; if (userCount > overCount) { userbase.users.push_back(tempBuffer); overCount++; } buffer.erase(0, 3); tempBuffer.ID = buffer; } else if (0 == buffer.find("created:")) { buffer.erase(0, 8); tempBuffer.date = buffer; } else if (0 == buffer.find("name:")) { buffer.erase(0, 5); tempBuffer.name = buffer; } else if (0 == buffer.find("password:")) { buffer.erase(0, 9); tempBuffer.password = buffer; } else if (0 == buffer.find("status:")) { buffer.erase(0,7); if (buffer == "active") tempBuffer.active = true; else if (buffer == "inactive") tempBuffer.active = false; } } if (userCount == 0) { userbase.users.push_back(tempBuffer); } inFile.close(); } else { cout << "NO USERS FOUND, CREATING NEW LIST.\n"; inFile.close(); // Determine current date time_t rawtime; struct tm * timeinfo; time (&rawtime ); timeinfo = localtime ( &rawtime ); string cdate = asctime(timeinfo); userstats userBuffer = {"1", cdate, "admin", "Admin1", admin, 1}; userbase.users.push_back(userBuffer); ofstream outFile("userlist.txt"); outFile << "created: " << cdate; outFile << "mod_date: " << cdate << "\n\n"; outFile << "ID:" << userbase.users[0].ID << "\n"; outFile << "created:" << userbase.users[0].date; outFile << "name:" << userbase.users[0].name << "\n"; outFile << "password:"<< userbase.users[0].password << "\n"; outFile << "security level:" << userbase.users[0].secLev << "\n"; outFile << "status:active\n\n"; outFile.close(); } // CHECK DIR char ch; cout << "Choices are listed below\n"; cout << "1) login\t2) Register an Account\n" << "q) Quit.\n"; cout << "Select option: "; bool breakFlag = false; while (breakFlag == false && cin.get(ch)) { cin.ignore(); // Could use getch to eliminate errors instead. switch (ch) { case '1': int logID; logID = userbase.login(); if (logID) { logID -= 1; loggedin(logID); } cout << "\nChoices are listed below\n"; cout << "1) login\t2) Register an Account\n" << "q) Quit.\n"; cout << "Select option: "; break; case '2': userbase.regAcc(); cout << "\nChoices are listed below\n"; cout << "1) login\t2) Register an Account\n" << "q) Quit.\n"; cout << "Select option: "; break; case 'q': breakFlag = true; cout << "\nEXITING PROGRAM\n"; break; default : cout << "Wrong input.\n\n"; } } return 0; } the "database" vector is inside a class, Userbase::userbase in dbmani.h, #ifndef DBMANI_H_INCLUDED #define DBMANI_H_INCLUDED enum securityLevel {user = 0, moderator, admin}; struct userstats { string ID; string date; string name; string password; // Password minimum: One capital letter and number included securityLevel secLev; // 0/normal user (Costumer, can only interact through messages) // 1/moderator (Can delete level 0 users and see list of users) // 2/admin (Same as moderator, but right to delete moderators aswell, defines root password) - INERASABLE bool active; }; class Userbase { private: string rootPass; public: Userbase(); void setPas(); void regAcc(); int login(); void writedb(); // void resetdb(string uP); vector<userstats> users; }; Userbase::Userbase() { rootPass = "Admin1"; } // Objective: Change rootpassword. Possibly connect admin/root passwords - // But having constructor-problems. //STATUS: Functional void Userbase::setPas() { cout << "Enter current root password: "; string pass; getline(cin, pass); if (pass == Userbase::rootPass) { cout << "Select new rootPass: "; getline(cin, pass); Userbase::rootPass = pass; cout << "Password is now \"" << Userbase::rootPass << "\" until system exit.\n\n"; } else cout << "Password incorrect."; } // Objective: To add member to vector database and call writedb // Status: Functional enum RegFlags { notUnique, tooShort, tooLong, inappropriate }; void Userbase::regAcc() { cout << "\nCREATING NEW USER\n"; string username, password; cout << "Enter desired username: "; getline(cin, username); cout << "Enter desired password: "; getline(cin,password); const unsigned int dbsize = Userbase::users.size(); // Do background check to make sure username isn't taken vector<RegFlags> regFlags; for (unsigned int i = 0; i < dbsize; i++) { if (username == Userbase::users.name) regFlags.push_back(notUnique); } if (password.size() < 6) regFlags.push_back(tooShort); if (password.size() > 15) regFlags.push_back(tooLong); if (regFlags.size() > 0) { cout << "\tREGISTRATION ERRORS: \n"; for (unsigned int j = 0; j < regFlags.size(); j++) { switch (regFlags[j]) { case notUnique : cout << "\t" << j + 1 << ". Your desired username, " << username << ", has already been taken\n"; continue; case tooShort : cout << "\t" << j + 1 << ". Your password is not long enough (six to 15 characters)\n"; break; case tooLong : cout << "\t" << j + 1 << ". Your password is too long (six to 15 characters\n"; break; case inappropriate: cout << "\t" << j + 1 << ". Your password is inappropiate.\n"; break; } } cout << "\nREGISTRATION FAILED\n"; } else { // Conversion of string value to integer value int i_regID = dbsize + 1; string str_regID; std::stringstream out; out << i_regID; str_regID = out.str(); cout << str_regID; // Date of creation time_t rawtime; struct tm * timeinfo; time (&rawtime ); timeinfo = localtime ( &rawtime ); string cdate = asctime(timeinfo); userstats regBuffer = {str_regID, cdate, username, password, user,1}; Userbase::users.push_back(regBuffer); Userbase::writedb(); cout << "\nREGISTRATION COMPLETE\n"; } } // Objective: Required login to view functions // Status: Functional int Userbase::login() { cout << "\nLOGGING IN\n"; string username, password; cout << "\tEnter username: "; getline(cin, username); cout << "\tEnter password: "; getline(cin, password); int loggedIn = 0; // 0 equals not logged in. const int dbsize = Userbase::users.size(); for (int i = 0; i < dbsize; i++) { if (username == Userbase::users.name) { if (password == Userbase::users.password && Userbase::users.active == 1) { loggedIn = i+1; } } } if (loggedIn > 0 ) { cout << "\n\nLOGIN SUCCESFUL\n"; cout << "Logged in as " << Userbase::users[loggedIn-1].name << ".\n"; return loggedIn; } else { cout << "Username/password incorrect.\n"; return loggedIn; } } // Objective: Write vector database to .txt database // Status: Functional void Userbase::writedb() { unsigned int dbsize = Userbase::users.size(); ifstream inFile("userlist.txt"); string cdate_orig; getline(inFile, cdate_orig); cdate_orig.erase(0,9); inFile.close(); ofstream outFile("userlist.txt", std::ios::trunc); time_t rawtime; struct tm * timeinfo; time (&rawtime ); timeinfo = localtime ( &rawtime ); string cdate = asctime(timeinfo); outFile << "created: " << cdate_orig << "\n"; outFile << "mod_date: " << cdate << "\n\n"; for (unsigned int i = 0; i < dbsize; i++) { time (&rawtime ); timeinfo = localtime ( &rawtime ); string cdate = asctime(timeinfo); outFile << "ID:" << Userbase::users.ID << "\n"; outFile << "created:" << Userbase::users.date; outFile << "name:" << Userbase::users.name << "\n"; outFile << "password:"<< Userbase::users.password << "\n"; if (Userbase::users.secLev == user) outFile << "security level:0" << "\n"; else if (Userbase::users.secLev == moderator) outFile << "security level:1" << "\n"; else if (Userbase::users.secLev == admin) outFile << "security level:2" << "\n"; if (Userbase::users.active == true) { outFile << "status:active\n\n"; } else if (Userbase::users.active == false) { outFile << "status:inactive\n\n"; } } outFile.close(); } Userbase userbase; #endif // DBMANI_H_INCLUDED I don't think session.h is needed, but it also contains some errors, which I think is attributed to secLev. #ifndef SESSION_H_INCLUDED #define SESSION_H_INCLUDED void listUsers(int logID); void loggedin(int logID) { char ch; bool breakFlag = false; if (userbase.users[logID].secLev == user) { cout << "Member choices are listed below\n"; cout << "1) Check messages\t2) Write a message" << "\n3) See users\nq) Log out.\n"; cout << "Choice: "; } else if (userbase.users[logID].secLev == moderator || userbase.users[logID].secLev == admin) { cout << "Member choices are listed below\n"; cout << "1) Check messages\t2) Write a message" << "\n3) See users\nq) Log out.\n"; cout << "Choice: "; } while (breakFlag == false && cin.get(ch)) { cin.ignore(); // Could use getch to eliminate errors instead. if (userbase.users[logID].secLev == user) { switch (ch) { case '1': break; case '2': break; case '3': listUsers(logID); break; case 'q': breakFlag = true; break; default : cout << "Wrong input.\n\n"; } cout << "\n\nMember choices are listed below\n"; cout << "1) Check messages\t2) Write a message" << "\n3) See users\nq) Log out.\n"; cout << "Choice: "; } else if (userbase.users[logID].secLev == moderator || userbase.users[logID].secLev == admin) { switch (ch) { case '1': break; case '2': break; case '3': listUsers(logID); break; case 'q': breakFlag = true; break; default : cout << "Wrong input.\n\n"; } cout << "\n\nMember choices are listed below\n"; cout << "1) Check messages\t2) Write a message" << "\n3) See users\nq) Log out.\n"; cout << "Choice: "; } cout << "Select option: "; } cout << "\n\nLOGGING OUT\n\n"; } void listUsers(int logID) { if (userbase.users[logID].secLev == user) { cout << "NAME\t\tRIGHTS\n"; for (int i = 0; i < userbase.users.size(); i++) { cout << userbase.users.name << "\t\t"; if (userbase.users.secLev == user) cout << "User\n"; else if(userbase.users.secLev == moderator) cout << "Moderator\n"; else if(userbase.users.secLev == admin) cout << "Admin\n"; } } else if (userbase.users[logID].secLev == moderator || userbase.users[logID].secLev == admin) { cout << "ID\tNAME\t\tRIGHTS\n"; for (int i = 0; i < userbase.users.size(); i++) { cout << userbase.users.ID << "\t"; cout << userbase.users.name << "\t\t"; if (userbase.users.secLev == user) cout << "User\n"; else if(userbase.users.secLev == moderator) cout << "Moderator\n"; else if(userbase.users.secLev == admin) cout << "Admin\n"; } } } #endif // SESSION_H_INCLUDED Now, the ERROR is, that when I choose to register a new person, it will rewrite the database completely wrong, and it will look like this, if I register user "asdf", ID:1 created:Sun May 13 16:10:26 2012name:admin password:Admin1 status:active ID:2 created:Sun May 13 16:16:29 2012 name:asdf password:asdfasdf security level:0 status:active To make it even worse, if I register a third account, it will replace the second user, asdf, when it should in fact make a third account. It will also bug out, when I check if the desired username is unique, when creating a new account, and not find if the name has already taken. For instance, my program created ID:1 user, as always. Then I copied ID:1 user manually and replaced "ID:1" with "ID:2" and "name:admin" with "name:admina", like this, created: Sun May 13 16:22:44 2012 mod_date: Sun May 13 16:22:44 2012 ID:1 created:Sun May 13 16:22:44 2012 name:admin password:Admin1 security level:2 status:active ID:2 created:Sun May 13 16:22:44 2012 name:admina password:Admin1 security level:2 status:active If I try to create another "admin", it will find out that the username is not unique, if I try to create an account with the name "admina", it will not find "admina" among the users, and therfore it will complete the registration. The new userlist.txt will then look like this, created: Sun May 13 16:22:44 2012 mod_date: Sun May 13 16:27:35 2012 ID:1 created:Sun May 13 16:22:44 2012name:admin password:Admin1 status:active ID:2 created:Sun May 13 16:27:35 2012 name:admina password:asdfasdf security level:0 status:active I don't think the regAcc() will read/write correct when I use writedb(), and errors only happen when I register a new user. Maybe some kind of newline bugs the reading? Preproc.h looks like this, if curious, #ifndef PREPROC_H_INCLUDED #define PREPROC_H_INCLUDED #include <iostream> #include <string> #include <vector> #include <fstream> #include <time.h> #include <cstdlib> #include <sstream> using std::cout; using std::cin; using std::ifstream; using std::ofstream; using std::vector; using std::string; #endif // PREPROC_H_INCLUDED By the way, the "status:Functional" may not be true if you see it in a comment above a function. I would be very grateful, and give out many internets, if anyone could point out the possible faults, since I'm relatively brain dead at the moment, and I can't really understand it. Regards, Boooke
  7. Boooke

    Switches

    I didn't think that switches would accept strings? Or maybe it changes if the string only consist of one element?
  8. Boooke

    Switches

    That was it, thank you! I put a cin.ignore() call just after the cin.get(). I also thought of just ruling out this factor by using getch(), but it seems to be deprecated.
  9. Boooke

    Switches

    Hello, I have a minor problem. I really do not understand why my switch-statement is failing. I made a quick switch-statement as following, char ch; while (cin.get(ch) && ch != 'q') { switch (ch) { case '1': cout<< "Hej"; break; case '2': break; case 'q': break; default : cout << "Wrong input.\n\n"; } cout << "Choice: "; } (The editor has messed a bit with the indenting) Anything except case 'q' turns to default case AND it includes the case ouput aswell. For instance, If I enter '1', it will output "HejChoice: Wrong input." (Choice and "Wrong input" is in the wrong order aswell?). This code only works in case 'q', which do not go through the default case - Which is quite weird, because it did not work before when I chose to move the cin.get(ch) into the while-loop and only place a "1" as the condition of the loop. Then it would return "Wrong input" when I pressed 'q'. In fact, that means case '2' do not work, since break should avoid the default case, but it still outputs "Choice: Wrong input." I hope someone sees my code flaws, because its quite weird to me. I have used switch-statements many times, and now it seems like I forgot how to use them all in a sudden? I tried googling source-codes, too, but I could not differ my switch from other peoples' switches. Regards, Boooke
  10. Boooke

    Structure Vector in class

    Ah, I see. So everytime I want to create a new element in the vector I will have to copy a whole structure object to the vector? I wrote something like this, ... else { cout << "NO USERS FOUND, CREATING NEW LIST.\n"; inFile.close(); userstats buffer = {"d1", "admin", "Admin1", admin}; userbase.users.push_back(buffer); ofstream outFile("userlist.txt"); time_t current = time(0); outFile << "created: " << current << "\n"; outFile << "mod_date: " << current << "\n\n"; outFile.close(); } ... Do you know the reason for this? A security measure for the correct data to be parsed, maybe? And thank you both for the solutions.
  11. Boooke

    Structure Vector in class

    ... Whops! Thanks for the heads up. but the problem seem to persist with the same error except for the parameter in the error "int" changing to "const char[3]".
  12. Boooke

    Structure Vector in class

    Hey, thank you, and sorry for the late answer. Yes, I know, a mess. I did use enums before, but I thought i'd optimize everything when it was all clear and functional. I was thinking, if it is allowed and somewhat accepted to make checks inside the class' constructor even before creating an class object, like this: Userbase::Userbase() { ifstream inFile("userlist.txt"); if (inFile.good()) { // Read password from first user (admin) in .txt } else rootPass = "Admin1"; inFile.close(); } Also, I seem not to be able to use push_back() with this, int main() { Userbase userbase; ifstream inFile; inFile.open("userlist.txt"); if(inFile.good()) { // ADD DATE OF MODIFICATION cout << "USERLIST FOUND, READING USERS.\n"; inFile.close(); } else { cout << "NO USERS FOUND, CREATING NEW LIST.\n"; inFile.close(); userbase.users.push_back(1, "admin", "Admin1", admin); ofstream outFile("userlist.txt"); time_t current = time(0); outFile << "created: " << current << "\n"; outFile << "mod_date: " << current << "\n\n"; outFile.close(); } return 0; } It gives me the error at line 78 (Which is the push_back() command), error: no matching function for call to 'std::vector<userstats, std::allocator<userstats> >::push_back(int, const char [6], const char [7], securityLevel)'| The vector structure looks like this, struct userstats { string ID; string name; string password; securityLevel secLev; // enum securityLevel {user, moderator, admin}; }; Btw, all the functions I have written (SetPas(), resetdb()) in the class are ideas that I have written down to remember and fully create when I have sorted this vector thing out. I have not fully written them.
  13. Hello, I am trying to make a class work in C++. This is what I have written so far, #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; struct userstats { string ID; int secLev; // 0, normal user (Costumer, can only interact through messages) // 1 moderator (Can delete level 0 users and see list of users) // 2 admin (Same as moderator, but right to delete moderators aswell, defines root password) - INERASABLE string name; string password; }; class userbase { private: string password; public: userbase(); static vector<userstats> users; void setPas(); // void resetdb(string uP); }; userbase::userbase() { password = "default"; } void userbase::setPas() { cout << "Enter current admin/root password: "; string pass; getline(pass); if (pass == userbase::password) { password = pass; userbase::users[1].password = pass; } else cout << "Password incorrect."; } int main() { userbase current; ifstream inFile; inFile.open("userlist.txt"); if(!inFile.good()) { current::users.push_back(); ofstream outFile("userlist.txt"); outFile << current::users(1).ID; } return 0; } This gives me a couple of errors when I try to build it, ||=== brugersystem v2, Debug ===| Line 36 |error: no matching function for call to 'getline(std::string&)'| Line 59 |error: 'current' is not a class or namespace| Line 61 |error: 'current' is not a class or namespace| ||=== Build finished: 3 errors, 0 warnings ===| I am not sure with any of them. The line 59 and 61 errors are given when I want to create a database with a "default" admin member, Line 36 I try to recieve line input from a user. This whole program is to be a sort of vague usersystem. It will check whether the .txt file containing information exists. If not, it should create the database with an default admin account. Regards, Boooke
  14. Boooke

    Making a staic variable dynamic?

    Hello, and sorry for the later answer. I am currently trying to replace the array with vectors, and I have created a vector inside of a class to contain the structure types. This class should contain all the functions required to use the structure. I am not really sure how to use the structure vector yet, and how to access members, but I am trying to read through different references to understand it better. Thank you for the replies. I think I can work something out now.
  15. Boooke

    Making a staic variable dynamic?

    Well, I can and try explain it. The structure will need to contain all sorts of user information there is, either created inside that loop (If "userlist.txt" do no exist)or read from a file (If "userlist.txt" exists). This data will, probably not be used for anything useful, but it is a requirement of this assigment I have. I will probably only use the "ID" and "name", which are string variables inside of the structure to sort the data and read messages from- and to users. Users will be deleted from time to time, and it should be resettable. I think it boils down to adding, reading, and deleting elements of the vector structure, and sometimes removing it completely. I haven't written much of this code yet. I will look into vectors, and replace the array.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!