login check array problem

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 3862 Views - Last Post: 05 June 2012 - 02:41 AM Rate Topic: -----

#1 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

login check array problem

Posted 27 May 2012 - 02:30 PM

lets say I want to make a login system that checks students ID's and password. using an array

string S[i] //to check that user and password belong to each other like S[1] = U[1] && P[1]
string U[i]
string P[i]

for (int i, i<=20,i++)
{
S[i] = U[i] && P[i]
//S[1] = U[1] && P[1]
//S[1] =/ U[1] && P[2,3 etc] vise versa
cout<<"user: ";
cin>>U[i]>>endl;
cout<<"password: ";
cin>>P[i]>>endl;

if S[i] = U[i] && P[i] // I know this is %100 wrong I want to know if there is a method to do something similar
{
cout<<"welcome"<<S[i]<<" Have a nice day";

else 
{
cout<<" wrong password please try again";
}
}

}
}




Is This A Good Question/Topic? 0
  • +

Replies To: login check array problem

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: login check array problem

Posted 27 May 2012 - 02:44 PM

View Postbusta5000, on 27 May 2012 - 03:30 PM, said:

lets say I want to make a login system that checks students ID's and password. using an array


Okay: "You want to make a login system that checks students' IDs and password, using an array"

Is there a question in there somewhere?



See FAQ # 4. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated May 2012
Spoiler



This post has been edited by tlhIn`toq: 27 May 2012 - 02:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

Re: login check array problem

Posted 27 May 2012 - 04:56 PM

View PosttlhIn`toq, on 27 May 2012 - 02:44 PM, said:

View Postbusta5000, on 27 May 2012 - 03:30 PM, said:

lets say I want to make a login system that checks students ID's and password. using an array


Okay: "You want to make a login system that checks students' IDs and password, using an array"

Is there a question in there somewhere?



See FAQ # 4. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated May 2012
Spoiler



What I wanted is how to change or improve this code. read the code and tell me what is wrong and how to fix it or give me tutorials that explain how to do such things etc.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5343
  • View blog
  • Posts: 16,672
  • Joined: 25-December 09

Re: login check array problem

Posted 27 May 2012 - 06:10 PM

The first thing you can do to improve your code is to find and use and indentation style that you like. Next ask specific questions based on the code you supplied.

Quote

if S[i] = U[i] && P[i] // I know this is %100 wrong I want to know if there is a method to do something similar

Similar to what? What are you actually trying to accomplish?

Jim
Was This Post Helpful? 1
  • +
  • -

#5 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: login check array problem

Posted 28 May 2012 - 05:12 AM

You have so many problems that I cannot even list them all... But to mention a few, you are using strings without initialization, you have not used ";" after first three declarations, if the password is right you are printing S[i] instead of S, you should cin S and P instead of S[i] and P[i], the endl in cin is useless, on line 15 in if statement you are not using (), you are comparing the value to God knows what, mismatched brackets, lack of indentation. PHEW !! As far the if statement is concerned,
if S[i] = U[i] && P[i]
You will have to declare 2 strings, 1 for username and 1 for password. Then, you must compare them by "==" since "=" is assignment, not comparison. So, the final if will look something like :
if(User==U && Pass==P)

Notice how I removed the [i] from everything ?? Because I am directly comparing strings instead of the character at I-th position. User is a string containing user name, which can be declared by something like
string User="UserName"
Do something similar for password..
Was This Post Helpful? 1
  • +
  • -

#6 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

Re: login check array problem

Posted 29 May 2012 - 02:58 AM

View Postaresh, on 28 May 2012 - 05:12 AM, said:

You have so many problems that I cannot even list them all... But to mention a few, you are using strings without initialization, you have not used ";" after first three declarations, if the password is right you are printing S[i] instead of S, you should cin S and P instead of S[i] and P[i], the endl in cin is useless, on line 15 in if statement you are not using (), you are comparing the value to God knows what, mismatched brackets, lack of indentation. PHEW !! As far the if statement is concerned,
if S[i] = U[i] && P[i]
You will have to declare 2 strings, 1 for username and 1 for password. Then, you must compare them by "==" since "=" is assignment, not comparison. So, the final if will look something like :
if(User==U && Pass==P)

Notice how I removed the [i] from everything ?? Because I am directly comparing strings instead of the character at I-th position. User is a string containing user name, which can be declared by something like
string User="UserName"
Do something similar for password..

I apologise for my mistakes ignore the missing; the p[i] cout is a mistake it should be p you are right. I didn't state clearly what I wanted. What I want is to make sure that the user array is equal to the password array.
for example I created a username and password
string S[i];
string U[i];
string P[i];
for (int i=0, i<=20,i++)
{
cin<<u; //sam
cin<<p; //1234
}

//missing if statement to check
if (//I do not know what to put to make sure i=i U[2] & P[2])
{
cout<<"welcome"<<u;
else
{
cout<<"wrong password";
}
}


now automatically sam is saved in u[1] and 1234 is saved in p[1]. S is only used to make sure that the password belong to password
so if username = sam & password = 4321 then this is wrong because
U[1] = "sam" & P[1] = "1234"
S[1] = U[1] && P[1]
So S is only used to check if i=i e.x. i=1 S[1], U[1], P[1] if i=/i then its wrong
but i do not know how to implement it.
Was This Post Helpful? 0
  • +
  • -

#7 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: login check array problem

Posted 29 May 2012 - 05:09 AM

Half a second, is that an array of string ?? If you want 1 username password, what is the need of array of strings ?? Simply declare
string U,P;


And be sure to take care of case, ie, whether string is U or u, as in C++ both are different. I assuming you wrote the array of string part by mistake. So, simply writing
cin << U ;
cin << P ;


Will get the entire string for you. Your current code takes 40 strings (20 for username, 20 for password) and then checks the string. What you need to do is this :
string U,P; //This will declare two strings, U and P
cin << U ; //Take input from user
cin << P ;
if(U=="sam" && P=="1234") //You can directly compare strings
{
   cout << "Welcome " << U;
}
else
{
   cout << "Wrong username and/or password";
}


I believe you will be able to understand something from this.
Was This Post Helpful? 0
  • +
  • -

#8 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

Re: login check array problem

Posted 30 May 2012 - 03:02 AM

View Postaresh, on 29 May 2012 - 05:09 AM, said:

Half a second, is that an array of string ?? If you want 1 username password, what is the need of array of strings ?? Simply declare
string U,P;


And be sure to take care of case, ie, whether string is U or u, as in C++ both are different. I assuming you wrote the array of string part by mistake. So, simply writing
cin << U ;
cin << P ;


Will get the entire string for you. Your current code takes 40 strings (20 for username, 20 for password) and then checks the string. What you need to do is this :
string U,P; //This will declare two strings, U and P
cin << U ; //Take input from user
cin << P ;
if(U=="sam" && P=="1234") //You can directly compare strings
{
   cout << "Welcome " << U;
}
else
{
   cout << "Wrong username and/or password";
}


I believe you will be able to understand something from this.

I did not mean to check for 1 student only I gave an example of 1. As you said its 20 usernames and 20 passwords
I want to check if the username array is equal to the password array.
e.x.
U[0] = "admin"
P[0] = "admin"
U[1] = "sam"
P[1] = "1234"
.
.
.
U[19] = "james"
P[19] "9876"
___________________________
All of the above are cin except U[0] P[0] its reserved for admin. So I want to check if i=i so U[2]& P[2]. where i is the array number. So this is to check array numbers not array strings or comparisons. This is for 20 students not 1 student.
Was This Post Helpful? 0
  • +
  • -

#9 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: login check array problem

Posted 30 May 2012 - 03:17 AM

Could you please elaborate what you exactly mean by

Quote

i=i so U[2]& P[2]

And always remember, in programming, use == for comparison, not =.
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: login check array problem

Posted 30 May 2012 - 03:31 AM

Why the hell would you go the parallel array route instead of creating an array of structs or classes?

struct UserLogin
{
    std::string name;
    std::string password;
}

Was This Post Helpful? 1
  • +
  • -

#11 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

Re: login check array problem

Posted 30 May 2012 - 05:43 AM

View PostJackOfAllTrades, on 30 May 2012 - 03:31 AM, said:

Why the hell would you go the parallel array route instead of creating an array of structs or classes?

struct UserLogin
{
    std::string name;
    std::string password;
}

what I mean by i==i U[2] & P[2]
is that when it checks password and login
//student register
cin>>U; //david since U[0] & U[1] are already assigned by cin a new array is selected so david is U[3]
cin>>P; //6534 since P[0] & P[1] are already assigned by cin a new array is selected so david is P[3] 
//now i=3
//student login
cin>>U;
//if i that is in U does not equal i in  P then  wrong password
//i==i so if U[3] where i=3 then P must have the same array number that is 3 P[3]

//I have over 20 students I cannot make then register and login without saving then in an array.



Was This Post Helpful? 0
  • +
  • -

#12 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: login check array problem

Posted 30 May 2012 - 09:27 AM

What I understand is that you have a array of username and passwords, which already have some value like U[0]="admin",p[0]="admin" and so on. Then, the user will enter a username and password, and you have to check if it matches with one of the 20 entries. Please correct me if I am interpreting your question wrong. So, you have to make U[20] and P[20] for the username and passwords, and then two other strings user and pass to store user input. So, your code will be something like
string U[20],P[20]; //To store the username and password
U[0]="admin";//Initialize all the username and password
P[0]="admin";
U[1]="sam";
P[1]="1234";
//And so on...
cin << user ; //Take input from user
cin << pass ;
for(int i=0;i<20;i++)
{
   if(user==U[i] && pass==P[i]) //Comparing the strings, that is your i=i comparison
   {
      cout << "Welcome " << user ;
      break;
   }
}
else
{
   cout << "Wrong username and/or password";
}


Was This Post Helpful? 1
  • +
  • -

#13 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

Re: login check array problem

Posted 30 May 2012 - 12:29 PM

View Postaresh, on 30 May 2012 - 09:27 AM, said:

What I understand is that you have a array of username and passwords, which already have some value like U[0]="admin",p[0]="admin" and so on. Then, the user will enter a username and password, and you have to check if it matches with one of the 20 entries. Please correct me if I am interpreting your question wrong. So, you have to make U[20] and P[20] for the username and passwords, and then two other strings user and pass to store user input. So, your code will be something like
string U[20],P[20]; //To store the username and password
U[0]="admin";//Initialize all the username and password
P[0]="admin";
U[1]="sam";
P[1]="1234";
//And so on...
cin << user ; //Take input from user
cin << pass ;
for(int i=0;i<20;i++)
{
   if(user==U[i] && pass==P[i]) //Comparing the strings, that is your i=i comparison
   {
      cout << "Welcome " << user ;
      break;
   }
}
else
{
   cout << "Wrong username and/or password";
}


Thank you but I apologies for not explaining enough. I thought what I wanted was clear enough. Anyways what I want is to let students register as in create their own strings of usernames and password so the strings are not in the code. All the strings are cin only. They "REGISTER" after they register and try to login the system checks their array number U[0] P[0]... U[19]P[19] etc. So the array is empty except for U[0] & P[0].

U[1-19] & P[1-19] do not exist yet since the students did not register yet. I hope this is clear enough I have searched multiple ways to do this method. Tried many methods of comparing arrays but none are useful for a code with this specific objective.
Was This Post Helpful? 0
  • +
  • -

#14 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,258
  • Joined: 08-January 12

Re: login check array problem

Posted 30 May 2012 - 12:38 PM

Ohh... So you will have to create two parts of your code
1. Registering
2. Logging in
You can create functions for these two, or take input like press 1 to register, 2 to login. After that, depending on choice, do one the above two.
To create the first one, do something like
int i;
for(i=1;i<20;i++) //This will help us know the first empty string
{
   if(U[i].size()==0)
      break;
}
if(i==20)
{
   cout << "All entries filled" ;
   //Exit from this function or whatever
}
cin << user[i];
cin << pass[i];


This will help the student register.
The second one, I have already told you.

This post has been edited by aresh: 30 May 2012 - 12:38 PM

Was This Post Helpful? 1
  • +
  • -

#15 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5922
  • View blog
  • Posts: 20,246
  • Joined: 05-May 12

Re: login check array problem

Posted 30 May 2012 - 03:40 PM

And why limit yourself to just 20? Use a vector and it grow as needed. Since you are using string, then presumably you can also use vector.

Anyway, here is a quick and dirty example. Other than some of the lambda voodoo that happens in the GetLoginIndex() method, hopefully its easy to read and understand.
class LoginSystem
{
public:
    bool AddLogin(const string & username, const string & password)
    {
        if (GetLoginIndex(username) >= 0)
            return false;

        UserLogin login;
        login.Username = username;
        login.Password = password;
        Logins.push_back(login);
        return true;
    }
    
    bool CanLogin(const string & username, const string & password)
    {
        int index = GetLoginIndex(username);
        if (index < 0)
            return false;
        return password == Logins[index].Password;
    }

protected:
    struct UserLogin
    {
        string Username;
        string Password;
    };

    vector<UserLogin> Logins;

    int GetLoginIndex(const string & username)
    {
        auto found = find_if(Logins.begin(), Logins.end(),
                                [&] (const UserLogin & login) -> bool
                                {
                                    return username == login.Username; 
                                });
        return found < Logins.end() ? found - Logins.begin() : -1;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    LoginSystem loginSystem;
    if (!loginSystem.AddLogin("admin", "password"))
        cerr << "Could not create admin login" << endl;
    if (loginSystem.AddLogin("admin", "Peekaboo"))
        cerr << "Oops! Can create a duplicate login" << endl;
    if (!loginSystem.CanLogin("admin", "password"))
        cerr << "Could not login with correct password" << endl;
    if (loginSystem.CanLogin("admin", "Peekaboo"))
        cerr << "Oops! Can login with incorrect password" << endl;

    return 0;
}


Was This Post Helpful? 2
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3