Failure to run loop

Asked

Viewed 40 times

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 isn’t read? The program always returns 0.

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 little strange, in C++ it is not very common to have nested class statements, so the Accounts struct would probably look better if stated outside the User class, before the User class. Furthermore, it is not common to declare instances of a class by reusing the reserved words class and struct. These would be the normal statements:

Accounts accs[ACCOUNT_MAX];
User managment;

without the reserved words struct and class.

Oops, one more mistake:

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) 

normally would 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 there’s more "weird" stuff left, if you look around...

  • JDHSAUESA thanks for the delicacy in quoting strange things, never felt so well received lmao. Anyway, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.