gpt4 book ai didi

c++ - 英语到摩尔斯电码程序

转载 作者:太空宇宙 更新时间:2023-11-04 16:05:16 25 4
gpt4 key购买 nike

#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 Code\n";
cout << "4 - Quit\n";
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]),说是错误。我该如何解决?而且我不知道将摩尔斯电码写成英语。我怎么知道用户输入的数组的位置?

错误信息:

error: no match for 'operator==' (operand types are 'char' and 'std::string {aka std::basic_string}')|

最佳答案

您正在尝试比较 stringschar

你需要这样写数组(如果你只想使用字符):

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"};

关于c++ - 英语到摩尔斯电码程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36673183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com