DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Char array, getting weird output (http://www.daniweb.com/forums/thread100989.html)

mb523 Dec 13th, 2007 10:49 am
Char array, getting weird output
 
Hello,

I am writing a program to determine if an inputted phrase or word is a palindrome. I am to use a separate function to input the string and another to check and see if it is a palindrome or not and return true or false. The first is a void function and the second, a bool. I have the array declared in main. After the first function (void) executes and the user enters the words, the output of the array in main is a line of smiley faces. This is only happens when the function writes to the array. When the array is declared and initialized with some letters, it displays fine in main. What is my issue here?

Here is the void function to get the user input and write it to a file. MAX = 20.

void getSent(char array[MAX])
{
        char ch;
        int index=0;

        cout << "Enter the palindrome: ";


        while ((ch = cin.get() != '\n') && (index < MAX-1))
        {
                array[index] = ch;
                index ++;

        }
        array[index] = '\0';
        cin.ignore (1000, '\n');

}

Thanks,

Mitch

Jishnu Dec 13th, 2007 11:17 am
Re: Char array, getting weird output
 
I can't see anything wrong in the function as such. Maybe something is wrong in the main() function where you are displaying the characters. Also, I think there should be || instead of && inside the condition of while loop. Do you mind posting the entire code?

Quote:

Here is the void function to get the user input and write it to a file.
I don't see it writing anything to any file. It is just modifying the character array.

niek_e Dec 13th, 2007 12:04 pm
Re: Char array, getting weird output
 
Quote:

        while ((ch = cin.get() != '\n') && (index < MAX-1))
        {
                array[index] = ch;
                index ++;

        }
        array[index] = '\0';

Your while-loop runs from 0 to MAX-1 (19). This is good because the array can contain 20 elements (0-19). But then you do this:
array[index] = '\0';
. But index now == MAX so in other words:
array[20] = '\0';
. This will try to write a \0 to element 20 which is the 21th element in the array get it? So not only are you trying to write to memory which is not 'yours', your string doesn't have an 'end of string character' (\0). This will result in memory exceptions and all kinds of other nasty things. (in your case: smilies)

Regards Niek

[edit]

Quote:

Originally Posted by Jishnu (Post 490393)
Also, I think there should be || instead of && inside the condition of while loop.

Nope, it's good the way it is.

mb523 Dec 13th, 2007 2:02 pm
Re: Char array, getting weird output
 
Thanks for the help so far guys!

I apologize, I meant write it to the array, not a file :$

niek_e, I understand what you mean. I tried to change it to
array[index-1] = '\0';
and it pushed the word one letter back. Here is the entire code:

#include <iostream> 

using namespace std;

//global constants
const int MAX = 20;

//fuction prototypes

void getSent(char array[MAX]);
bool palindrome(const char array[MAX]);


int main()
{
char array[MAX];

 getSent(array);

 cout << array; //displays array for testing only

 cout<< endl;
        if (palindrome(array))
        {
                cout << "\nThe phrase you entered is a palindrome!";
        }
        else
        {       
                cout << "\nNOT";
        }

cout << endl;

return 0;

}


//////////////////////////////////////////////////////////////////////////////////
///////////////////////////////Functions//////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////

void getSent(char array[MAX])
{
        char ch;
        int index=0;

        cout << "Enter the palindrome: ";


        while ((ch = cin.get() != '\n') && (index < MAX-1))
        {
                array[index] = ch;
                index ++;

        }
        array[index] = '\0';
        cin.ignore (1000, '\n');

}

///////////////////////////////////////////////////////////////////////////////

bool palindrome (const char array[MAX])
{
int end=0, start=0;

for (end=0; array[end] != '\0'; end++);
end--;


for (start=0; start < end;)
{
        if (array[start] != array[end])
        {
                return false;
               
        }
        start ++;
        end --;

}

return true;
}

Mitch

vmanes Dec 13th, 2007 2:23 pm
Re: Char array, getting weird output
 
In your reading loop, the input statement needs to be enclosed in parens before comparison to newline character, as so:
        while ( ( (ch = cin.get()) != '\n') && (index < MAX-1) )
Othewise, ch was not being assigned the character read in, but rather, the result of the comparison. Remember, assignment has a lower operator precedence than the comparison!

Val

mb523 Dec 13th, 2007 10:26 pm
Re: Char array, getting weird output
 
That did it, vmanes! Thank you, I would have never caught that small error.

Only one thing... it isn't keeping it from working, but it is a little annoying. After entering the text into the program, I hit enter and it takes the blinking curser to the next line and does nothing. I have to hit enter a 2nd time to get it to continue with the program. The rest of it executes perfectly.

Thank you all again, this is an awesome forum... I will be visiting more often to join in the fun! I love working on computers and build all my own.

Mitch

vmanes Dec 14th, 2007 12:49 am
Re: Char array, getting weird output
 
The need to hit enter again is because of the line:
cin.ignore (1000, '\n');
It's a tricky thing trying to deal with newline stuck in the input stream if the user types something more than the number of characters in your input. Taking input one character at a time doesn't make it any easier.

Is there some reason you can't just use getline and grab a bunch of input?


All times are GMT -4. The time now is 8:05 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC