Char array, getting weird output

Reply

Join Date: Dec 2007
Posts: 31
Reputation: mb523 is an unknown quantity at this point 
Solved Threads: 0
mb523's Avatar
mb523 mb523 is offline Offline
Light Poster

Char array, getting weird output

 
0
  #1
Dec 13th, 2007
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: Char array, getting weird output

 
0
  #2
Dec 13th, 2007
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?

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.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,752
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 294
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Maven

Re: Char array, getting weird output

 
0
  #3
Dec 13th, 2007
  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)

Regards Niek

[edit]

Originally Posted by Jishnu View Post
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 niek_e; Dec 13th, 2007 at 12:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 31
Reputation: mb523 is an unknown quantity at this point 
Solved Threads: 0
mb523's Avatar
mb523 mb523 is offline Offline
Light Poster

Re: Char array, getting weird output

 
0
  #4
Dec 13th, 2007
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
  1. array[index-1] = '\0';
and it pushed the word one letter back. Here is the entire code:

  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.
C++ noob!

Custom built: AMD X2 5000+ Black Edition @3.1GHZ w/ AC Freezer 64 PRO, 2GB Corsair XMS2 DDR2, 320GB SATAII HDD, XFX GeForce 8600GT XXX w/ SilenX cooler, 500W Ultra PSU, dual 19" LCD's, Vista Business and XP Pro on dualboot!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,670
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 192
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Char array, getting weird output

 
2
  #5
Dec 13th, 2007
In your reading loop, the input statement needs to be enclosed in parens before comparison to newline character, as so:
  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
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 31
Reputation: mb523 is an unknown quantity at this point 
Solved Threads: 0
mb523's Avatar
mb523 mb523 is offline Offline
Light Poster

Re: Char array, getting weird output

 
0
  #6
Dec 13th, 2007
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
C++ noob!

Custom built: AMD X2 5000+ Black Edition @3.1GHZ w/ AC Freezer 64 PRO, 2GB Corsair XMS2 DDR2, 320GB SATAII HDD, XFX GeForce 8600GT XXX w/ SilenX cooler, 500W Ultra PSU, dual 19" LCD's, Vista Business and XP Pro on dualboot!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,670
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 192
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Char array, getting weird output

 
0
  #7
Dec 14th, 2007
The need to hit enter again is because of the line:
  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?
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC