关于网友提出的“ not all control paths return a value”问题疑问,本网通过在网上对“ not all control paths return a value”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: not all control paths return a value
描述:#include
#include
#include
#include
void shuffle(int [][13]);
int dduizi(const char *[]);
int huase(const char *[]);
int sunzi(const char *[]);
void fapai( int [][13],const char *[],const char *[]);
int main()
{
int result;
const char *suit[4]=
{"hearts","diamonds","clubs","spades"};
const char *face[13]=
{"Ace","deuce","three","four",
"five","six","seven","eight",
"nine","ten","jack","queen","king"};
int deck[4][13]={0};
srand(time(0));
shuffle(deck);
fapai(deck,face,suit);
result = dduizi(face);
//h=huase(suit);
//s=sunzi(face);
cout<<"有"<<><>
return 0;
}
void shuffle(int wdeck[][13])
{
int row,colum;
for (int card=1;card<=52;card++)
{
do
{row= rand() % 4;
colum= rand() % 13;
}
while(wdeck[row][colum] !=0);
wdeck[row][colum]=card;
}
}
void fapai(int wdeck[][13],const char *wface[],const char *wsuit[])
{
for(int card=1;card<=5;card++)
for(int row=0;row<=3;row++)
for(int colum=0;colum<=12;colum++)
if(wdeck[row][colum]==card)
cout<<><>
<<>
<<><>
<<>
<<>
<<(card % 2==0?'\n':'\t');
}
int dduizi(const char *face[])
{
int f=0;
for(int colum=1;colum<6;colum++)
for(int i=0;i<6;i++)
if(face[colum]==face[i+1])
return f++;
else
return 0;
}
菜鸟求教!
为什么我的值没有传不回去呢?
解决方案1: 看了LZ的程序,可知dduizi这个函数是判断5张牌中有几对对子。但这个函数的错误不仅是编译警告所提示的问题,而且是不能完成预定的功能。参数face是13张牌的数值,是常数而不是随机发的5张牌。
要在发牌函数fapai中记录5张牌的face号和suit号,为此要定义一个类型和数组如下:
#define MAX_CARD_BUFF 5
typedef struct
{
int suit_id;
int face_id;
} card_t;
card_t card_buffer[MAX_CARD_BUFF];
然后再判断card_buffer中有几对是相同的face_id。整个程序修改如下:
#include
#include
#include
#include
#define MAX_CARD_BUFF 5
typedef struct
{
int suit_id;
int face_id;
} card_t;
card_t card_buffer[MAX_CARD_BUFF];
void shuffle(int [][13]);
int dduizi(void);
int huase(const char *[]);
int sunzi(const char *[]);
void fapai( int [][13],const char *[],const char *[]);
int main()
{
int result;
const char *suit[4]=
{"hearts","diamonds","clubs","spades"};
const char *face[13]=
{"Ace","deuce","three","four",
"five","six","seven","eight",
"nine","ten","jack","queen","king"};
int deck[4][13]={0};
srand(time(0));
shuffle(deck);
fapai(deck,face,suit);
result = dduizi();
//h=huase(suit); 这也是错的
//s=sunzi(face); 这也是错的
cout << "有" << result << "个对子" << endl;
return 0;
}
void shuffle(int wdeck[][13])
{
int row,column;
for (int card=1;card <=52;card++)
{
do
{
row= rand() % 4;
column= rand() % 13;
}
while(wdeck[row][column] !=0);
wdeck[row][column]=card;
}
}
void fapai(int wdeck[][13],const char *wface[],const char *wsuit[])
{
for(int card=0;card<>
for(int row=0;row <=3;row++)
for(int column=0;column <=12;column++)
if(wdeck[row][column]==(card+1))
{
card_buffer[card].face_id=row;
card_buffer[card].suit_id=column;
cout << setw(8) << setiosflags(ios::right)
<< wface[column] << "of "
<< setw(8) << setiosflags(ios::left)
<< setw(8)
<< wsuit[row]
<< (card % 2==0?'\n':'\t');
}
}
int dduizi(void)
{
int count=0;
for(int j=0;j<>
{
for(int i=j+1;i<>
{
if (card_buffer[i].suit_id<0) continue; /*已经对上了*/
if(card_buffer[j].suit_id==card_buffer[i].suit_id)
{
card_buffer[i].suit_id=-1; /*已经对上的改为-1*/
count++;
}
}
}
return count;
}
另外,LZ将要写的判断花色和顺子的函数也要按此思路修改。
以上介绍了“ not all control paths return a value”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3785059.html