Search code examples
c++c++11palindrome

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 ='\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";
}

Solution

  • if you properly indent things, many things become apparent, and it is much easier to track what is going on. see some of my comments i added to ur code below. but general overview is that you ask for inputs but never store them, simply overwrite them. then you try to check an empty char[][] to see if its a palindrome by comparing its rows to its columns. your code has significant problems. you should use a debugger like visual studio or gdb to step through your code to see what is happening. so you can understand what errors you made in the design of your code.

    #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";
    }