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:

#include <cstdlib>
#include <iostream>
#include "functions.h"
using namespace std;

void checkDetails(string user, string pass);

string usernames[10];
string passwords[10];
int main(int argc, char *argv[])
{
   string user_username;
   string user_password;
   usernames[1] = 'Phillip';
   usernames[2] = 'Phillip';
   usernames[3] = 'Phillip';

   passwords[1] = 'pass';
   passwords[2] = 'pass2';
   passwords[3] = 'pass3';

  cout << "Please enter your username";
  cin >> user_username;
  
  cout << "Please enter your password:";
  cin >> user_password;

  checkDetails(user_username, user_password);

   system("PAUSE");
   return 0;
}

void checkDetails(string user, string pass)
{

   for(int i=0; (i < 10); i++)
   {
      if(user == usernames[i])
     {
        if(pass == passwords[i])
        {
          cout << "You're in the system";
        }else{
          cout << "You're not in the system";
       }else {
          cout << "Your pass was not recognised";
   }
}

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:

getUsers(usernames[i]);

Wouldn't work, would it?
Thanks for any help and advice!

Recommended Answers

All 2 Replies

What about adding a class. Starting with that is a great way to group data together.

class UserInfo
{
   private:
    std::string Name;
    std::string Pass;      // THIS IS NOT SECURE
    int age;
    // etc...                 
};

But you may want to just use a token index e.g.

class UserDetails
{
   std::string Name;
   int index; 
}

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:

UserDetails* UPtr= findUser(Name);
if (UPtr)
  UPtr->printUser();
// or:
DataBaseObj DB;
// stuff...
if (UPtr)
   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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.