做....在c ++中循环

时间:2015-05-11 01:20:44

标签: 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);

我需要帮助才能做到这一点......循环运行时。它会跳过播放器一,并在我收到一个显示中止的错误框之前工作一次,重试关闭。我目前正在使用microsft visual studio。

这是整个程序本身。 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;

}

1 个答案:

答案 0 :(得分:1)

因为在循环体之前没有定义c时,循环的条件是(c < 5)。您需要将while (c < 5)更改为while (f < 5)或在循环体外定义c