Hello! I have an assignment in my "intro to programming class" and I am having trouble just getting started.

We are suppose to take a txt file that has several lines of text - and create a program using "fin" to test each line to determine if it is a palindrome or not.

So far I have only the very beginning....here is my code

#include <iostream>
#include <string>
#include <fstream>
using namespace std; 
int main() 
{
  ifstream fin;
  fin.open("palindromes.txt");
  if (!fin.good ()) throw "I/O error";
  
return 0;
}

I have no idea how to create a loop that will check for palindromes. Can someone give me pointers on how to get started in order to do that?

Thank you,
confused!

Recommended Answers

All 8 Replies

Use this to figure out if the number (string) is a palindrome or not...
the rest, the file handling, try doing it by yourself, it is very easy.. if you have troubles doing that, let me know. :icon_wink:

#include <iostream>
#include <string>
using namespace std;


int main ()
{   
    int x;
    string n,mem,inverse="";           //inverse is empty at the begining..
    cin >> n;                     // enter the number we want to check if it is polindrome or not
    mem=n;                 // store n in mem,, you will needed later...
    x=n.size();            // get the size of the string and store it in the int x

   while (x>0)  
   {
       inverse=inverse+n.substr(x-1);             // get's the last digit and puts it in the new string 'inverse'
       n.erase(x-1,x);                   //erase the last digit... that's why we needed 'mem'
           x--; // loop stuff
   }

    cout << inverse << endl;                // not necessary. 

   if (mem == inverse)                 // if the entered string is the same as the inverse, then it is "palindromic"
                                           // some examples...  123321 or ada etc.                                                                                 
   {
       cout << "The number is palindromic";
   }


    return 0;
}

/*****************************************************************
**The program reverses the string that was inputed by the user.
** tells us if it is polindromic or not.
**Haji Akhundov. (c) All rights reserved.:)
******************************************************************/

I am not showing-off, But just want to tell that how can STL come to your rescue.
The program submitted by Haji Akhundov, Can be shorten to few lines only.

#include<iostream>
#include<string>
#include<algorithm>
int main()
{
    std::string orig, rev;
    std::cin>>orig;
    rev=orig;
    std::reverse(rev.begin(), rev.end() );
    if (rev==orig)
        std::cout<<"Yes";
    else
        std::cout<<"No";
}

And I'd like to point out that hajiakundov and siddhant3s have both not helped confused! to learn. Giving answers only gets the assignment done, but does not promote learning how to do the assignment.

Please read the stickies at the top of this forum.

>And I'd like to point out that hajiakundov and siddhant3s have both not helped >confused! to learn. Giving answers only gets the assignment done, but does >not promote learning how to do the assignment.

I agree. Neither do I like it. Read My post about it.
http://www.daniweb.com/forums/post813528.html#post813528
These are few quotes from in there:

Please note:
I know no one gets paid to help here. I even know that you can debug the flawed code very quickly and repost it. But the real pain lies in explaining the things to the new-bie so that he can get meaning to your code.

I was just trying to point to Haji Akhundov's post, that the things could be done in much simple manner with STL in hand. I was trying to show, the number of lines which gets reduced while using STL

And I'd like to point out that hajiakundov and siddhant3s have both not helped confused! to learn. Giving answers only gets the assignment done, but does not promote learning how to do the assignment.

Please read the stickies at the top of this forum.

Vmanes >> The thread was posted on Nov 26th, 2006... I think he completed the assignment by today? Don't you think so? :)

siddhant3s>> Thanks man, I'll take a look at your code ;)

Please don't reply to old posts, it does tend to confuse the situation. If you have a question regarding an old post, start a new thread, and feel free to refer to the old one.

That way near-sighted old farts like me don't get all riled up. ;)

After all, you did write as if responding to the OP, who, as you said, has probably long completed the assignment.

ok, i got what you saying)
but note, when people google these kind of things, they get the links to this kind of threads... and I wouldn't propably figure out how to shorten this program using STL. :)
So, I think that everything is cool)
take care.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.