0

I am trying to write a program where a user is asked for input and then I check if the 2 sub strings (splitting in half) is the mirror image. I want to loop continually until a user stops entering in input but my code is not looping correctly. The code works fine on the first iteration of the while loop, but then during the second iteration of the while loop, getline() does not re-request user input, it just sets std::string input to "".

I'm sure I am missing something obvious.

Code


#include <iostream>
#include <string>
#include <stack>
#include <algorithm>

using namespace std;

bool compareMirrorString(const std::string&, const std::string&);

int main()
{
    std::string input;
    string first_string;
    string second_string;
    bool result;
    int input_length;
    int first_string_length;
    std::string quit;

    cout << "Enter 2 strings to compare, seperated by a # e.g. \"abc#cba\"." << endl;

    while ( true )
    {
        std::getline(std::cin, input);

        //split string into its two component parts
        input_length = input.length();
        first_string_length = input_length / 2;
        first_string.assign(input, 0, first_string_length);
        second_string.assign(input, first_string_length + 1);

        //test if two strings are mirror images of each other

        result = compareMirrorString(first_string, second_string);

        if (result) {
            cout << "Yes they match.";
        }
        else {
            cout << "No they do not match.";
        }

        cout << "\nDo you want to test another string? Y for yes, q to quit." << endl;
        cin >> quit;
        if (quit == "q" or quit == "Q" or quit == "quit" or quit == "Quit" or quit == "QUIT" 
            or quit == "no" or quit == "No") 
        {
            break;
        }
        else 
        {
            cout << "Enter another 2 strings to compare, seperated by a # e.g. \"abc#cba\"." << endl;
        }

    } //end of while

    return 0;

}

//is second a mirror image of first?
bool compareMirrorString(const std::string& first, const std::string& second) 
{

    if (first.length() != second.length()) {
        return false;
    }

    //put first_string on stack    
    std::stack<char> stackChar;

    for (auto elem : first){
        stackChar.push(elem);
    }

    int size = stackChar.size();

    //compare first and second strings
    bool compare_equal = true;

    for (int i = 0; i < size; i++)
    {
        if (stackChar.top() == second[i])
        {
            stackChar.pop();
        }
        else
        {
            compare_equal = false;
            break;
        }  
    }

    return compare_equal;

}
Reno
  • 831
  • 3
  • 10
  • 18
  • Take a look at this question: https://stackoverflow.com/questions/33990640/c-getline-doesnt-get-the-input – Telescope Jun 23 '20 at 17:54

1 Answers1

1

In the first iteration of the loop:

cin >> quit;

will read up to, but not including the newline character.

In the second iteration of the loop:

std::getline(std::cin, input);

will read that newline character, and hence read an empty string into input.

There are several ways to fix this

  1. You could use getline to read the string quit

  2. You could read input again, if it happens to be an empty string.

  3. You could call cin.get(); after the cin >> quit to clear the newline.

cigien
  • 50,328
  • 7
  • 37
  • 78