我实际上是想用c ++创建一个虚张声势的卡片游戏。当我打电话给showAllCards函数时,牌组中的牌(Ace of Hearts)没有显示在输出上,显示它的唯一方法是在主体中添加两个牌组结构。当程序运行时(没有顶牌), dev也会产生一些警告。我究竟做错了什么? 您也可以参考一下程序中的一些改进/更改。 这是代码:
#include <iostream>
#include<Strings.h>
#include<cstdlib>
using namespace std;
struct link{
int data;
link *link;
};
struct deck
{
string suit[32];
string value[32];
}card;
struct cardsDeck{
string nxt[20];
string suitlist[4] = {"hearts","spades","clubs","diamonds"};
string vals[8] = {"ace","two","three","four","five","jack","queen","king"};
}cdeck;
class Game
{
private:
link *first;
public:
Game();
void check();
void mask();
void pass();
void enterCard();
void showAllCards();
void shuffle();
void rearrangeCards();
};
Game::Game(){
first=NULL;
}
void Game::rearrangeCards(){
short int x = 0, y = 0, z = 0;
while(x < 32)
{
card.suit[x] = cdeck.suitlist[y];
card.value[x] = cdeck.vals[z];
++x;
++z;
if(x % 8 == 0)
{
++y;
}
if(z % 8 == 0)
{
z = 0;
}
}
}
void Game::showAllCards(){
int x;
while(x < 32)
{
if(card.suit[x]!=card.suit[x-1]){
cout<<"\n";
}
cout << card.value[x] << " of " << card.suit[x] << "\n";
++x;
}
}
int main(){
Game game;
int op;
game.rearrangeCards();
cout<<"Welcome to Bluff Mashter 20/12/15::6:46\nEnter the Operation please:\n\n";
cin>>op;
while(op!=0){
switch(op){
case 1:
game.showAllCards();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
}
}
return 1;
}
以下是警告
20 63 E:\Projjects\mainbluuf.cpp [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
21 78 E:\Projjects\mainbluuf.cpp [Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
20 63 E:\Projjects\mainbluuf.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
21 78 E:\Projjects\mainbluuf.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
答案 0 :(得分:1)
假设您的方法是这样的:
Error: No compatible version found: cordova-android@'>=2.3.0 <2.4.0'
Valid install targets:
["3.5.0","3.5.1","3.6.0","3.6.1","3.6.3","3.6.4","3.7.0","3.7.1","4.0.0","4.0.1","3.7.2","4.0.2","4.1.0","4.1.1","5.0.0"]
在您的第一次迭代中,您正在执行此操作:void Game::showAllCards(){
int x = 0;
while(x < 32)
{
if(card.suit[x]!=card.suit[x-1]){
cout<<"\n";
}
cout << card.value[x] << " of " << card.suit[x] << "\n";
++x;
}
}
这是错误的。
card.suit[0]!=card.suit[-1]
虽然我没有测试代码,但我想这可以解决问题。
注意:我将void Game::showAllCards(){
int x = 1;
while(x < 32)
{
if(card.suit[x]!=card.suit[x-1]){
cout<<"\n";
}
cout << card.value[x] << " of " << card.suit[x] << "\n";
++x;
}
}
设置为x
,这是唯一的变化。由于您是C ++的新手,默认情况下1
变量可以包含存储在该内存地址中的任何不可预测的值,因此良好的做法是 始终 初始化你的变量。
<强>更新强>:
试试这个:
uninitialized
更新2 :
void Game::showAllCards(){
for(x = 0; x < 32; ++x)
{
cout << card.value[x] << " of " << card.suit[x] << "\n";
}
}
更新3:
int main(){
Game game;
int op;
game.rearrangeCards();
cout<<"Welcome to Bluff Mashter 20/12/15::6:46\nEnter the Operation please:\n\n";
cin >> op;
while(op!=0){
switch(op){
case 1:
game.showAllCards();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
}
cin >> op;
}
return 0;
}