The assignment is to write a program that does the following. You will need to define extra functions to
complete some of these tasks such as the first two bullet items:
Prompts the user to type in a character string
Gets the characters and stores them in a stack data structure.
Makes use of the stack and a queue data structure to check for a palindrome.
Defines a function “isPalindrome” that determines whether the string is a palindrome. The function
“isPalindrome” returns 1 if x is a palindrome, and 0 if x is not a palindrome.
int isPalindrome(char *x)
Displays an appropriate message on the screen that indicates the string and whether the string is a
palindrome or not.
I have come up with the following code, but when I enter a string I get the error "Program5.exe has stopped working". Please help :-)
Hi! Was this made in a development environment? I'm trying to follow the #include statements to files but you've only posted the .cpp's without the .h files - which would be crucial to understanding what's going on. Splitting the posted code into the two named files doesn't help resolve the problem.
Can you post your .h files as well, if there are any in the directory that you got the code you wrote from?
$ g++ main.cpp palindrome.cpp -o main
palindrome.cpp:4:26: fatal error: isPalindrome.h: No such file or directory
#include "isPalindrome.h"
^
compilation terminated.
Believing that to be a typo I removed the "is" and it compiled without further errors or warnings, giving me a main.exe...
~/src/forums/mysiarobin1987$ ./main
Enter a char string
nope
~/src/forums/mysiarobin1987$ ./main
Enter a char string
nun
It's running.. It's not producing any output after input however, because gets is never coming back when used that way in main.cpp - a cout statement placed right after the gets confirms this. the program dies at gets. don't use gets.
As long as I can see, if you follow the assignment instruction, you should have a directory with these files:
Queue.h
Stack.h
palindrome.h
palindrome.cpp
main.cpp
The compilation instruction should resemble the following:
g++ -std=c++14 -Wall -Wextra -pedantic-errors palindrome.cpp main.cpp -o main.exe
In your code there’re things I can’t understand.
1) The assignment logic is pretty simple:
a) get a string
b) push every character into *both* a stack *and* a queue
c) pop back the characters from *both* the stack *and* the queue and compare them
d) since one container is FILO and the other is FIFO, what comes out is one character from the rear of the string and one from the front
e) so, if there’re no differences between the two sequences, the string was palindrome.
May I ask you what logic did you follow?
2) The assignment asks you to read a char string from console, presumably spaces included.
What instructions do you know which do that? Do you know about std::getline()?
I’ve noticed you included <string> and used std::cout...
Please consider:
in main():
- ask the user for a string
- obtain a null-terminated C-style char string from it
- pass that C-style char string to isPalindrome()
- valuate the return value
in isPalindrome():
- get the length of the passed C-style char string
- create a stack and a queue based on that length
- in a loop, check every character of the C-style char string and, if it is a letter, make it uppercase (or lowercase) and push it into the stack and the queue
- [suggestion: record the number of the pushed characters]
- once finished, start another loop, that will perform as many iteration as character you’ve pushed
- at every iteration, get back a character from the stack and one from the queue
- as soon as they don’t match, return the code for “not palindrome”
- if all them match, return code for “palindrome”
- make your life easy