英语到摩尔斯电码程序
English to Morse code program
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <stdlib.h>
#include <string>
using namespace std;
int main()
{
string text[39] = {"A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9","0","Stop",",","?"};
string code[39] = {".-","-...","-.-.","-..",".","..-","--.","....","..",".---","-.-",".-..","--",
"-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",
".----","..---","...--","....-",".....","-....","--....","---..","----.","-----",".-.-.-","--..--","..--.."};
string English, Morse, output_string;
int option, string_size = 0, location;
char again = 'y', letter;
while(again == 'y')
{
system("cls");
cout << "1 - Encode(Text to Morse)n";
cout << "2 - Decode(Morse Code to Text)n";
cout << "3 - Display the Morse Coden";
cout << "4 - Quitn";
cout << "Enter 1,2,3 or 4:";
cin >> option;
cin.ignore(256,'n');
system("cls");
switch(option)
{
case 1:
cout << "nEnter a string with multiple words to encode:";
getline(cin, English);
system("cls");
cout << "nThe target string to be translated is:" << "n";
cout << English << "n";
string_size = English.length();
for(int n = 0; n <= string_size-1; n++)
{
letter = (char)English.at(n);
if(letter != ' ')
{
for(int t = 0; t <=39; t++)
{
if(letter == text[t])
{
cout << code[t] << " ";
break;
}
}
}
else if(letter == ' ')
{
cout << "n";
}
}
getch();
break;
}
}
}
我还没有完成它,但我不知道为什么我不能运行if(letter == text[t])
,它说这是一个错误。 我该如何解决它?而且我不知道写摩尔斯到英语的代码。如何知道用户输入的数组的位置?
错误信息:
错误:与"运算符=="不匹配(操作数类型为"char"和"std::string {aka std::basic_string}"(|
您正在尝试比较字符串和字符。
你需要像这样写数组(如果你只想使用字符(:
char text[39] = {'A','B','C','D','E','F','G','H','I','J','K','L','M'};
而不是:
string text[39] = {"A","B","C","D","E","F","G","H","I","J","K","L","M"};
for (int t = 0; t <= 39; t++)
您有 39 个项目从零索引开始,因此您的循环应该上升到(但不包括(39
for (int t = 0; t < 39; t++)
{
...
}
您可以声明一个临时字符串以将每个字母复制到字符串。您还需要确保文本为大写:
letter = (char)English.at(n);
if (letter != ' ')
{
for (int t = 0; t < 39; t++)
{
std::string temp;
temp = toupper(letter);
if (temp == text[t])
{
cout << code[t] << " ";
break;
}
}
}
如果你希望数组是字符串 - 那么使用 strcmp(( 函数。
if(strcmp(text[t],letter)==0)
{
cout << code[t] << " ";
break;
}
祝你好运!
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 无法通过字符串循环我的程序以将其从英语翻译成猪拉丁语
- 英语到摩尔斯电码程序