在两个循环中检查字符串中的空格

时间:2016-08-27 17:42:45

标签: c++

在创建用户注册表的过程中,我看到一个带有相同条件的double do {} while循环。条件是如果在字符串中检测到空格,则读数将不可用

void createAccount() 
{
    unsigned short int i = 0;
    bool space = false;

    cin.ignore();

    cout << "FIRST NAME: ";
    getline(cin, fullName[0]);

    do {
        cout << "MIDDLE NAME: ";
        getline(cin, fullName[1]);

        for (i = 0; i < fullName[1].size(); i++) 
        {
            if (fullName[i][1] == 32) {
                space = true;
                break;
            }

            else {
                space = false;
                break;
            }
        }   
    } while(space);

    /*Reset values for the same loop again (I would not like to write 2 times all of this)
    i is reseted at loop-for, which helps.*/
    space = false;

    do {
        cout << "MIDDLE NAME: ";
        getline(cin, fullName[1]);

        for (i = 0; i < fullName[1].size(); i++) 
        {
            if (fullName[i][1] == 32) {
                space = true;
                break;
            }

            else {
                space = false;
                break;
            }
        }   
    } while(space);

    fullName[3] = fullName[0] + string(" ") + fullName[1] + string(" ") + fullName[2];
}

我真的不知道怎么能把它放在同一个循环中。我心碎了。

@edit:我是罗杰,正确的做法就是把fullName [1] [i]和破坏条件都错了。

@edit²:结果:

class BankAccount 
{
private:    
    string fullName[5];
    char accountAddress[10];
    unsigned short int cards;
    float money;
    bool visa, mastercard, americanExpress;

    void checkName(string name, string typeName, bool exception)
    {
        unsigned short int errorVar, i;
        errorVar = i = 0;
        bool space = false;

        do {
            if (errorVar > 0)
                cout << "Enter only the name purposed." << endl << endl;    

            if (exception)
                cout << typeName << " NAME (type no if you haven't): ";

            else
                cout << typeName << " NAME: ";

            getline(cin, name);

            for (i = 0; i < name.size(); i++)
            {
                if (name[i] == ' ')
                {
                    space = true;
                    break;
                }

                else
                    space = false;
            }

            errorVar++;
        } while (space);

        if (name.compare("no") == 0)
            name = "NULL";
    }

public:
    void createAccount() 
    {   
        cout << endl << "FIRST NAME: "; /* First name has no checks (it can be a compound name) */         
        getline(cin, this->fullName[0]); 

        checkName(this->fullName[1], "SECOND", false);
        checkName(this->fullName[2], "THIRD", true); 
        checkName(this->fullName[3], "LAST", false);

        if (this->fullName[2].compare("NULL") == 0)
        {
            this->fullName[4] = this->fullName[0] + string(" ") + this->fullName[1] + string(" ") + this->fullName[3];  //NOT FULL NAME
            cout << this->fullName[4];
        }

        else
        {
            this->fullName[4] = this->fullName[0] + string(" ") + this->fullName[1] + string(" ") + this->fullName[2] + string(" ") + this->fullName[3];  //NOT FULL NAME
            cout << this->fullName[4]; 

        }
    }

    BankAccount() {/* Constructor */}
    ~BankAccount() {/* Deconstructor */}
};

逻辑上这是一个简单的东西。感谢您在其他领域帮助和改进代码。

1 个答案:

答案 0 :(得分:0)

明显而干净的解决方案是将do循环放入函数(或方法)中,并使用fullname[1]调用两次,然后再调用fullname[2]