C++ 程序中没有输出?
我正在制作一个程序来洗牌一副牌,然后发两手牌,但是现在,没有输出!我仔细阅读了无限循环的代码,但什么也找不到!有人能帮我吗C++ 程序中没有输出?,c++,infinite-loop,visual-studio-debugging,visual-studio-2013,playing-cards,C++,Infinite Loop,Visual Studio Debugging,Visual Studio 2013,Playing Cards,我正在制作一个程序来洗牌一副牌,然后发两手牌,但是现在,没有输出!我仔细阅读了无限循环的代码,但什么也找不到!有人能帮我吗 // (Description in Word File) #include <iostream> #include <cstdlib> #include <ctime> #include <string> using namespace std; unsigned seed = time(0); // for use of
// (Description in Word File)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
unsigned seed = time(0); // for use of rand()
const int SUIT = 4;
const int FACE = 13;
const int HAND = 5;
// A couple of reference arrays.
const string SUITS[SUIT] = {" Hearts", " Diamonds", " Clubs", " Spades"};
const string FACES[FACE] = {"Ace of", "Two of", "Three of",
"Four of", "Five of", "Six of", "Seven of",
"Eight of", "Nine of", "Ten of",
"Jack of", "Queen of", "King of"};
// The reason that these functions are in Main is because
// it gives me error 2065: undeclared identifier otherwise.
void shuffle(string[][FACE]);
void deal(string[][FACE], string[HAND]);
void displayDeck(string[][FACE]);
void displayHand(string[HAND]);
int main()
{
string deck[SUIT][FACE];
string hand1[HAND];
string hand2[HAND];
srand(seed); // for use of rand()
// Shuffle the deck.
shuffle(deck);
// Now display the deck
displayDeck(deck);
// Deal for each hand.
deal(deck, hand1);
deal(deck, hand2);
// Now display each hand.
displayHand(hand1);
displayHand(hand2);
return 0;
}
// This function will keep track of face values
// for the shuffle function
bool duplCheck(string cards[][FACE], int suit, int face)
{
bool status = true;
for (int count1 = 0; count1 != SUIT; count1++)
for (int count2 = 0; count2 != FACE; count2++)
if (cards[count1][count2] == cards[suit][face]
&& suit != count1 && face != count2)
return false;
return status;
}
// This function will shuffle the deck.
void shuffle(string cards[][FACE])
{
int randFace, randSuit;
// These loops will assign random face values
// and suits to each place in cards[][].
for (int count1 = 0; count1 != SUIT; count1++)
for (int count2 = 0; count2 != FACE; count2++)
{
do
{
randFace = rand() % FACE;
randSuit = rand() % SUIT;
if (duplCheck(cards, randSuit, randFace) == true)
cards[count1][count2] =
FACES[randFace] + SUITS[randSuit];
} while(duplCheck(cards, randSuit, randFace) == false);
}
}
// This function deals out a hand of five random cards.
void deal(string cards[][FACE], string hand[HAND])
{
for (int count = 0; count != HAND; count++)
{
// make random suit and face numbers
int randSuit = rand() % SUIT;
int randFace = rand() % FACE;
// If random card is not obsolete...
if (cards[randSuit][randFace] != "null")
// ...assign that card to hand.
hand[count] = cards[randSuit][randFace];
// obsolete that card
cards[randSuit][randFace] = "null";
}
}
void displayDeck(string cards[][FACE])
{
std::cout << "\t\tThe Deck:\n\n";
for (int count1 = 0; count1 != SUIT; count1++)
for (int count2 = 0; count2 != FACE; count2++)
{
std::cout << ((count1 * FACE) + count2 + 1) << " "
<< cards[count1][count2] << endl;
}
}
void displayHand(string hand[HAND])
{
}
//(Word文件中的描述)
#包括
#包括
#包括
#包括
使用名称空间std;
无符号种子=时间(0);//用于rand()的使用
常数=4;
const int FACE=13;
常数int HAND=5;
//一对参考数组。
康斯特弦套装[套装]={“红心”、“钻石”、“梅花”、“黑桃”};
常量字符串FACES[FACE]={“Ace of”、“Two of”、“Three of”,
“四之”、“五之”、“六之”、“七之”,
“八分之”、“九分之”、“十分之”,
“杰克的”、“女王的”、“国王的”};
//这些功能主要是因为
//它给了我错误2065:否则是未声明的标识符。
无效洗牌(字符串[][面]);
无效交易(字符串[][面]、字符串[手]);
void displayDeck(字符串[][面]);
void displayHand(字符串[HAND]);
int main()
{
弦甲板[套装][正面];
弦手1[手];
弦手2[手];
srand(seed);//用于rand()的使用
//洗牌。
洗牌(牌组);
//现在展示甲板
显示甲板(甲板);
//每手交易。
交易(甲板、手1);
交易(甲板、手2);
//现在展示每只手。
显示手(手1);
显示手(hand2);
返回0;
}
//此函数将跟踪面值
//对于shuffle函数
bool duplCheck(字符串卡[][面]、整数套装、整数面)
{
布尔状态=真;
for(int count1=0;count1!=SUIT;count1++)
for(int count2=0;count2!=FACE;count2++)
如果(卡片[count1][count2]==卡片[suit][face]
&&西装!=count1和&face!=count2)
返回false;
返回状态;
}
//此函数将洗牌牌组。
无效洗牌(串牌[][面])
{
内特兰德face,兰德西特;
//这些循环将指定随机的面值
//并适用于卡片[]中的每个位置。
for(int count1=0;count1!=SUIT;count1++)
for(int count2=0;count2!=FACE;count2++)
{
做
{
randFace=rand()%FACE;
randSuit=rand()%SUIT;
if(duplCheck(卡片、随机套装、随机脸)=真)
卡片[count1][count2]=
脸+套装;
}while(duplCheck(卡片、随机套装、随机脸)=false);
}
}
//此函数用于分发一手五张随机牌。
无效交易(串牌[][面],串手[手])
{
for(int count=0;count!=HAND;count++)
{
//随机制作西装和脸部号码
int randSuit=rand()%SUIT;
int randFace=rand()%FACE;
//如果随机卡没有过时。。。
如果(卡片[randSuit][randFace]!=“空”)
//…把那张卡交给我。
手牌[计数]=牌[randSuit][randFace];
//那张卡过时了
卡片[randSuit][randFace]=“空”;
}
}
无效显示组(字符串卡[][面])
{
std::cout您的显示手功能为空
void displayHand(string hand[HAND])
{
cout << "Put some output here";
}
void displayHand(字符串指针[指针])
{
cout您的问题在于随机播放,特别是在使用duplCheck时
void shuffle(string cards[][FACE])
{
int randFace, randSuit;
// These loops will assign random face values
// and suits to each place in cards[][].
for (int count1 = 0; count1 != SUIT; count1++)
for (int count2 = 0; count2 != FACE; count2++)
{
do
{
randFace = rand() % FACE;
randSuit = rand() % SUIT;
std::cout << count1 << "," << count2 << " trying " << randFace << "/" << randSuit << std::endl;
if (duplCheck(cards, randSuit, randFace) == true) {
std::cout << "duplCheck returned true" << std::endl;
cards[count1][count2] =
FACES[randFace] + SUITS[randSuit];
}
} while(duplCheck(cards, randSuit, randFace) == false);
}
}
因为duplCheck返回false,所以您将永久留在这个循环中
bool duplCheck(string cards[][FACE], int suit, int face)
{
bool status = true;
for (int count1 = 0; count1 != SUIT; count1++)
for (int count2 = 0; count2 != FACE; count2++)
if (cards[count1][count2] == cards[suit][face]
&& suit != count1 && face != count2)
return false;
return status;
}
如果存在重复项,duplCheck似乎会返回“false”,如果没有,则返回“true”,但您对它的使用预期正好相反:当duplCheck返回true时,while循环将停止,如果存在重复项,则duplCheck将返回true。问题是,您的数据组最初以空(“”)开头值。您可以在调用shuffle()之前在开始处填写此项,也可以将此检查添加到duplCheck()的开始处:
displayHand
什么都不做。你希望发生什么?什么是“duplCheck”你在shuffle的主循环中调用它两次吗?30秒添加一些额外的输出表明这就是你的代码中断的地方。据我所知,你永远不会离开shuffle
有效但不是答案。我想添加它作为注释,但我没有足够的声誉…@kfsoneIt不起作用,无论我做了什么更改。我想我会的采取另一种方法。是的,我想出了另一种方法。天哪,这个项目很痛苦!!
bool duplCheck(string cards[][FACE], int suit, int face)
{
bool status = true;
for (int count1 = 0; count1 != SUIT; count1++)
for (int count2 = 0; count2 != FACE; count2++)
if (cards[count1][count2] == cards[suit][face]
&& suit != count1 && face != count2)
return false;
return status;
}
if(cards[suit][face].length() == 0)
return true;