943,812 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3491
  • C++ RSS
Dec 13th, 2007
0

Char array, getting weird output

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. void getSent(char array[MAX])
  2. {
  3. char ch;
  4. int index=0;
  5.  
  6. cout << "Enter the palindrome: ";
  7.  
  8.  
  9. while ((ch = cin.get() != '\n') && (index < MAX-1))
  10. {
  11. array[index] = ch;
  12. index ++;
  13.  
  14. }
  15. array[index] = '\0';
  16. cin.ignore (1000, '\n');
  17.  
  18. }

Thanks,

Mitch
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
mb523 is offline Offline
31 posts
since Dec 2007
Dec 13th, 2007
0

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.
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Dec 13th, 2007
0

Re: Char array, getting weird output

Quote ...
C++ Syntax (Toggle Plain Text)
  1. while ((ch = cin.get() != '\n') && (index < MAX-1))
  2. {
  3. array[index] = ch;
  4. index ++;
  5.  
  6. }
  7. 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)

[edit]

Click to Expand / Collapse  Quote originally posted by Jishnu ...
Also, I think there should be || instead of && inside the condition of while loop.
Nope, it's good the way it is.
Last edited by Nick Evan; May 11th, 2010 at 9:05 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Dec 13th, 2007
0

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
C++ Syntax (Toggle Plain Text)
  1. array[index-1] = '\0';
and it pushed the word one letter back. Here is the entire code:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. //global constants
  6. const int MAX = 20;
  7.  
  8. //fuction prototypes
  9.  
  10. void getSent(char array[MAX]);
  11. bool palindrome(const char array[MAX]);
  12.  
  13.  
  14. int main()
  15. {
  16. char array[MAX];
  17.  
  18. getSent(array);
  19.  
  20. cout << array; //displays array for testing only
  21.  
  22. cout<< endl;
  23. if (palindrome(array))
  24. {
  25. cout << "\nThe phrase you entered is a palindrome!";
  26. }
  27. else
  28. {
  29. cout << "\nNOT";
  30. }
  31.  
  32. cout << endl;
  33.  
  34. return 0;
  35.  
  36. }
  37.  
  38.  
  39. //////////////////////////////////////////////////////////////////////////////////
  40. ///////////////////////////////Functions//////////////////////////////////////////
  41. //////////////////////////////////////////////////////////////////////////////////
  42.  
  43. void getSent(char array[MAX])
  44. {
  45. char ch;
  46. int index=0;
  47.  
  48. cout << "Enter the palindrome: ";
  49.  
  50.  
  51. while ((ch = cin.get() != '\n') && (index < MAX-1))
  52. {
  53. array[index] = ch;
  54. index ++;
  55.  
  56. }
  57. array[index] = '\0';
  58. cin.ignore (1000, '\n');
  59.  
  60. }
  61.  
  62. ///////////////////////////////////////////////////////////////////////////////
  63.  
  64. bool palindrome (const char array[MAX])
  65. {
  66. int end=0, start=0;
  67.  
  68. for (end=0; array[end] != '\0'; end++);
  69. end--;
  70.  
  71.  
  72. for (start=0; start < end;)
  73. {
  74. if (array[start] != array[end])
  75. {
  76. return false;
  77.  
  78. }
  79. start ++;
  80. end --;
  81.  
  82. }
  83.  
  84. return true;
  85. }

Mitch
Last edited by mb523; Dec 13th, 2007 at 2:09 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
mb523 is offline Offline
31 posts
since Dec 2007
Dec 13th, 2007
2

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:
C++ Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Dec 13th, 2007
0

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
Reputation Points: 10
Solved Threads: 0
Light Poster
mb523 is offline Offline
31 posts
since Dec 2007
Dec 14th, 2007
0

Re: Char array, getting weird output

The need to hit enter again is because of the line:
C++ Syntax (Toggle Plain Text)
  1. 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?
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: problem with knights
Next Thread in C++ Forum Timeline: Counter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC