写的猜数字游戏编译不通

cnyifeng 2006-02-09 09:19:56
GuessNum.h
#include "stdlib.h"
#include "time.h"
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;

/*
猜数字的游戏.要求如下:

规则:
电脑随机生成4个数,范围1-9,比如1234。
你猜4个数,当数字相同位置相同则为一个A,当数字相同位置不同则为一个B
比如你猜2345,则是3B
你猜1398,则是1A1B
限制次数,比如只让自己猜8次
*/

class GuessNum
{
public:
GuessNum(int times); // 猜测的次数
GuessNum();
int Compare(int the_number); // 返回匹配的结果,比如1A,1A1B等等.
int Compare(char number[]);

~GuessNum(void);
private:
int _times;
char _the_number[4];

static const int RANGE_MAX = 9999;
static const int RANGE_MIN = 1000;

};

GuessNum.cpp
#include "stdafx.h"
#include "GuessNum.h"

GuessNum::GuessNum(int times)
{
srand((unsigned)time(NULL));
// 让系统多循环几次,这样取出的随机数比较的随机.
int temp_value = 0;

for(int i = 0; i < 10; i++)
{
temp_value = (int)(((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
}

if(temp_value > 9999)
{
temp_value -= RANGE_MIN;
}

sprintf(_the_number, "%d", temp_value);

cout << "The number is " << _the_number << endl;

_times = times;
}

GuessNum::GuessNum()
{
GuessNum(8);
}

GuessNum::~GuessNum(void)
{

}

int GuessNum::Compare(int number)
{
if(number < 1000 || number > 9999)
{
cout << "您输入的数字" << number << "是不对的,请重新输入.\n数字要求在1000和9999之间" << endl;
return -1;
}

char compare_num[4];
sprintf(compare_num, "%d", number);

Compare(compare_num);

return 0;
}

int GuessNum::Compare(char number[])
{
string result = "";
int exactly_match_number = 0;
int match_number = 0;

for(int i = 0; i < 4; i++)
{
cout << _the_number[i] << endl;
}

//cout << "The number is " << _the_number << endl;

for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(number[i] == _the_number[j]) // 有数字的匹配
{
match_number++;
}
}
}

for(int i = 0; i < 4; i++)
{
if(number[i] == _the_number[i]) // 有完全的匹配
{
exactly_match_number++;
}
}

cout << "您输入的数字是'" << number << "',匹配的结果是 ";
if(exactly_match_number > 0)
{
cout << exactly_match_number << "A";
}

cout << match_number - exactly_match_number << "B";

cout << endl;

return 0;
}

main.cpp
#include "stdafx.h"
#include <string>
#include "GuessNum.h"

int main()
{
GuessNum* guess = new GuessNum();

cout << "请输入您想要猜测的数字" << endl;
int number;

for(int i = 0; i < 8; i++)
{
cin >> number;
guess->Compare(number);
}

}

这个程序没有办法运行.
目前遇到的问题是_the_number这个字符数组在构造函数里面赋值了之后,在Compare函数里面取_the_number值的时候确出现错误,这个问题我没有想明白.
究竟是在构造函数里面不允许这么赋值,还是有什么其他的问题?
...全文
206 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
du51 2006-02-09
  • 打赏
  • 举报
回复
#include "stdlib.h"
#include "time.h"
#include <iostream>
const int RANGE_MAX = 9999;
const int RANGE_MIN = 1000;
using namespace std;

/*
猜数字的游戏.要求如下:

规则:
电脑随机生成4个数,范围1-9,比如1234。
你猜4个数,当数字相同位置相同则为一个A,当数字相同位置不同则为一个B
比如你猜2345,则是3B
你猜1398,则是1A1B
限制次数,比如只让自己猜8次
*/

class GuessNum
{
public:
void layer(int times); // 猜测的次数
GuessNum();
int Compare(int the_number); // 返回匹配的结果,比如1A,1A1B等等.
int Compare(char number[]);

~GuessNum();
private:
int _times;
char _the_number[4];
char compare_num[4];
};

void GuessNum::layer(int times)
{
srand((unsigned)time(NULL));

int temp_value = 0;

for(int i = 0; i < 10; i++)
{
temp_value = (int)(((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
}

if(temp_value > 9999)
{
temp_value -= RANGE_MIN;
}
else if(temp_value<1000)
{
temp_value+=RANGE_MIN;
}

sprintf(_the_number, "%d", temp_value);

cout << "The number is " << _the_number << endl;

_times = times;
}

GuessNum::GuessNum()
{
layer(8);
}

GuessNum::~GuessNum(){}

int GuessNum::Compare(int number)
{
if(number < 1000 || number > 9999)
{
cout << "您输入的数字" << number << "是不对的,请重新输入.\n数字要求在1000和9999之间" << endl;
return -1;
}
_the_number;
//char compare_num[4];
sprintf(compare_num, "%d", number);

Compare(compare_num);

return 0;
}

int GuessNum::Compare(char number[])
{
int exactly_match_number = 0;
int match_number = 0;
int i,j;

//cout << _the_number <<"||||||||||"<< endl;

//cout << "The number is " << _the_number << endl;

for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
if(number[i] == _the_number[j]&&_the_number[j]-number[j]&&_the_number[i]-number[i]) // 有数字的匹配
{
match_number++;
}
}
}

for(i = 0; i < 4; i++)
{
if(number[i] == _the_number[i]) // 有完全的匹配
{
exactly_match_number++;
}
}

cout << "您输入的数字是'" << number << "',匹配的结果是 ";
if(exactly_match_number > 0)
{
cout << exactly_match_number << "A";
}

cout << match_number<< "B";

cout << endl;

return 0;
}

int main()
{
GuessNum guess;

cout << "请输入您想要猜测的数字" << endl;
int number;

for(int i = 0; i < 8; i++)
{
cin >> number;
guess.Compare(number);
}
system("PAUSE");
return 0;

}
/*
The number is 9094
请输入您想要猜测的数字
9490
您输入的数字是'9490',匹配的结果是 2A2B
9094
您输入的数字是'9094',匹配的结果是 4A0B
*/
cnyifeng 2006-02-09
  • 打赏
  • 举报
回复
问题不是出在sprintf上面的.这一点我可以肯定.
du51 2006-02-09
  • 打赏
  • 举报
回复
问题出在sprintf上吧.
cnyifeng 2006-02-09
  • 打赏
  • 举报
回复
"Stack around the variable 'compare_num' was corrupted".
这个错误又该如何的去解决?

在GuessNum.cpp中,变量i多次定义
这个应该是没有影响的,因为我都是在循环里面使用的,不会出错.

我用的是vc6.0,要去掉#include "stdafx.h"
这个也不是错误,在VS2005里面不加上这个是无法编译通过的.

下面这两条语句要用#define声明为常量:
static const int RANGE_MAX = 9999;
static const int RANGE_MIN = 1000;
这个我不清楚哪种写法更好一些,等我查查资料先.
sharpshootor 2006-02-09
  • 打赏
  • 举报
回复
你要以下断点调试,我的vc6.0是先空构造函数,然后是带参数的
分在cpp文件中的下面语名处设断点:
GuessNum* guess = new GuessNum();
GuessNum::GuessNum(int times)
GuessNum::GuessNum()
sharpshootor 2006-02-09
  • 打赏
  • 举报
回复
在GuessNum.cpp中,变量i多次定义
我用的是vc6.0,要去掉#include "stdafx.h"
下面这两条语句要用#define声明为常量:
static const int RANGE_MAX = 9999;
static const int RANGE_MIN = 1000;
cnyifeng 2006-02-09
  • 打赏
  • 举报
回复
To sharpshootor
请详细的指出错误,如果可能的话,提示一下如何的改正错误.谢谢

To MarcoCC(天蓝蓝,海蓝蓝)
我用的是VS2005 Express版.

程序我重新修改了之后是这样的
GuessNum.h
#include "stdlib.h"
#include "time.h"
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;

/*
猜数字的游戏.要求如下:

规则:
电脑随机生成4个数,范围1-9,比如1234。
你猜4个数,当数字相同位置相同则为一个A,当数字相同位置不同则为一个B
比如你猜2345,则是3B
你猜1398,则是1A1B
限制次数,比如只让自己猜8次
*/

class GuessNum
{
public:
GuessNum(int times); // 猜测的次数
int Compare(const int the_number); // 返回匹配的结果,比如1A,1A1B等等.
int Compare(const char number[]);

~GuessNum(void);
private:
int _times;
char _the_number[4];

static const int RANGE_MAX = 9999;
static const int RANGE_MIN = 1000;

};

GuessNum.cpp
#include "stdafx.h"
#include "GuessNum.h"

GuessNum::GuessNum(int times)
{
srand((unsigned)time(NULL));
// 让系统多循环几次,这样取出的随机数比较的随机.
int temp_value = 0;

for(int i = 0; i < 10; i++)
{
temp_value = (int)(((double) rand() / (double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
}

if(temp_value > 9999)
{
temp_value -= RANGE_MIN;
}

sprintf(_the_number, "%d", temp_value);

cout << "The number is " << _the_number << endl;

_times = times;
}

GuessNum::~GuessNum(void)
{

}

int GuessNum::Compare(const int number)
{
if(number < 1000 || number > 9999)
{
cout << "您输入的数字" << number << "是不对的,请重新输入.\n数字要求在1000和9999之间" << endl;
return -1;
}

char compare_num[4];
sprintf(compare_num, "%d", number);

Compare(compare_num);

return 0;
}

int GuessNum::Compare(const char number[])
{
string result = "";
int exactly_match_number = 0;
int match_number = 0;

cout << "The number is " << _the_number << endl;

for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
if(number[i] == _the_number[j]) // 有数字的匹配
{
match_number++;
}
}
}

for(int i = 0; i < 4; i++)
{
if(number[i] == _the_number[i]) // 有完全的匹配
{
exactly_match_number++;
}
}

cout << "您输入的数字是'" << number << "',匹配的结果是 ";
if(exactly_match_number > 0)
{
cout << exactly_match_number << "A";
}

if(match_number - exactly_match_number > 0)
{
cout << match_number - exactly_match_number << "B";
}

cout << endl;

return 0;
}

现在的程序运行了之后会告诉我
"Stack around the variable 'compare_num' was corrupted".
我想这个和内存没有回收是不是有关系?但是不知道应该在哪儿写回收的语句
sharpshootor 2006-02-09
  • 打赏
  • 举报
回复
有多处错误.
MarcoCC 2006-02-09
  • 打赏
  • 举报
回复
刚才调试的时候,我发现程序先是执行了带参数的初始化函数,接着又执行了不带参数的初始化函数.
我不理解为什么会是这样的,我的程序应该是先执行不带参数的初始化函数,然后才执行带参数的初始化函数的啊
------------------------------------------------------
搂住你用的是什么编译器?

按照main函数中的语句是决不会出现你说的这个情况的
MarcoCC 2006-02-09
  • 打赏
  • 举报
回复
不好意思,没看到程序中对输入数字的大小作了限制,因此错误原因还要再看看
cnyifeng 2006-02-09
  • 打赏
  • 举报
回复
To MarcoCC
我想问题不是出在你说的"声明的是char的数组,但是却往里面赋整形值,这必将导致非法内存操作"
上面,因为在赋值的过程中没有出现错误,之后也是可以正常的打印赋值后的_the_number值的.

问题应该是出在了我刚才所说的问题上.但究竟是为什么我就不明白了
cnyifeng 2006-02-09
  • 打赏
  • 举报
回复
刚才调试的时候,我发现程序先是执行了带参数的初始化函数,接着又执行了不带参数的初始化函数.
我不理解为什么会是这样的,我的程序应该是先执行不带参数的初始化函数,然后才执行带参数的初始化函数的啊
MarcoCC 2006-02-09
  • 打赏
  • 举报
回复
当然有问题。声明的是char的数组,但是却往里面赋整形值,这必将导致非法内存操作
cnyifeng 2006-02-09
  • 打赏
  • 举报
回复
谢谢各位的解答

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