当我输入"..."时,用户输入不起作用
Userinput doesn't work when I enter "..."
我正在做一个单词和向后的莫尔斯。除了一种情况外,程序正常工作,当我在用户输入中键入"…",然后按空格时,它会变得非常小,程序不会返回任何内容。例如,如果我尝试打字。。。返回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捕获字符串会截断空格处的输入。
相关文章:
- C++如何通过用户输入删除列表元素
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 用户定义函数中的指针和输入
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 在C++中,我如何接受不同于同一行的用户输入
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 当用户键入分隔符时,停止getline()输入
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 显示基于用户输入的整数的字符
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 如果用户没有输入任何内容或输入错误,如何重新输入用户的输入?
- 为什么在代码块中执行时会跳过输入用户输入的行?
- 使用 for 循环在数组中输入用户