简体   繁体   English

在C ++中循环时

[英]Do…While loop in c++

int f = 1;
    do

    {
        f++;
        int c, d;
        if ( yesNo == 'y' || yesNo == 'Y')
        {
            cout << "Player 1 how many numbers are on your card" << endl;
            cin >> c;
            mf_bingo(c);
        }

        else if ( yesNo == 'n' || yesNo == 'N')
        {
            cout << "Sorry player 1" << endl;
        }
        cout << "Player 2 do you see this number on your card." << endl;
        cin >> yesNo;
        if (yesNo == 'y' || yesNo == 'Y')
        {
            cout << "Player 2 how many numbers are on your card" << endl;
            cin >> c;
            mf_bingo(c);
        }

        else if (yesNo == 'n' || yesNo == 'N')
        {
            cout << "Sorry player 2" << endl;
        }
        cout << "Press 1 to continue generating random numbers." << endl;
        cin >> d;
        mf_numbers(d);
    } while (c < 5);

I need help getting this do... while loop running. 在循环运行时,我需要帮助才能做到这一点。 It skips past player one and works one time before I get an error box that says abort, retry close. 在我看到一个错误框,提示中止,然后重试关闭之前,它会跳过一个玩家并工作一次。 I am currently using microsft visual studio. 我目前正在使用Microsft Visual Studio。

here is the whole program itself. 这是整个程序本身。 Linux users might have trouble getting the random number generator to work. Linux用户可能无法使随机数生成器正常工作。

using namespace std;
void mf_printcard1(int);
void mf_printcard2(int);
int mf_bingo(int);
void mf_numbers(int);



