Using arrays and determining palindrones question.

This is a discussion on Using arrays and determining palindrones question. within the C++ Programming forums, part of the General Programming Boards category; i am testing five strings to see if they are palindrone. my program is running, but in my outfile, it ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    10

    Question Using arrays and determining palindrones question.

    i am testing five strings to see if they are palindrone. my program is running, but in my outfile, it is saying that everything is "Not Palindrone"....which isn't correct. and the strings look messed up when displayed also. i don't know what is wrong with my code, but i think my problem lies within my "for loop." i am trying to successfully reverse the arrays so it can compare oldstr and newstr. any help appreciated. thank you.

    Source Code:
    Code:
    #include<iostream.h>
    #include<fstream.h>
    #include<string.h>
    
    ofstream outfile;
    ifstream infile;
    
    void main()
    {
    
    infile.open ("rh1165p.dat");
    outfile.open ("out5.txt");
    
    char oldstr[26], newstr[26];
    
    while (infile)
    {
      infile.getline(oldstr,26);
      infile>>ws;
    
      outfile<<oldstr<<"   ";
      int counter=0;
    
      for (int psn=strlen(oldstr)-1; psn>=0; psn--)
      {
        counter++;
        newstr[counter]=oldstr[psn];
      }
      outfile<<newstr;
    
      if (strcmp(oldstr,newstr)==0)
        outfile<<" ==> Palindrone"<<endl<<endl;
      else
        outfile<<" ==> Not Palindrone"<<endl<<endl;
    }
    }
    Here is my "rh1165p.dat" file i am pulling from:

    152215
    dandy randy
    Able was I ere I saw elbA
    RemeR
    200211112002


    And here's my final output in my outfile so far:

    152215 «512251 ==> Not Palindrone

    dandy randy «ydnar ydnad ==> Not Palindrone

    Able was I ere I saw elbA «Able was I ere I saw elbA^A@^A ==> Not Palindrone

    RemeR «RemeRwas I ere I saw elbA^A@^A ==> Not Palindrone

    200211112002 «200211112002re I saw elbA^A@^A ==> Not Palindrone

    Code Tags added by Kermi3
    eskimos have feelings too...

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I'll just provide a fix to the code you presented. I'll let the other people complain about your algorithm, code tags(lack of), and void main hehe.

    Change this:
    Code:
    for (int psn=strlen(oldstr)-1; psn>=0; psn--)
    {
       counter++;
       newstr[counter]=oldstr[psn];
    }
    to
    Code:
    for (int psn=strlen(oldstr)-1; psn>=0; psn--)
    {
       newstr[counter]=oldstr[psn];
       counter++;
    }
    newstr[counter] = '\0';
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help. Effort well made with the bold though!

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    10
    Kermi3, thanks for the code tag advice. i was unaware of it. and thanks for the welcome!

    and Cshot, thank you so much for the help. i figured it was just one or two lines i had messed up.

    i guess my last question is, this does not display the new string (the reversed one). how would i add that? because an example of a line of output i have now is:

    RemeR ==> Palindrone

    i would like it to read:

    RemeR RemeR ==> Palindrone

    (the second "RemeR" being the reversed string)

    and about my algorithm, comments, void main, etc...i'm extremely new to C++ and guess dont know why void main is so bad. maybe in the future it will be bad to use? and my algorthm is just scratch notes on a piece of paper that i did not translate to this board. no comments is a bad thing, i know. my professor does not require them and has never even 'mentioned' them to us or how to use them effectively. i'll learn
    eskimos have feelings too...

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    10
    well, i figured out my solution to displaying newstr.

    outfile<<newstr;

    i feel dumb, but i guess this will be efficient for now and my program runs fine.

    thanks.
    eskimos have feelings too...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determining size of block of memory
    By ExxNuker in forum C Programming
    Replies: 9
    Last Post: 01-12-2006, 10:54 PM
  2. determining frequency
    By lakai02 in forum C Programming
    Replies: 4
    Last Post: 10-03-2002, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21