r/programminghomework • u/RS7theDream • Jan 13 '17
How can I cin.getline without a constant array size in c++?
Hi. This is a homework I have to check if an input is a palindrome. Palindrome means it spells out the same regardless if your read from left to right or vice versa. Example BOB, A TOYOTA, SOS are palindrome DOG FOG are not palindrome.
The problem I am having is - I see the only way to put letters into an array is using cin.getline(ch, size) and size must be const else it won't run. Basically, my question is if there is any way to input characters into an array of the fixed size the user enters?
Example: user enters dog. now the array size is 2(thus having 3 slots [0][1][2].)
Code: Code in Pastebin
Thank you. What I implemented in the isPalindrome bool fuction is- compare first array value against the last array value
I don't want the answers, I am looking for how I can input a user-input into a character array with a custom non-const size. Even a link is appreciated if the result is there. I tried stackoverflow and other searches with no luck; the examples that I saw from it wouldn't have made a difference in the result regardless of the array size.
2
u/whereisbill Jan 14 '17
First check what functions your teacher has shown you.
I was once marked down for using features not covered in class.
Googling for answers takes practise. You will find solutions faster.
Would std::getline work for you?
2
u/[deleted] Mar 07 '17
Push your input into a
queuestack, then pop it. Aqueuestack is Last-In-First-Out, so the last letter you insert is the first letter that will come out.From there you can do a simple comparison to check if your output is the same as your input. If so, it is a palindrome, if not, welp.
edit: brainfart