944,184 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 275
  • C++ RSS
Oct 17th, 2009
0

I'm lost..

Expand Post »
Right, so I'm trying to create like a login system and my overall goal is to do it in classes but for the meantime, I won't. So this is my main file:

C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "functions.h"
  4. using namespace std;
  5.  
  6. void checkDetails(string user, string pass);
  7.  
  8. string usernames[10];
  9. string passwords[10];
  10. int main(int argc, char *argv[])
  11. {
  12. string user_username;
  13. string user_password;
  14. usernames[1] = 'Phillip';
  15. usernames[2] = 'Phillip';
  16. usernames[3] = 'Phillip';
  17.  
  18. passwords[1] = 'pass';
  19. passwords[2] = 'pass2';
  20. passwords[3] = 'pass3';
  21.  
  22. cout << "Please enter your username";
  23. cin >> user_username;
  24.  
  25. cout << "Please enter your password:";
  26. cin >> user_password;
  27.  
  28. checkDetails(user_username, user_password);
  29.  
  30. system("PAUSE");
  31. return 0;
  32. }
  33.  
  34. void checkDetails(string user, string pass)
  35. {
  36.  
  37. for(int i=0; (i < 10); i++)
  38. {
  39. if(user == usernames[i])
  40. {
  41. if(pass == passwords[i])
  42. {
  43. cout << "You're in the system";
  44. }else{
  45. cout << "You're not in the system";
  46. }else {
  47. cout << "Your pass was not recognised";
  48. }
  49. }

So, basically the checkDetails is going to check to see if the username and password match. But if they do, it runs another function "getUser" which displays all the users information but how do I do it because it's stored in a array? For example:

C++ Syntax (Toggle Plain Text)
  1. getUsers(usernames[i]);

Wouldn't work, would it?
Thanks for any help and advice!
Similar Threads
Reputation Points: 41
Solved Threads: 7
Posting Whiz in Training
Phil++ is offline Offline
233 posts
since Jan 2009
Oct 17th, 2009
0
Re: I'm lost..
What about adding a class. Starting with that is a great way to group data together.
c++ Syntax (Toggle Plain Text)
  1. class UserInfo
  2. {
  3. private:
  4. std::string Name;
  5. std::string Pass; // THIS IS NOT SECURE
  6. int age;
  7. // etc...
  8. };

But you may want to just use a token index e.g.
c++ Syntax (Toggle Plain Text)
  1. class UserDetails
  2. {
  3. std::string Name;
  4. int index;
  5. }

and then use the index number (e.g. your i as a way of looking up a data-entry elsewhere.

This way you can either do something like this:
c++ Syntax (Toggle Plain Text)
  1. UserDetails* UPtr= findUser(Name);
  2. if (UPtr)
  3. UPtr->printUser();
  4. // or:
  5. DataBaseObj DB;
  6. // stuff...
  7. if (UPtr)
  8. DB->printUser(UPtr->getIndex());
Note: You could sort your list of users and then when you have lots you don't need to loop through all of them.

Also: your current code prints a lot of "your pass was not regonised"
That needs to be after the loop if you don't find a user. So have another look at your if/else construction.

Think about using std::map<std::string,userClass> or std::vector<userClass> or something else other than a simple array. Since you will want to add new users later.

Don't use using namespace std; it simple defeats the objective of namespace std: to NOT pollute your global namespace.
Reputation Points: 749
Solved Threads: 135
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008
Oct 17th, 2009
0
Re: I'm lost..
You could pass the username and password arrays (or the single array of user objects when you make a class) to the checkDetails( ) function. That way you don't have the arrays in global space (something we want to avoid) and the function will not be tied to any specific array names.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Callback issues
Next Thread in C++ Forum Timeline: Can a function gives back function without pointers?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC