我是新来的函数,并试图获得返回值的挂起。更改返回值
我做了一个打印文本的函数,但我希望它根据实际函数中发生的情况返回一个值。
这是一个例子:
int function(char a)
{
int b = 0;
if(a == 'a')
{
b++;
}
cout << "Example";
if(b == 1){
return 1;
}
else
{
return 0;
}
}
然而,在这种情况下,返回值将被“卡住”设置为0,并不会更改为1。有没有一种方法,使返回值的可靠在函数中发生了什么?或者这不是它应该如何完成的?
编辑:这是我正在处理的代码。基本上这是瑞典语中的“Hang子手”游戏。我想是的“wordCheck”函数返回一个值,如果所有的信中处处都(和单词完成),但现在它只是返回0,即使“NC”是4
#include <iostream>
#include <string>
using namespace std;
int wordCheck(char word[4], int c)
{
int nC = 0, a;
if(c != 1)
{
for (int i = 0; i < 4; i++)
{
if (word[i] == NULL)
{
word[i] = '_';
}
else if (word[i] != '_')
{
nC++;
}
}
cout << "Ordet: "<< word[0] << " " << word[1] << " " << word[2] << " " << word[3] << endl << endl;
}
if(nC == 4){
return 1;
}
else
{
return 0;
}
}
int main()
{
setlocale(LC_ALL,"");
char guess = 'l', word[50] = "bajs", letter[4] = {NULL, NULL, NULL, NULL}, null[4] = {NULL, NULL, NULL, NULL};
int i = 0, d = wordCheck(null, 1);
do
{
i++;
system("cls");
cout << "**********HÄNGA GUBBE**********" << endl;
cout << "*******************************" << endl;
cout << "*********GISSA PÅ ORDET********" << endl;
cout << "-------------------------------" << endl << endl;
wordCheck(letter, 0);
cout << "Gissning " << i << " : ";
cout << d;
cin >> guess;
if (guess == word[0])
{
letter[0] = 'B';
}
else if (guess == word[1])
{
letter[1] = 'a';
}
else if(guess == word[2])
{
letter[2] = 'j';
}
else if (guess == word[3])
{
letter[3] = 's';
}
} while (wordCheck(null, 1) != 1);
system("pause");
return 0;
}
(COUT < < d仅仅是用于调试的返回值,还考虑到裸露的代码不被抛光,这是一项正在进行的工作)
@dasblinkenlight我不好,我编辑了这篇文章。这是一个错字。 – user2166357 2014-12-03 20:14:59
你想做什么? – user3344003 2014-12-03 20:21:23
它如何“卡住”?你尝试过'function('a')'吗? – crashmstr 2014-12-03 20:22:43