从语法来讲 ,问题只有一个,函数void totle() 中的for(int i=0....),这样i且的生命周期只有在这个for语句中,而之后你还在for{}结束之后使用i,错误.
解决方法,把i的生命周期变为整个void totle()函数或,把其后的赋值移动到for语句中.
这个程序不是楼主写的吧,看时间是07年12月的
原来的调试环境应该是vc++6.0,所以才会出现这个问题,vc6.0比较老啦
还有一个不是问题的问题 就是你如此频繁的使用cin函数,那么记得在每次使用结束之后 都要加上一句cin.clear();
否则前一次的输入可能会影响到后边的结果
#include
#include
using namespace std;
struct SCORC
{
signed int len;
signed int wrong;
signed int right;
double time;
};
SCORC scorc={0,0,0,0.0};
static char* txt=NULL;
static char* input=NULL;
void totle()
{
scorc.right=0;
scorc.wrong=0;
int i;
for (i=0; i
if ('\0'==txt[i])
break;
if (txt[i]==input[i])
scorc.right++;
else
scorc.wrong++;
}
scorc.len=i;
}
int main()
{
cout<<"----------------------------------------------------------------------------"<
char exit='\0';
cout<<"是否重新开始?(任意键开始!Ctrl+Z退出!)"<
{
cout<<"请输入目标文本估计长度: ";
cin>>scorc.len;
cin.clear();
txt=new char[scorc.len];
memset(txt, 0, scorc.len);
input= new char[scorc.len];
cout<<"输入什么字符时结束文本输入(建议使用 # ): ";
char ch;
cin>>ch;
cin.clear();
cout<<"---开始录入目标文本---"<
cin.getline(txt,scorc.len,ch);
cin.clear();
cout<<"---目标文件录入完毕。"<
cin.get();
while (10!=cin.get());
cout<<"-------------------------------------------------------------------------"<
cin.getline(input,scorc.len,ch);
clock_t finish= clock();
scorc.time=double((finish-start))/CLOCKS_PER_SEC;
totle();
cout<<"---测试完成!统计---"<
}
system("pause");
return 0;
}
程序呢?
#include
#include
using namespace std;
struct SCORC
{
signed int len;
signed int wrong;
signed int right;
double time;
};
SCORC scorc={0,0,0,0.0};
static char* txt=NULL;
static char* input=NULL;
void totle(void)
{
scorc.right=0;
scorc.wrong=0;
for (int i=0; i
if ('\0'==txt[i])
break;
if (txt[i]==input[i])
scorc.right++;
else
scorc.wrong++;
}
scorc.len=i;
}
void main()
{
cout<<"----------------------------------------------------------------------------"<
char exit='\0';
cout<<"是否重新开始?(任意键开始!Ctrl+Z退出!)"<
{
cout<<"请输入目标文本估计长度: ";
cin>>scorc.len;
txt=new char[scorc.len];
memset(txt, 0, scorc.len);
input= new char[scorc.len];
cout<<"输入什么字符时结束文本输入(建议使用 # ): ";
char ch;
cin>>ch;
cout<<"---开始录入目标文本---"<
cin.getline(txt,scorc.len,ch);
cout<<"---目标文件录入完毕。"<
while (10!=cin.get());
cout<<"-------------------------------------------------------------------------"<
cin.getline(input,scorc.len,ch);
clock_t finish= clock();
scorc.time=double((finish-start))/CLOCKS_PER_SEC;
totle();
cout<<"---测试完成!统计---"<
}
}
刚刚忘记把写好的程序发上去了,现在补一下...谢谢
#include
#include
using namespace std;
struct SCORC
{
signed int len;
signed int wrong;
signed int right;
double time;
};
SCORC scorc={0,0,0,0.0};
static char* txt=NULL;
static char* input=NULL;
void totle(void)
{
scorc.right=0;
scorc.wrong=0;
int i=0;
for (; i
if ('\0'==txt[i])
break;
if (txt[i]==input[i])
scorc.right++;
else
scorc.wrong++;
}
scorc.len=i;
}
int main()
{
cout<<"----------------------------------------------------------------------------"<
char exit='\0';
cout<<"是否重新开始?(任意键开始!Ctrl+Z退出!)"<
{
cout<<"请输入目标文本估计长度: ";
cin>>scorc.len;
txt=new char[scorc.len];
memset(txt, 0, scorc.len);
input= new char[scorc.len];
cout<<"输入什么字符时结束文本输入(建议使用 # ): ";
char ch;
cin>>ch;
cout<<"---开始录入目标文本---"<
cin.getline(txt,scorc.len,ch);
cout<<"---目标文件录入完毕。"<
while (10!=cin.get());
cout<<"-------------------------------------------------------------------------"<
cin.getline(input,scorc.len,ch);
clock_t finish= clock();
scorc.time=double((finish-start))/CLOCKS_PER_SEC;
totle();
cout<<"---测试完成!统计---"<
}
system("pause");
return 0;
}