gpt4 book ai didi

c++ - Do....While 在 C++ 中循环

转载 作者:行者123 更新时间:2023-11-28 06:16:35 24 4
gpt4 key购买 nike

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;

}

最佳答案

因为你的循环条件是(c < 5)当 c 直到循环体才被定义。您要么需要更改 while (c < 5)while (f < 5)或定义 c在循环体之外。

关于c++ - Do....While 在 C++ 中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com