我必须使用二维数组检查回文,我使用了文件但它不起作用
I've got to check palindrome using 2D array,i've used files but its not working
#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 ='[=10=]'-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";
}
如果你适当地缩进,很多事情就会变得很明显,并且更容易跟踪正在发生的事情。请参阅我在下面添加到您的代码中的一些评论。但总体概述是您要求输入但从不存储它们,只是简单地覆盖它们。然后你尝试通过比较它的行和列来检查一个空的 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 ='[=10=]'-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";
}
#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 ='[=10=]'-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";
}
如果你适当地缩进,很多事情就会变得很明显,并且更容易跟踪正在发生的事情。请参阅我在下面添加到您的代码中的一些评论。但总体概述是您要求输入但从不存储它们,只是简单地覆盖它们。然后你尝试通过比较它的行和列来检查一个空的 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 ='[=10=]'-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";
}