int main()
{
    cout << "   _________________________________________\n"
        "  |         B |  I |  N  |  G  |  O |       |\n"
        "  |     ------|----|-----|-----|----|----   |\n"
        "  |     ------|----|-----|-----|----|----   |\n"
        "  |     ------|----|-----|-----|----|----   |\n"
        "  |     ------|----|-----|-----|----|----   |\n"
        "  |     ------|----|-----|-----|----|----   |\n"
        "   _________________________________________\n" << endl;
    int a, b, c, d;
    char yesNo;
    cout << "\n\n\n\tCard choices" << endl;
    cout << "Bingo Card 1: 12, 5, 32, 18, 50, 46, 2, 29, 1, 30" << endl;
    cout << "Bingo Card 2: 43, 1, 6, 34, 8, 19, 23, 42, 41, 5" << endl;
    cout << "Bingo Card 3: 12, 5, 22, 45, 50, 13, 3, 8, 35, 18" << endl;
    cout << "Bingo Card 4: 34, 50, 1, 6, 21, 39, 8, 49, 9, 19" << endl;
    cout << "Bingo Card 5: 30, 14, 7, 28, 43, 34, 37, 48, 49, 18" << endl;
    cout << "Bingo Card 6: 12, 9, 10, 41, 50, 37, 11, 29, 2, 7, 47" << endl;
    cout << "Bingo Card 7: 15, 6, 35, 27, 8, 11, 32, 9, 39, 23" << endl;
    cout << "Bingo Card 8: 48, 21, 3, 9, 2, 32, 50, 31, 13, 38" << endl;
    cout << "Bingo Card 9: 20, 49, 28, 10, 8, 7, 14, 46, 19, 34" << endl;
    cout << "Bingo Card 10: 18, 26, 43, 5, 27, 45, 38, 50, 2, 29" << endl;

    cout << "Player 1 pick a card that you want." << endl;
    cout << "Be sure to write down your numbers." << endl;
    cin >> a;
    mf_printcard1(a);
    cout << "\n\n";
    cout << "Now player 2, you pick a card that you want.  It can not be the same as player 1" << endl;
    cout << "Be sure to write down your numbers." << endl;
    cin >> b;
    cout << "\n\n";
    mf_printcard2(b);
    cout << "\nHere is your first number: 10" << endl;
    cout << "Player 1 is this number on your bingo card?" << endl;
    cout << "Press y for yes and n for no" << endl;
    cin >> yesNo;
    if (yesNo == 'y' || yesNo == 'Y')
    {
        cout << "Player 1 how many numbers are on your card" << endl;
        cin >> c;
        mf_bingo(c);
    }

    if (yesNo == 'n' || yesNo == 'N')
    {
        cout << "Sorry player 1" << endl;
    }

    cout << "Player 2 is this number on your bingo card?" << endl;
    cout << "Press y for yes and n for no" << endl;
    cin >> yesNo;
    if (yesNo == 'y' || yesNo == 'Y')
    {
        cout << "Player 2 how many numbers are on your card" << endl;
        cin >> c;
        mf_bingo(c);
    }

    else if (yesNo == 'n' || yesNo == 'N')
    {
        cout << "Sorry player 2" << endl;

    }
    cout << "Press 1 to continue generating random numbers." << endl;
    cin >> d;
    mf_numbers(d);
    int f = 1;
    do

    {
        f++;
        int c, d;
        if ( yesNo == 'y' || yesNo == 'Y')
        {
            cout << "Player 1 how many numbers are on your card" << endl;
            cin >> c;
            mf_bingo(c);
        }

        else if ( yesNo == 'n' || yesNo == 'N')
        {
            cout << "Sorry player 1" << endl;
        }
        cout << "Player 2 do you see this number on your card." << endl;
        cin >> yesNo;
        if (yesNo == 'y' || yesNo == 'Y')
        {
            cout << "Player 2 how many numbers are on your card" << endl;
            cin >> c;
            mf_bingo(c);
        }

        else if (yesNo == 'n' || yesNo == 'N')
        {
            cout << "Sorry player 2" << endl;
        }
        cout << "Press 1 to continue generating random numbers." << endl;
        cin >> d;
        mf_numbers(d);
    } while (f < 5);
    cout << "Game over" << endl;
    return 0;
}
void mf_printcard1(int a)
{
    cout << "\n";
    cout << "Player 1 here is your card" << endl;
    cout << "\n";
    if (a == 1)
    {
        cout << "Your Bingo numbers are: 12, 5, 32, 18, 50, 46, 2, 29, 1, 30" << endl;
    }
    else if (a == 2)
    {
        cout << "Your bingo numbers are: 43, 1, 6, 34, 8, 19, 23, 42, 41, 5" << endl;
    }
    else if (a == 3)
    {
        cout << "Your bingo numbers are: 12, 5, 22, 45, 50, 13, 3, 8, 35, 18" << endl;
    }
    else if (a == 4)
    {
        cout << "Your bingo numbers are: 34, 50, 1, 6, 21, 39, 8, 49, 9, 19" << endl;
    }
    else if (a == 5)
    {
        cout << "Your bingo numbers are: 30, 14, 7, 28, 43, 34, 37, 48, 49, 18" << endl;
    }
    else if (a == 6)
    {
        cout << "Your bingo numbers are: 12, 9, 10, 41, 50, 37, 11, 29, 2, 7, 47" << endl;
    }
    else if (a == 7)
    {
        cout << "Your bingo numbers are: 15, 6, 35, 27, 8, 11, 32, 9, 39, 23" << endl;
    }
    else if (a == 8)
    {
        cout << "Your bingo numbers are: 48, 21, 3, 9, 2, 32, 50, 31, 13, 38" << endl;
    }
    else if (a == 9)
    {
        cout << "Your bingo numbers are: 20, 49, 28, 10, 8, 7, 14, 46, 19, 34" << endl;
    }
    else if (a == 10)
    {
        cout << "Your bingo numbers are: 18, 26, 43, 5, 27, 45, 38, 50, 2, 29" << endl;
    }

    else
    {
        cout << "Not a valid option" << endl;
    }
}
void mf_printcard2(int b)
{
    cout << "\n";
    cout << "Player 2 here is your card" << endl;
    cout << "\n";
    if (b == 1)
    {
        cout << "Your Bingo numbers are: 12, 5, 32, 18, 50, 46, 2, 29, 1, 30" << endl;
    }
    else if (b == 2)
    {
        cout << "Your bingo numbers are: 43, 1, 6, 34, 8, 19, 23, 42, 41, 5" << endl;
    }
    else if (b == 3)
    {
        cout << "Your bingo numbers are: 12, 5, 22, 45, 50, 13, 3, 8, 35, 18" << endl;
    }
    else if (b == 4)
    {
        cout << "Your bingo numbers are: 34, 50, 1, 6, 21, 39, 8, 49, 9, 19" << endl;
    }
    else if (b == 5)
    {
        cout << "Your bingo numbers are: 30, 14, 7, 28, 43, 34, 37, 48, 49, 18" << endl;
    }
    else if (b == 6)
    {
        cout << "Your bingo numbers are: 12, 9, 10, 41, 50, 37, 11, 29, 2, 7, 47" << endl;
    }
    else if (b == 7)
    {
        cout << "Your bingo numbers are: 15, 6, 35, 27, 8, 11, 32, 9, 39, 23" << endl;
    }
    else if (b == 8)
    {
        cout << "Your bingo numbers are: 48, 21, 3, 9, 2, 32, 50, 31, 13, 38" << endl;
    }
    else if (b == 9)
    {
        cout << "Your bingo numbers are: 20, 49, 28, 10, 8, 7, 14, 46, 19, 34" << endl;
    }
    else if (b == 10)
    {
        cout << "Your bingo numbers are: 18, 26, 43, 5, 27, 45, 38, 50, 2, 29" << endl;
    }

    else
    {
        cout << "Not a valid option" << endl;
    }
}
int mf_bingo(int c)
{
    if (c == 0)
        cout << "You need to match get 5 more matching numbers to win." << endl;
    else if (c == 1)
    {
        cout << "You only need 4 more matching numbers to win" << endl;
    }
    else if (c == 2)
    {
        cout << "You only need 3 more numbers to win" << endl;
    }
    else if (c == 3)
    {
        cout << "You only need 2 more numbers to win" << endl;
    }
    else if (c == 4)
    {
        cout << "You only need 1 more number to win" << endl;
    }
    else if (c == 5)
    {
        cout << "You win!!!" << endl;
    }
    return true;
}
void mf_numbers(int d)
{
    int xRan;
    srand(time(0));
    xRan = rand() % 50 + 1;
    cout << xRan << endl;

}

Its because your condition for the loop is (c < 5) when c is not defined until the loop body. 这是因为,直到循环体中才定义c时,循环的条件是(c < 5) You either need to change while (c < 5) to while (f < 5) or define c outside of the loop body. 您需要将while (c < 5)更改为while (f < 5)或者在循环主体外部定义c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM