输入“...”时,用户输入不起作用

时间:2012-02-09 16:46:44

标签: c++ console-application

我对言语和倒退做了莫斯。程序工作除了一种情况,当我在用户输入中键入“...”然后按空格它变得非常小,程序不返回任何东西。因此,例如,如果我尝试键入...返回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 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

您的控制台“有用”地将三个句点字符(...)转换为unicode标准允许的省略号(...)。由于你正在使用std::string,(我假设是linux,因为Windows没有这样做),它必须转换为UTF-8。 unicode字符是代码点U + 2026,UTF-8为0xE2 0x80 0xA6,或者为cstring "\xE2\x80\xA6"

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

答案 2 :(得分:0)

我已经完成并在必要时用std :: classes替换你的东西,对我来说硬编码输入字符串“......”导致输出“SS”所以你的实际莫尔斯翻译是好的(假设您希望它丢弃空格) - 但用cin捕获字符串会截断空格处的输入。