郑伟驹
郑伟驹

Reputation: 19

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

I didn't finish it yet, but I don't know why I can't run if(letter == text[t]), it says it's an error. how can I fix it? And I have no idea to write the code that Morse to English. how can I know the position of the array that the user entered?

Error message:

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

Upvotes: 0

Views: 1566

Answers (3)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

for (int t = 0; t <= 39; t++)

You have 39 items starting at zero index, therefore your loop should go up to (but not including) 39

for (int t = 0; t < 39; t++)
{
    ...
}

You can declare a temporary string to copy each letter to string. You would also need to make sure text is upper case:

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

Upvotes: 1

STF
STF

Reputation: 1513

You are trying to compare between strings and char.

You need to write the array like that (if you want to use just characters):

char text[39] = {'A','B','C','D','E','F','G','H','I','J','K','L','M'};

and not:

string text[39] = {"A","B","C","D","E","F","G","H","I","J","K","L","M"};

Upvotes: 4

STF
STF

Reputation: 1513

If you want the array to be string - then use strcmp() function.

if(strcmp(text[t],letter)==0)
      {
        cout << code[t] << " ";
        break;
      }

Have a good luck!

Upvotes: 0

Related Questions