不能在回文程序中输入多个字符串

Cannot input more than one string in palindrome program

本文关键字:字符串 输入 回文 程序 不能      更新时间:2023-10-16
#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] = '';
    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;
}

我只是想知道是否有人可以帮助我解释为什么我的代码不工作。

我可以运行它一次,我得到了正确的答案,但是当提示符问我是否要输入另一个字符串,我输入'y'时,提示符只是跳过输入并自行终止。

我尝试了cin.ginore('n', 80),但那只是给了我一堆空行。我在末尾添加了代码位来检查值,它们都转到0并删除字符串。

也许一个链接到一个适当的解释系统如何处理内存?

编辑:当第二次运行输入序列时,我一直得到同样的问题。输出如下所示:

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

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

我将从头开始重新构建程序,并尝试不使用函数。我仍然希望有一个页面的链接,解释如何使用现代c++处理用户输入。

问题在于:

cin >> response;

这将用户输入的y/n读入变量response,但在输入缓冲区中留下一个换行符,由getline函数和isPalindrome函数选择。

要解决这个问题,需要在读取用户响应后从输入缓冲区中删除换行符。您可以使用:

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

使用上面的修复,您可以重试一次回文检查。为了使多次重试成为可能,您需要一个循环。我建议在主程序中使用do-while循环:

char response;
do {
        isPalindrome();
        cout << "Input another string(y/n)?" << endl;
        cin >> response;
        std::cin.ignore(INT_MAX);
        response = toupper(response);
} while(response == 'Y');

你需要一个循环。没有代码指示程序返回到顶部。

char response = 'Y';
while (response == 'Y') {
    isPalendrome();
    cout << "Input another string(y/n)?" << endl;
    cin >> response;
}

这不是整个程序,只是while循环所需的关键元素。您应该了解while是如何工作的,并使它为您的程序工作。

在当代c++中,通常使用标准库组件进行字符串处理:

#include <iostream>
#include <string>
int main()
{
    std::string line1, line2, response;
    do
    {
        std::cout << "First string: ";
        if (!std::getline(std::cin, line1)) { /* error */ }
        std::cout << "Second string: ";
        if (!std::getline(std::cin, line2)) { /* error */ }
        // check line1 and line2 for palindromy
        std::cout << "Again (y/n)? ";
        std::getline(std::cin, response);
    } while (std::cin && (response == "y" || response == "Y"));
}