c++ - 当我输入 "..."时用户输入不起作用

标签 c++ console-application

我正在做一个莫尔斯语和倒退。除了一种情况外,该程序可以正常工作,当我在用户输入中键入“...”然后按空格时,它变得非常小并且程序不返回任何内容。因此,例如,如果我尝试输入 ... 以返回 S 它有效,但如果我尝试输入 ... ... 对于 SS 它不起作用。我正在使用斯坦福库来获取用户输入和处理 map ,但是当我使用标准库时会发生同样的事情。

#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 或控制台正在创建的任何值:)

关于c++ - 当我输入 "..."时用户输入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9215043/

相关文章:

task - 我应该使用 Task.Wait() 吗?

c# - 如何使控制台应用程序显示为 "Not Responding"?

c# - Console.Write发出声音

c++ - 使用 Windows API 的 RSSI

c++ - C++ 中的 Functor 永远不能抽象吗?

c++ - 编译时类型名到函数名的映射

c# - 使用 C# 控制台应用程序打开和关闭互联网浏览器

c# - 我在控制台应用程序的 App.Config 中设置 elmah 时遇到问题

c++ - 用于光线追踪的 DXR 描述符堆管理

c++ - 停止与推力一起使用的 odeint 集成