Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6845937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:33:44+00:00 2026-05-27T00:33:44+00:00

#include <iostream> #include <ctype.h> using namespace std; void isPalindrome(); int main() { char response;

  • 0
#include <iostream>
#include <ctype.h>

using namespace std;

void isPalindrome();

int main()
{
    char response;
    isPalindrome();
    cout << "Input another string(y/n)?" << endl;
    cin >> response;
    response = toupper(response);
    if (response == 'Y')
        isPalindrome();

    return 0;
}

void isPalindrome()
{
    char str[80], str2[80];
    int strlength;
    int j = 0;
    int front, back;
    bool flag = 1;

    cout << "Input a string:" << endl;
    cin.getline(str, 80);
    strlength = strlen(str);

    for (int i = 0; i < strlength; i++)
    {
        if (islower(str[i]))
            str[i] = toupper(str[i]);
    }

    for (int i = 0; i < strlength; i++)
    {
        if (isalpha(str[i]))
        {
            str2[j] = str[i];
            j++;

        }
    }

    str2[j] = '\0';
    front = 0;
    back = strlength - 1;

    for (int i = 0; i < j / 2; i++)
    {
        if (str2[front] != str2[back])
        {
            flag = 0;

            break;
        }
    }

    if (!(flag))
        cout << "It is not a palindrome" << endl;
    else
        cout << "It's a palindrome" << endl;

    cout << "str: " << str << " str2: " << str2 << " strlength: " << strlength << " j: " << j << endl;
    cout << "front: " << front << " back: " << back << " flag: " << flag << endl;
}

I was just wondering if anybody could help explain to me why my code isn’t working.

I can run it once just fine and I get the right answer, but when the prompt asks if I want to input another string and I type 'y', the prompt just skips over the input and terminates on it’s own.

I tried cin.ginore('\n', 80), but that just gave me a bunch of blank lines. I added the bit of code at the end to check the values and they all go to 0 and drop the strings.

Maybe a link to a proper explanation of how the system handles memory?

edit:
I keep getting the same problem when running the input sequence a second time. The output looks like this:

    Input a string:
    Radar
    It's a palindrome
    Input another string(y/n)?
    y


    _ <- this being my cursor after pressing enter 3 times

I’ll just re-build the program from scratch and try to do it without a function. I’d still appreciate a link to a page that explains how to process user input using modern c++.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:33:45+00:00Added an answer on May 27, 2026 at 12:33 am

    The problem is with:

    cin >> response;
    

    This reads the user input y/n into the variable response but a newline is left in the input buffer which is picked by the getline function the isPalindrome function.

    To fix this you need to remove the newline from the input buffer after you read the user response. You do it by using:

    cin >> response;
    std::cin.ignore(INT_MAX);
    

    With the above fix you can retry the palindrome check just once. To make multiple retries possible you’ll need a loop. I would recommend a do-while loop in your main as:

    char response;
    do {
            isPalindrome();
            cout << "Input another string(y/n)?" << endl;
            cin >> response;
            std::cin.ignore(INT_MAX);
            response = toupper(response);
    } while(response == 'Y');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; int main() { double u = 0; double w
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include iostream #include string using namespace std; #define AA(bb) \ string(::##bb); int main (int
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
#include <iostream> #include <cctype> // isdigit using namespace std; // Global buffer const int
#include<iostream> using namespace std; class Abs { public: virtual void hi()=0; }; class B:public
#include <iostream> using namespace std; class Imp { public: int X(int) {return 50;} int
#include iostream #include vector using namespace std; const vector<int>& Getv() { vector<int> w(10); w[0]=10;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.