I'm lost..

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 148
Reputation: Phil++ is an unknown quantity at this point 
Solved Threads: 3
Phil++ Phil++ is offline Offline
Junior Poster

I'm lost..

 
0
  #1
Oct 17th, 2009
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:

  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:

  1. getUsers(usernames[i]);

Wouldn't work, would it?
Thanks for any help and advice!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
0
  #2
Oct 17th, 2009
What about adding a class. Starting with that is a great way to group data together.
  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.
  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:
  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.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
0
  #3
Oct 17th, 2009
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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC