当我输入"..."时,用户输入不起作用

Userinput doesn't work when I enter "..."

本文关键字:输入 用户 不起作用      更新时间:2023-10-16

我正在做一个单词和向后的莫尔斯。除了一种情况外,程序正常工作,当我在用户输入中键入"…",然后按空格时,它会变得非常小,程序不会返回任何内容。例如,如果我尝试打字。。。返回S是有效的,但如果我尝试键入。。。对于SS来说,它不起作用。我使用stanford库来获取用户输入和处理映射,但当我使用标准库时也会发生同样的事情。

#include <iostream>
#include <string>
#include "console.h"
#include "random.h"
#include "map.h"
#include "simpio.h"
using namespace std;

int main() {
    string input = getLine("Please enter words or morse code");
    Map<string, string> toMorse;
    toMorse.put("A", ".-");
    toMorse.put("B", "-...");
    toMorse.put("C", "-.-.");
    toMorse.put("D", "-..");
    toMorse.put("E", ".");
    toMorse.put("F", "..-.");
    toMorse.put("G", "--.");
    toMorse.put("H", "....");
    toMorse.put("I", "..");
    toMorse.put("J", ".---");
    toMorse.put("K", "-.-");
    toMorse.put("L", ".-..");
    toMorse.put("M", "--");
    toMorse.put("N", "-.");
    toMorse.put("O", "---");
    toMorse.put("P", ".--.");
    toMorse.put("Q", "--.-");
    toMorse.put("R", ".-.");
    toMorse.put("S", "...");
    toMorse.put("T", "-");
    toMorse.put("U", "..-");
    toMorse.put("V", "...-");
    toMorse.put("W", ".--");
    toMorse.put("X", "-..-");
    toMorse.put("Y", "-.--");
    toMorse.put("Z", "--..");
    Map<string, string> toSentence;
    for(char c0='A'; c0<='Z'; c0++)
    {
        string c="";
        c.append(1, c0);
        //cout<<toMorse.get(c)<<endl;
        toSentence.put(toMorse.get(c), c);
    }
    if(input[0]=='.' || input[0]=='-')
    {
        string toLetter;
        for(int i=0; i<input.length(); i++)
        {
            if(input[i] != ' ' && i<input.length()-1)
            {
                toLetter.append(input.substr(i, 1));
            }
            else if(input[i] != ' ' && i==input.length()-1)
            {
                toLetter.append(input.substr(i, 1));
                cout << toSentence.get(toLetter);
            }
            else
            {
                cout << toSentence.get(toLetter);
                toLetter = "";
            }
        }
    }
    else
    {
        for(int i=0; i<input.length(); i++)
        {
            if(toMorse.containsKey(input.substr(i,1)))
            {
                cout << toMorse.get(input.substr(i,1)) << " ";
            }
        }
    }
   return 0;
}

听起来你的控制台正在将3个句点更改为省略号运行,就像文字处理程序可能会做的那样。但不知道如何解决这个问题,除非扫描Unicode或控制台正在创建的任何值:)

您的控制台"非常有用"地将unicode标准允许的三个句点字符(…)转换为省略号(…)。由于您使用的是std::string(我假设是linux,因为Windows不这么做),所以它必须转换为UTF-8。unicode字符是代码点U+2026,在UTF-8中是0xE2 0x80 0xA6,或者作为cstring "xE2x80xA6"

来源:"Unicode将一系列三句点字符(U+002E)识别为与水平省略号字符的兼容性等效字符(尽管不是规范字符)。"-http://en.wikipedia.org/wiki/Ellipsis

我已经遍历并在必要时用std::classes替换了你的东西,对我来说,对输入字符串"……"进行硬编码会得到"SS"的输出,所以你的实际莫尔斯翻译是可以的(假设你想让它放弃空格),但用cin捕获字符串会截断空格处的输入。