joe*_*joe -1 c++ compiler-errors
} while (mf_bingo(c) != 5);
Run Code Online (Sandbox Code Playgroud)
这是我不断收到错误的那一行.我尝试在mf_bingo(c)之前放一个空格并取出(c)
编辑:这是完整的循环
do
{
char yesNo;
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 (mf_bingo(c) != 5);
Run Code Online (Sandbox Code Playgroud)
这是mf_numbers
void mf_numbers(int d)
{
int xRan;
srand(time(0));
xRan = rand() % 50 + 1;
cout << xRan << endl;
}
Run Code Online (Sandbox Code Playgroud)
这是mf_bingo
void 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;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是声明无效mf_bingo().你必须让这个函数返回一个int.
您可以使用以下代码重现此问题:
void mf_bingo(int c) { // void doesn't return anything
}
int main() {
do { } while(mf_bingo(c) != 5);
}
Run Code Online (Sandbox Code Playgroud)
错误C2120解释说,当函数返回时void,即没有,您无法将其与整数进行比较.通常情况下,intellisence应该已经将mf_bingo强调为潜在错误,并提供更准确的描述.
您可以轻松纠正此问题:
int mf_bingo(int c) { // here we return something
... // same logic as before
return c;
}
Run Code Online (Sandbox Code Playgroud)