| | |
Char array, getting weird output
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
Thanks,
Mitch
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)
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
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?
I don't see it writing anything to any file. It is just modifying the character array.
•
•
•
•
Here is the void function to get the user input and write it to a file.
"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.
"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
•
•
•
•
C++ Syntax (Toggle Plain Text)
while ((ch = cin.get() != '\n') && (index < MAX-1)) { array[index] = ch; index ++; } array[index] = '\0';
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]
Nope, it's good the way it is.
Last edited by niek_e; Dec 13th, 2007 at 12:07 pm.
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 and it pushed the word one letter back. Here is the entire code:
Mitch
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)
array[index-1] = '\0';
C++ Syntax (Toggle Plain Text)
#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
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!
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!
In your reading loop, the input statement needs to be enclosed in parens before comparison to newline character, as so:
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
C++ Syntax (Toggle Plain Text)
while ( ( (ch = cin.get()) != '\n') && (index < MAX-1) )
Val
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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
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!
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!
The need to hit enter again is because of the line:
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?
C++ Syntax (Toggle Plain Text)
cin.ignore (1000, '\n');
Is there some reason you can't just use getline and grab a bunch of input?
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
- Another C++ N00B Begging For Help (C++)
- HELP!!! Weird problem... (C++)
- Graphics In Pixel: Part II B (C)
Other Threads in the C++ Forum
- Previous Thread: problem with knights
- Next Thread: Counter
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference simple string strings studio system temperature template templates test text text-file tree unix url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






