Loop execution failed

1
// Example program
#include <iostream>
#include <string>

#define     ACCOUNT_MAX    3

using namespace std;

class User 
{
private:
    struct Accounts {string user, password, firstName;};
    struct Accounts accs[ACCOUNT_MAX];
    bool online;

    void setAccounts() 
    {   
        accs[0].user = "admin";
        accs[0].password = "adminpw";
        accs[0].firstName = "Administrator";
        accs[1].user = "user01";
        accs[1].password = "user01pw";
        accs[1].firstName = "User 01";
        accs[2].user = "user02";
        accs[2].password = "user02pw";
        accs[2].firstName = "User 02";
    }

public:
    int Access(string user, string password)
    {
        int count = 0;

        for (; count <= ACCOUNT_MAX; count++)
        {
            if (user.compare(this->accs[count].user) == 0 && password.compare(this->accs[count].password) == 0)  
            {
                this->online = true;
                break;
            }

            else
                this->online = false;
        }

        return this->online;
    }

    User() {}
    ~User() {}
};

int main()
{
    class User *managment = new User();
    string localUser, localPassword;

    cout << "A system [Version 2.0]\n" << endl;

    do {
        cout << "User: ";
        cin >> localUser;
        cout << "Password: ";
        cin >> localPassword;
    } while (managment->Access(localUser, localPassword) == false);



    return 0;  
}

Could someone tell me why the condition is not read? The program always returns 0.

    
asked by anonymous 13.09.2016 / 02:58

1 answer

1

The constructor of the User class does nothing, so the setAccounts method is never called ... this is the error.

Another strange (but not wrong) thing is the statement

class User *managment = new User();

The normal would be to simply declare

User managment;

More strange things: the method

int Access(string user, string password);

should be declared

bool Access(const string& user, const string& password);
The Accounts statement is also a bit strange, in C ++ it's not very common to have nested class declarations, so struct Accounts would probably look better if declared out of the User class before the User class. Also, it is not common to declare instances of a class by reusing the class and struct reserved words. These would be the normal statements:

Accounts accs[ACCOUNT_MAX];
User managment;

without the reserved words struct and class.

Oops, one more error:

for (; count <= ACCOUNT_MAX; count++)

should be

for (; count < ACCOUNT_MAX; count++)

Now one more strange thing: this line

   if (user.compare(this->accs[count].user) == 0 && password.compare(this->accs[count].password) == 0) 

would normally be written simply

   if (user == accs[count].user && password == accs[count].password) 

One more strange thing: these two lines

   int count = 0;
   for (; count <= ACCOUNT_MAX; count++)

would normally be written just like

   for (int count = 0; count < ACCOUNT_MAX; count++)

Maybe you have more "strange" things yet, if you seek ...

    
13.09.2016 / 03:06