You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++中noskipws生效但异常,空输入重复输出empty问题排查

回文检测程序输入为空时重复输出"empty"的问题解决

问题描述

我正在编写一个检测短语是否为回文的程序,要求当输入为空或仅含空白字符时输出"empty"。程序流程为:先输入待检测短语的数量(例如3),再依次输入待检测短语。程序在正常输入时运行良好,但当首次输入为空时,会反复输出"empty",而非继续请求下一个输入。

现有代码:

#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>

using namespace std;

int main() {

    int N;

    int isPalindromeCounter=0;
    int counter = 0;

    string inputString;
    cin >> N;
    cout << "\n";

    while (counter<N){
        counter++;

        cin.ignore(100, '\n');
        cin >> noskipws >> inputString;

        if (inputString.length()==0){
            cout <<"empty\n";
            continue;
        }

        int left = 0;
        int right = inputString.length()-1;

        if (inputString.length()>20){
            cout <<"error\n";
            continue;
        }

        bool isPalindrome = true;

        while (left<right){
            if (inputString[left] != inputString[right]){
                isPalindrome = false;
                cout << "no\n";
                break;
            }
            left++;
            right--;
        }

        if (isPalindrome){
            cout << "yes\n";
            isPalindromeCounter++;
        }
    }

    double percentage =  double(isPalindromeCounter)/double(counter)*100;
    if (trunc(percentage) != percentage){
        cout << setprecision(5) << percentage;
    }
    else {cout << percentage << ".000" ;}

    return 0;
}

错误原因

  1. 字符串残留问题:使用cin >> noskipws >> inputString时,若输入为空行(仅换行),inputString不会被更新,之前的空值会一直保留,导致后续循环每次都判定为空,重复输出"empty"。
  2. 输入流处理逻辑错误:cin.ignore(100, '\n')在第一次循环时会跳过输入N后的换行,但后续输入为空时,ignore和noskipws的组合会导致输入流状态异常,无法正确读取下一行。
  3. 空白字符判断缺失:当前代码仅判断字符串长度为0的情况,但用户要求"仅含空白字符"也输出"empty",而noskipws会把空白字符读入字符串,此时长度不为0,不符合需求。

修正方案

改用getline读取整行输入,同时每次循环前清空字符串,添加空白字符检测逻辑:

修正后的代码

#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include <limits>

using namespace std;

// 判断字符串是否全为空白字符
bool isAllWhitespace(const string& s) {
    for (char c : s) {
        if (!isspace(static_cast<unsigned char>(c))) {
            return false;
        }
    }
    return true;
}

int main() {
    int N;
    int isPalindromeCounter = 0;
    int counter = 0;
    string inputString;

    // 读取N后,彻底清空输入流中的换行,避免干扰后续getline
    cin >> N;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "\n";

    while (counter < N) {
        counter++;
        // 每次循环前清空字符串,避免旧值残留
        inputString.clear();
        // 读取整行输入,包含空行和空白字符
        getline(cin, inputString);

        // 判断空或全空白的情况
        if (inputString.empty() || isAllWhitespace(inputString)) {
            cout << "empty\n";
            continue;
        }

        // 长度超过20的错误处理
        if (inputString.length() > 20) {
            cout << "error\n";
            continue;
        }

        bool isPalindrome = true;
        int left = 0;
        int right = inputString.length() - 1;

        while (left < right) {
            if (inputString[left] != inputString[right]) {
                isPalindrome = false;
                cout << "no\n";
                break;
            }
            left++;
            right--;
        }

        if (isPalindrome) {
            cout << "yes\n";
            isPalindromeCounter++;
        }
    }

    double percentage = static_cast<double>(isPalindromeCounter) / counter * 100;
    // 统一格式输出三位小数
    cout << fixed << setprecision(3) << percentage;

    return 0;
}

关键修改点

  • 用getline(cin, inputString)读取整行,能正确捕获空行和含空白的行。
  • 新增isAllWhitespace函数,满足"仅含空白字符输出empty"的需求。
  • 读取N后用cin.ignore(numeric_limits<streamsize>::max(), '\n')彻底清空输入流,避免干扰后续getline。
  • 每次循环前调用inputString.clear(),确保旧值不会残留。
  • 修改百分比输出为fixed << setprecision(3),保证格式统一(如50%输出50.000)。

内容的提问来源于stack exchange,提问作者reconsidering my career

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 00:30:45