I'm trying to make a Palindrome program, that basically returns whatever you enter, just backwards. For some reason, the cNewArray is not only couting the original array backwards, but also the original array. For example if I enter "Hello" it returns "Hello = olleHHello". I'm very new to referencing pointers and addressing and all that stuff, and I think that might be what's causing it. Can someone help?

#include <iostream>



int Absolute ( int nOrigNum )
{
    int nAbsolute = 0;
    if ( nOrigNum < 0 )
    {
        while ( nOrigNum < 0 )
        {
            nOrigNum++;
            nAbsolute++;
        }
        return nAbsolute;
    }
    else
    {
        return nOrigNum;
    }
}

int PosSwitch ( int nOrigPos )
{
    int nTempPos = nOrigPos - 10;
    nTempPos++;
    int nNewPos = Absolute ( nTempPos );

    return nNewPos;
}



void SwitchPrint ( char *cOrigArray, char *cNewArray )
{

    for ( int x = 0; x < 10; x++ )
    {
        if (!( ( cOrigArray [ x ] >= 65 && cOrigArray [ x ] <= 90 ) || ( cOrigArray [ x ] >= 97 && cOrigArray [ x ] <= 122 )  ))
        {
            cOrigArray [ x ] = ' ';
        }
    }


    for ( int i = 0; i < 10; i++ )
    {
        int j = PosSwitch ( i );
        if ( ( cOrigArray [ i ] >= 65 && cOrigArray [ i ] <= 90 ) || ( cOrigArray [ i ] >= 97 && cOrigArray [ i ] <= 122 )  )
        {
            cNewArray [ j ] = cOrigArray [ i ];
        }
        else
        {
            cNewArray [ j ] = ' ';
        }
    }


    std::cout << cOrigArray << " = " << cNewArray;
    std::cout << "\n" << "\n";

}


int main ( int argc, char** argv )
{
    char PalindromeArrayOrig [ 10 ];
    std::cout << "Enter a word, 10 letters or less." << "\n";

    std::cin >> PalindromeArrayOrig;

    char PalindromeArrayNew [ 10 ];
    SwitchPrint( PalindromeArrayOrig, PalindromeArrayNew );
    return 0;
}

havent really learned strings.. just the c style ones in arrays. and what kind of loop are you talking?

Although your code needs a lot of improvement a simple way to work around this is to end the cNewArray string with the escaped zero character:

...
...
...
int endPos = 0;
for ( int i = 0; i < 10; i++ )
    {
        int j = PosSwitch ( i );
        if ( ( cOrigArray [ i ] >= 65 && cOrigArray [ i ] <= 90 ) || ( cOrigArray [ i ] >= 97 && cOrigArray [ i ] <= 122 )  )
        {
            cNewArray [ j ] = cOrigArray [ i ];
        }
        else
        {
            cNewArray [ j ] = ' ';
        }
        endPos = i;
    }
[b]cNewArray[endPos + 1] = '\0';[/b]
...
...
...

I would also suggest you prompt the user enter 9 letters or less (you have to have in mind the terminal '\0' character), or you could increase the size of each array to 11. :)

Thank you so much! and what could I improve on? I've kind of gotten to the point where I understand the concepts of the basics and don't know where to go from here.. I got to pointers and references, and although I've read over it 300 times, I just don't know when to use them, or for that matter how. Any tips?

Well one sure hint for you is try to think less complex. Things that can get done the simple way, must be done so. You always have to examine your code and ask yourself: "Is there a more simple, cleaner and more direct way to do this?" If so, do it.
Look for example your Absolute function. It can get done as simple as:

unsigned int Absolute(int n) {
   if(n<0) 
      n *= (-1);
   return n;
}

I've noticed I'm terrible at that so far. My friend from school who knows it simplifies my code ALL the time. And thank you for all feedback!

This article has been dead for over six months. Start a new discussion instead.