0

I am trying to skip the spaces in my code using getline();

I think I solved the spacing problem, but I'm trying to make the code check from the beginning of the word and the end of the word at the same time, so that when I type sentences like "ufo tofu" it will come back as a palindrome.

I've tried removing the spaces, but it only causes the system to return me an error.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main() {
    string userInput;
    int startInput;
    int endInput;
    bool isPalindrome = true;

    startInput = userInput.length();

    getline (cin, userInput);
    cin.ignore();

    for (int i = 0; i<(startInput/2); i++){
        if (userInput[i] != userInput[(startInput -1) -i])
            isPalindrome = false;
    }

    if (isPalindrome){
        cout << userInput << " is a palindrome" << endl;
    }
    else {
        cout << userInput << " is not a palindrome" << endl;
    }

    return 0;
}

I am trying to make the output come back as "is not a palindrome" when I submit my code to be graded.

These are the two errors that are coming back;

4: Compare output
0 / 2
Output differs. See highlights below. 
Special character legend
Input
statistics
Your output
statistics is a palindrome
Expected output
statistics is not a palindrome


6: Compare output
0 / 2
Output differs. See highlights below. 
Special character legend
Input
evil is alive
Your output
evil is alive is a palindrome
Expected output
evil is alive is not a palindrome
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Leo V.
  • 37
  • 1
  • 9
  • You could just ignore the spaces and only compare letters using an if statement. – Bayleef Jul 07 '19 at 15:28
  • sorry im new to coding, is their anyway you might be able to explain it to me just a bit more ? – Leo V. Jul 07 '19 at 15:32
  • `startInput = userInput.length();` needs to go after you've read some input, otherwise it is always 0. Once you fix that it works for me. You might think about if you need `cin.ignore();` or not. You shouldn't, and it will eat the first character of any additional input if you need to process more. Learning to step through code in a debugger would be a good thing to learn early and would have helped a lot here. – Retired Ninja Jul 07 '19 at 15:40
  • as @RetiredNinja. Then you may remove all spaces and continue with your code. see this, https://stackoverflow.com/a/83538/9748260 to remove white spaces from a string – Sohaib Jundi Jul 07 '19 at 15:47
  • @SohaibJundi I believe the OP was confused and thought the spaces were not being checked causing a palindrome with a space in it to be flagged as valid instead of invalid. The way the code is currently all input will be considered a palindrome since the input size is always 0 and the loop does not even run. No need to remove whitespace if I am reading the requirements correctly. – Retired Ninja Jul 07 '19 at 15:51
  • @RetiredNinja OP provided an example ("ufo tofu" it will come back as a palindrome), if spaces where not removed, "ufo tofu" would come as non-palindrome – Sohaib Jundi Jul 07 '19 at 15:54
  • yes i want sentences like "ufo tofu" and others like it to come back as as a palindrome currently it is coming back as not a palindrome – Leo V. Jul 07 '19 at 16:14
  • The code you have posted can never say anything other than palindrome. The for loop will never be entered because `startInput` will always be 0. Is this your real copy/pasted code? – Retired Ninja Jul 07 '19 at 16:25
  • its my real code but im a beginner and what do i change startInput to? – Leo V. Jul 07 '19 at 16:33
  • so i changed my code around to this; getline (cin, userInput); cin.ignore(); startInput = userInput.length(); – Leo V. Jul 07 '19 at 17:12
  • and not its showing the this error; 3: Compare output 0 / 1 Output differs. See highlights below. Special character legend Input never odd or even Your output never odd or even is not a palindrome Expected output never odd or even is a palindrome – Leo V. Jul 07 '19 at 17:13
  • Your palindrome loop could be replaced with `bool isPalindrome = std::equal(userInput.begin(), std::next(userInput.begin(), userInput.size() / 2), userInput.rbegin());` if you'd like to use standard library functions. – Ted Lyngmo Jul 07 '19 at 21:06

1 Answers1

0
string s;
do {
    getline(cin, s);
}while(s.empty());
s.erase((remove(s.begin(),s.end(),' ')),s.end());
cout<<s<<endl;

Let's say your string s is ufo tofu. It will after erasing all spaces become ufotofu. That way you can easily check if it's palindrome or not.

How does this thing work ?

  1. Well we declare a string called s. In that string, we will store our ufo tofu input.
  2. We use do-while loop to input our "sentence" into a string. We could use just getline(cin, s);, but if you pressed enter-key once, it would stop your program.
  3. Next thing, we use function combination of functions remove and erase: As the beginning parameter we use function remove, which finds all the spaces in the string, pushes them to the end of the container (in our case string s), and returns beginning iterator of that "pushing", and the second parameter tells us to remove every single element of container from that beginning iterator to the end.
  4. We just print out the string, but now without spaces!

I think this is really simple way to do it, but if it can't be useful to you, I am sorry for wasting your time reading all of this! :)

SomeName
  • 909
  • 2
  • 8
  • 15
  • Note that your loop will fail to handle the case where the user types in ONLY spaces and then presses ENTER. You might consider using `getline(cin >> ws, ...)` to ensure the user types in at least 1 non-whitespace char. – Remy Lebeau Jul 07 '19 at 18:23
  • I didn't want to go that deep and secure since he is doing just palindromic stuff. – SomeName Jul 07 '19 at 18:26