#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <stdio.h>
using namespace std;
int second();
main(){
char myWord[5][20]={second()};
int x;
char c;
ifstream fin;
ofstream fout;
fin.open("Palinput.txt");
fout.open("Paloutput.txt");
for(int row=0;row < 5;row++)
{
for(int col=0;col < 20 ;col++){
cout<<myWord [row][col];
}
int i, j, flag=0, n ='\0'-1;
for (i=0, j=n; i<j; i++,j--)
statement:
if (myWord[i]!=myWord[j])
{
flag=0;
break;
}
else
{
flag=1;
}
if (flag)
cout<<myWord<<" pallindrome \n ";
else
cout<<myWord<<" not palindrome \n ";
}
return 0;
}
int second(){
ofstream thefile("Palinput.txt");
cout<<"Press enter after each word entered!\n";
cout<<"Enter 5 string \n"<<endl;
string txt;
int a;
for(a=0;a<=4;a++)
{
cin>>txt;
}
cout<<"Press ctrl+z to continue\n";
}
答案 0 :(得分:0)
如果你正确地缩进了东西,许多事情变得明显,并且更容易追踪正在发生的事情。看到我的一些评论我添加到下面的你的代码。但一般概述是你要求输入但从不存储它们,只是覆盖它们。然后你尝试检查一个空的char [] [],通过比较它的行与它的列来查看它是否是一个回文。您的代码存在重大问题。你应该使用像visual studio或gdb这样的调试器来逐步查看代码,看看发生了什么。这样你就可以理解你在代码设计中犯了什么错误。
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <stdio.h>
using namespace std;
int second();
main()
{
char myWord[5][20]={second()};//this isnt assigning to myWord
int x;
char c;
ifstream fin;
ofstream fout;
fin.open("Palinput.txt");//opening file but not doing
fout.open("Paloutput.txt");//anything with it
for(int row=0;row < 5;row++)
{
for(int col=0;col < 20 ;col++)
{
cout<<myWord [row][col];//myWord has nothing in it
}
int i, j, flag=0, n ='\0'-1;
for (i=0, j=n; i<j; i++,j--) //unused for loop
statement: //unused label
if (myWord[i]!=myWord[j])//how is a row=column? always true
{
flag=0;
break;
}
else
{
flag=1;
}
if (flag)
cout<<myWord<<" pallindrome \n ";
else
cout<<myWord<<" not palindrome \n ";
}
return 0;
}
int second()
{
ofstream thefile("Palinput.txt"); //file never opened
cout<<"Press enter after each word entered!\n";
cout<<"Enter 5 string \n"<<endl;
string txt;
int a;
for(a=0;a<=4;a++)
{
cin>>txt; //simply overwriting a variable with new input
//without using the input
}
cout<<"Press ctrl+z to continue\n";
}