2016-04-17 31 views
0
#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]),它說這是一個錯誤。我該如何解決它?我不知道把Morse的代碼寫成英文。我怎麼知道用戶輸入的數組的位置?英文到摩斯碼代碼

錯誤消息:

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

+2

什麼是錯誤消息? – STF

+0

錯誤消息是:錯誤:'operator =='不匹配(操作數類型是'char'和'std :: string {aka std :: basic_string }')| –

回答

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

您正在嘗試焦炭之間的比較。

你需要寫這樣的數組(如果你想使用只是個字符):

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

但陣列中有一個「停止」。 –

+0

@鄭偉駒但**信**是字符,所以它永遠不會是「停止」它可以只是字符。 – STF

0

- 然後使用strcmp()函數。

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

祝你好運!

+0

在strcmp()''可以與'letter'一起使用之前,還需要更改。 –