我的条件程序无法通过if语句[保留]正确执行

问题描述 投票:0回答:1
#include <iostream>
#include <vector>
#include <string>
class Account;
std::vector<Account> Accounts;
//declared account class and also declared the vector in which i store each account object.



class Account
{
protected:
    std::string username = "hey";
    std::string password;
    int securityNumber = 0;
    bool Available = true;
    std::string theirUsernamechoice;
public:

    std::string Username()
    {
        return username;
    }

    void CreateAccount()
    {
    //asking for username input, iterate through each object, if there is a successful match, then set available = false.
        std::cout << "Please enter your desired username: ";
        std::cin >> theirUsernamechoice;

        for (Account object : Accounts)
        {
            std::cout << object.Username() << std::endl;
            if (object.Username() == theirUsernamechoice)
            {

                Available = false;
                std::cout << "Account creation failed. Username is not available." << std::endl;
                break;
            }
        }
        //it seems like the match between the input username and the values of the usernames within the object never actually executes, so the program keeps saying that the username is available.
        if (Available == true)
        {
            this->username = theirUsernamechoice;
            std::cout << "Please input your password" << std::endl;
        }

    }


};



int main()
{

    Account A, B, C, D, E, F;
    Accounts = { A,B,C,D,E,F };
    A.CreateAccount();
    B.CreateAccount();

}
c++ conditional-statements
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.