C++问题:程序编译通过,但是无法执行

题目是编写程序提示输入2个字符串,看其中一个是否为另一个字母颠倒顺序得到的。
#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
main()
{
string text1;
string text2;
int count=1;

cout<<"输入字符串1以#结束"<<endl;
std::getline(cin,text1,'#');
cout<<"输入字符串2以#结束"<<endl;
std::getline(cin,text2,'#');
if(sizeof text1/sizeof text1[0]==sizeof text2/sizeof text2[0])
{
for(int j=0;j<sizeof text1/sizeof text1[0];j++)
{
if(text1[j]=text2[sizeof text1/sizeof text1[0]])
{

count++;
}
}
}
else
cout<<"no"<<endl;
if(count==sizeof text1/sizeof text1[0])
cout<<"yes"<<endl;
else
cout<<"no"<<endl;

return 0;
}

#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
main()
{
string text1;
string text2;
int count=1; //============这里改为:count=0;

cout<<"输入字符串1以#结束"<<endl;
std::getline(cin,text1,'#');
cout<<"输入字符串2以#结束"<<endl;
std::getline(cin,text2,'#');
if(sizeof text1/sizeof text1[0]==sizeof text2/sizeof text2[0])
{
for(int j=0;j<sizeof text1/sizeof text1[0];j++)
{
if(text1[j]=text2[sizeof text1/sizeof text1[0]]) //==========>>>这里应该改为:======>>>>
//=====>>>>if(text1[j] == text2[sizeof text1/sizeof text1[0] - j - 1])
{
count++;
}
}
}
else //=====================>>>>这句和下一句可以去掉,只要用count变量判断就可以了
cout<<"no"<<endl;//=========>>>>这句和上一句一起去掉

if(count==sizeof text1/sizeof text1[0])
cout<<"yes"<<endl;
else
cout<<"no"<<endl;

return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2006-11-14
#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
main()
{
string text1;
string text2;
int count=0;

cout<<\"输入字符串1以#结束\"<<endl;
std::getline(cin,text1,\'#\');
cout<<\"输入字符串2以#结束\"<<endl;
std::getline(cin,text2,\'#\');

int Len1 = text1.size();
int Len2 = text2.size();
if(Len1==Len2-1)
{
for(int j=0;j<Len1;j++)
{
if(text1[j]==text2[Len1-j])
{
count++;
}
}
}
else
cout<<\"no\"<<endl;

if(count==Len1)
cout<<\"yes\"<<endl;
else
cout<<\"no\"<<endl;

return 0;
}

程序有几个特别注意的地方:
1.得出string的长度可以调用方法size(),不要用sizeof()/sizeof();
2.输入的两个字符串有点问题,输入text1时候必须回车后确认输入,而这个回车进入了text2的内容里,text2的长度始终要长一个单位
3.比较时候采用if(text1[j]==text2[Len1-j])比较,逆序排列
第2个回答  2006-11-14
std::getline(cin,text1,'#'); 这个函数有问题,我跟踪过了, 把它改了就可以, 你的这个函数获得的数据大小比你需要的数据要大,你可以跟踪一下看看,