注册 登录
编程论坛 C++教室

求解~! 程序崩溃的原因

tantan821 发布于 2010-08-24 11:49, 519 次点击
#include <iostream>
#include <vector>
#include <time.h>
using namespace std;

class dian
{
public:
    dian();
    void getppai();
    void rule();
    void bigen();
    void yaopai();
    void shuchu();
    void dshuchu();
    void shuchu1();
    float fanhui();
private:
    float zong;
    int s;
    float a1[2][10];
    vector<int> ivec;
};
 
dian::dian()
{
        zong=0;
        s=2;;
    ivec.push_back(1);
    ivec.push_back(2);
    ivec.push_back(3);
    ivec.push_back(4);
    ivec.push_back(5);
    ivec.push_back(6);
    ivec.push_back(7);
    ivec.push_back(8);
    ivec.push_back(9);
    ivec.push_back(10);
    ivec.push_back(11);
    ivec.push_back(12);
    ivec.push_back(13);
}
void dian::rule()                                                               //游戏规则            
{
cout<<" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"   welcome to 21点 game~!\n";
cout<<"   game rules:\n";
cout<<" 1.庄家 :本轮坐庄的玩家既是计算机.\n";
cout<<" 3.闲家 :本轮不坐庄的玩家。 \n";
cout<<" 4.如果玩家的点数大于21点时点,玩家将不能再要牌;\n";
cout<<" 5.当牌中出现J,Q,K按0.5点计算;\n";  
cout<<" 6.如果玩家和电脑同时爆点或者点数相同系统判定为和局!\n";
cout<<" 7.good luck to you!"<<endl;
cout<<" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"<<endl;
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
}
void dian::bigen()
{
    int k,c;
    k=rand()%13;
    a1[1][0]=ivec[k];
    if(a1[1][0]==11)
    {
        a1[2][0]=0.5;
    }
    else if(a1[1][0]==12)
    {
        a1[2][0]=0.5;
    }
    else if (a1[1][0]==13)
    {
        a1[2][0]=0.5;
    }
    else
    {
        a1[2][0]=a1[1][0];
    }
    srand((unsigned)time(NULL));
    c=rand()%13;
    a1[1][1]=ivec[c];
    if(a1[1][1]==11)
    {
        a1[2][1]=0.5;
    }
    else if(a1[1][1]==12)
    {
        a1[2][1]=0.5;
    }
    else if (a1[1][1]==13)
    {
        a1[2][1]=0.5;
    }
    else
    {
        a1[2][1]=a1[1][1];
    }
    zong=a1[2][0]+a1[2][1];
}
void dian::yaopai()
{
    int k;
    k=rand()%13;
    a1[1][s]=ivec[k];
    if(a1[1][s]==11)
    {
        a1[2][s]=0.5;
    }
    else if(a1[1][s]==12)
    {
        a1[2][s]=0.5;
    }
    else if (a1[1][s]==13)
    {
        a1[2][s]=0.5;
    }
    else
    {
        a1[2][s]=a1[1][s];
    }
    zong=zong+a1[2][s];
    cout<<a1[2][s]<<endl<<zong;
    s=++s;
}
void dian::shuchu()
{
    cout<<"你牌的点数是:";
    for (int i=0;i<s;++i)
    {
        if(a1[1][i]==11)
        {
            cout<<"J"<<"\t";
            a1[2][i]=0.5;
        }
        else if(a1[1][i]==12)
        {
            cout<<"Q"<<"\t";
            a1[2][i]=0.5;
        }
        else if (a1[1][i]==13)
        {
            cout<<"K"<<"\t";
            a1[2][i]=0.5;
        }
        else
        {
            a1[2][i]=a1[1][i];
            cout<<a1[2][i]<<"\t";
        }
        zong=zong+a1[2][i];
    }
    cout<<"一共"<<zong<<"点";
    cout<<endl;
}
void dian::dshuchu()
{
    cout<<"电脑第二张牌的点数是:";
    for (int i=1;i<s;++i)
    {
        if(a1[1][i]==11)
        {
            cout<<"J"<<"\t";
            a1[2][i]=0.5;
        }
        else if(a1[1][i]==12)
        {
            cout<<"Q"<<"\t";
            a1[2][i]=0.5;
        }
        else if (a1[1][i]==13)
        {
            cout<<"K"<<"\t";
            a1[2][i]=0.5;
        }
        else
        {
            a1[2][i]=a1[1][i];
            cout<<a1[2][i]<<"\t";
        }
        zong=zong+a1[2][i]-a1[2][0];
    }
    cout<<"一共"<<zong<<"点";
    cout<<endl;
}

void dian::shuchu1()
{
    cout<<"电脑的点数是:";
    for (int i=0;i<s;++i)
    {
        if(a1[1][i]==11)
        {
            cout<<"J"<<"\t";
            a1[2][i]=0.5;
        }
        else if(a1[1][i]==12)
        {
            cout<<"Q"<<"\t";
            a1[2][i]=0.5;
        }
        else if (a1[1][i]==13)
        {
            cout<<"K"<<"\t";
            a1[2][i]=0.5;
        }
        else
        {
            a1[2][i]=a1[1][i];
            cout<<a1[i]<<"\t";
        }
        zong=zong+a1[2][i];
    }
    cout<<"一共"<<zong<<"点";
    cout<<endl;
}
float dian::fanhui()
{
    return zong;
}
int main()
{
    dian ren;
    ren.bigen();
    ren.yaopai();
}
2 回复
#2
mxs8102010-08-24 13:31
设置断点,单步跟踪,找到崩溃的位置,然后再处理问题~~~~
要掌握调试程序的基本方法~~

祝你好运
#3
tantan8212010-08-24 16:13
谢谢, 我已经找到呢~!
  数组越界了    忘记呢 a1[2][10]  是0开头的  没有2
1