在C ++中循环布尔

时间:2015-11-20 03:57:38

标签: c++ boolean

我是C ++的新手,目前通过Code :: Blocks运行我的代码 所以,我想问一下,当用户在此代码中输入No时,如何重复相同的代码:

if (first == "No" )
{
    cout << "\nPlease Re-enter Your Name. \n";
    cin >> name;
    cout << "\nPlease Re-enter Your Age. \n";
    cin >> age;
    cout << "\nPlease Re-select Your Class. \n";
    cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
    cout << setfill(' ') << setw(1) << "|" << setw(15) << left << "Classes" << setw(1) << "|" << setw(15) << left << "Advanced" << setw(1) << "|"  << setw(5) << endl;
    cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
    cout << setfill(' ') << setw(1) << "|" << setw(15) << left << "Mage" << setw(1) << "|" << setw(15) << left << "Sorceress" << setw(1) << "|"  << setw(5) << endl;
    cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
    cout << setfill(' ') << setw(1) << "|" << setw(15) << left << "Fighter" << setw(1) << "|" << setw(15) << left << "Warrior" << setw(1) << "|" << endl;
    cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
    cout << setfill(' ') << setw(1) << "|" << setw(15) << left << "Ninja" << setw(1) << "|" << setw(15) << left << "Assassin" << setw(1) << "|"  << setw(5) << endl;
    cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;

    cout << "\n";
    cin >> str_class;
    if (str_class == "Mage" )
    {
        cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
        cout << setfill(' ') << setw(1) << "|" << setw(15) << left << "Name" << setw(1) << "|" << setw(15) << left << "Class" << setw(1) << "|"  << setw(15) << "Age" << setw(1) << "|" << endl;
        cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
        cout << setfill(' ') << setw(1) << "|" << setw(15) << left << name << setw(1) << "|" << setw(15) << left << str_class << setw(1) << "|"  << setw(15) << age << setw(1) << "|" << endl;
        cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
    }

    if (str_class == "Fighter")
    {
        cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
        cout << setfill(' ') << setw(1) << "|" << setw(15) << left << "Name" << setw(1) << "|" << setw(15) << left << "Class" << setw(1) << "|"  << setw(15) << "Age" << setw(1) << "|" << endl;
        cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
        cout << setfill(' ') << setw(1) << "|" << setw(15) << left << name << setw(1) << "|" << setw(15) << left << str_class << setw(1) << "|"  << setw(15) << age << setw(1) << "|" << endl;
        cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
    }

    if (str_class == "Ninja")
    {
        cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
        cout << setfill(' ') << setw(1) << "|" << setw(15) << left << "Name" << setw(1) << "|" << setw(15) << left << "Class" << setw(1) << "|"  << setw(15) << "Age" << setw(1) << "|" << endl;
        cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
        cout << setfill(' ') << setw(1) << "|" << setw(15) << left << name << setw(1) << "|" << setw(15) << left << str_class << setw(1) << "|"  << setw(15) << age << setw(1) << "|" << endl;
        cout << setfill('-') << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << endl;
    }

    cout << "Your Profile Has Been Changed. \nDo You Want This Profile To Be Sent To The Academy Database ? (Yes/No) \n";
    cin >> first;
}

如何改变这一点,以便每次用户说“不”,它会重复这段代码吗?

1 个答案:

答案 0 :(得分:-1)

使用while语句,第一个等于No,然后继续运行循环。当它们再次输入No时,它将使用户保持循环并重新运行while循环中的代码。

    while (first == "No" )
            {
...
    cout << "Your Profile Has Been Changed. \nDo You Want This Profile To Be Sent To The Academy Database ? (Yes/No) \n";
                cin >> first;

        }