如何将'int'函数调用到'void'函数
how to call a 'int' function to a 'void' function
我试图根据使用2个不同的函数的返回值打印某个消息。当在内部调用函数时,另一个则表示没有声明
int isMatch(int x1, int y1, int x2, int y2)
{
if(deck[x1][y1] == deck[x2][y2])
{
return 2;
}
else if(deck[x1][y1][0] == deck[x2][y2][0])
{
return 1;
}
else if(deck[x1][y1][1] == deck[x2][y2][1])
{
return 1;
}
return 0;
}
void printMatch()
{
int attempt;
isMatch(x1, y1, x2, y2);
if(isMatch() == 2)
{
cout << "You have a match!";
attempt++;
}
}
当我这样尝试时,它仍然不起作用
int isMatch(int x1, int y1, int x2, int y2)
{
if(deck[x1][y1] == deck[x2][y2])
{
return 2;
}
else if(deck[x1][y1][0] == deck[x2][y2][0])
{
return 1;
}
else if(deck[x1][y1][1] == deck[x2][y2][1])
{
return 1;
}
return 0;
}
void printMatch()
{
int attempt;
int isMatch(int x1, int y1, int x2, int y2);
if(isMatch() == 2)
{
cout << "You have a match!";
attempt++;
}
}
!!!!!!!!!!更新 !!!!!!!!!!!!!!!!!
int isMatch(int x1, int y1, int x2, int y2)
{
if(deck[x1][y1] == deck[x2][y2]) /// Checks entire element (_,_)
{
return 2;
}
else if(deck[x1][y1][0] == deck[x2][y2][0]) /// Checks first element (_, )
{
return 1;
}
else if(deck[x1][y1][1] == deck[x2][y2][1]) /// Checks second element ( ,_)
{
return 1;
}
return 0;
}
void printMatch()
{
int attempt = 0; /// increments the users attempts
int score = 0; /// adds total score
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
int match = isMatch(x1, y1, x2, y2);
if (match == 2)
{
attempt++;
score = score + 2;
cout << "nYou have a match!"
<< "nTotal score is: " << score
<< "nTotal no. of attempts: " << attempt << endl << endl;
}
else if (match == 1)
{
attempt++;
score = score + 1;
cout << "nYou have a match!"
<< "nTotal score is: " << score
<< "nTotal no. of attempts: " << attempt << endl << endl;
}
}
将printMatch()函数更改为:
void printMatch(int x1, int y1, int x2, int y2)
{
int attempt;
int temp = isMatch(x1, y1, x2, y2);
if(temp == 2)
{
cout << "You have a match!";
attempt++;
}
}
这将isMatch
函数的返回值保存到名为temp
的变量中。然后,您可以使用这个变量temp
来测试您的条件。
您的printMatch
函数存在许多问题。试一试
void printMatch()
{
int x1 = 4, y1 = 3, x2 = 2, y1 = 1; //you'll need to define these values in some useful way
int attempt = 0;
int match = isMatch(x1, y1, x2, y2);
if(match == 2)
{
cout << "You have a match!";
attempt++;
}
}
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 在c++类上调用void函数
- C++为什么尽管我调用了void函数,它却不起作用
- 如何从void函数输出字符串
- 如何将指针从一个void函数传递到另一个C++
- 为什么我在使用void函数时得到错误代码C2276
- 指针没有更新它在void函数内部指向的值
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- void 函数可以传递值参数吗?
- 在 void 函数中使用 #include 变量C++
- 如何在 C++ 中将 void 函数传递给 void 指针
- 使用运算符调用 void 函数时出错<set>
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- C++ :: 如何捕获由 void 函数创建的矩阵
- 如何将一个 void 函数传递给另一个 void 函数?
- 当我使用 void 函数的返回值(通过强制转换函数指针)时,究竟会发生什么?
- C++命名空间中调用 void 函数的错误
- 当数组位于两个循环之间时,您可以调用数组的 void 函数吗?
- 结构新手,我对如何从 void 函数中返回值并将其放入另一个函数感到困惑
- 查找素数:错误:控件到达非void函数的末尾