my morse code translator

my code works for the most part, but im trying to allow whole words. i am a somewhat beginner at coding, please help and explain in very simple terms how to allow whole words and double digit+ numbers (i would be using one / for a new letter/number and // for a space) i will be adding more characters after too like !,?,&, etc...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  #include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
    using std::string;
string X;
  /*
  INITIAL TRY (now a reference):
   char a[]="· –";
   char b[]="– · · ·";
   char c[]="– · – ·";
   char d[]="– · ·";
   char e[]="·";
   char f[]="· · – ·";
   char g[]="– – ·";
   char h[]="· · · ·";
   char i[]="· ·";
   char j[]="· – – –";
   char k[]="– · –";
   char l[]="· – · ·";
   char m[]="– –";
   char n[]="– ·";
   char o[]="– – –";
   char p[]="· – – ·";
   char q[]="– – · –";
   char r[]="· – ·";
   char s[]="· · ·";
   char t[]="–";
   char u[]="· · –";
   char v[]="· · · –";
   char w[]="· – –";
   char x[]="– · · –";
   char y[]="– · – –";
   char z[]="– – · ·";
  
   */
   while (3==3) 
   {
cout<< "type any character to see its morse code equivilant: ";

cin >> X;
if (X=="a")
{
cout <<".-";
}
if (X=="b")
{
cout <<"-...";
}if (X=="c")
{
cout <<"-.-. ";
}if (X=="d")
{
cout <<"-..";
}if (X=="e")
{
cout <<".";
}if (X=="f")
{
cout <<"..-.";
}if (X=="g")
{
cout <<"--.";
}if (X=="h")
{
cout <<"....";
}if (X=="i")
{
cout <<"..";
}if (X=="j")
{
cout <<".---";
}if (X=="k")
{
cout <<"-.-";
}if (X=="l")
{
cout <<".-..";
}if (X=="m")
{
cout <<"--";
}if (X=="n")
{
cout <<"-.";
}if (X=="o")
{
cout <<"---";
}if (X=="p")
{
cout <<".--.";
}if (X=="q")
{
cout <<"--.-";
}if (X=="r")
{
cout <<".-.";
}if (X=="s")
{
cout <<"...";
}if (X=="t")
{
cout <<"-";
}if (X=="u")
{
cout <<"..-";
}if (X=="v")
{
cout <<"...-";
}if (X=="w")
{
cout <<".--";
}if (X=="x")
{
cout <<"-..-";
}if (X=="y")
{
cout <<"-.--";
}if (X=="z")
{
cout <<"--..";
}if (X=="0")
{
cout<<"-----";
}if (X=="1")
{
cout<<".----";
}if (X=="2")
{
cout<<"..---";
}if (X=="3")
{
cout<<"...--";
}if (X=="4")
{
cout<<"....-";
}if (X=="5")
{
cout<<".....";
}if (X=="6")
{
cout<<"-....";
}if (X=="7")
{
cout<<"--...";
}if (X=="8")
{
cout<<"---..";
}if (X=="9")
{
cout<<"----.";
}


cout <<"\n";
}
system ("pause");

}
I'm not exactly sure what you are asking.

...how to allow whole words and double digit+ numbers (i would be using one / for a new letter/number and // for a space) i will be adding more characters after too like !,?,&, etc..


Are you saying you want to be able to read in an entire word and then print out that word as morse code?

This could be done by adding another loop to process the word read into your input string X.

Starting at line 43:
1
2
3
4
5
6
7
cin >> X;
//for each character in the string, 
//print the morse code representation of that character
for(size_t i = 0; i < x.length(); i++)
{
    // Your long block of if statements would go here.
}


This won't solve your whole problem, but it is a starting point. Additionally, I'd suggest putting the if block into a separate function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string TranslateToMorse(const string& character)
{
    if (character=="a")
   {
      return ".-";
   }
   else if(character=="b")
   {
      return "-...";
   }
   // ...
   // the other stuff
   // ...
   else if(character=="9")
   {
      return "----.";
   }
   else
   {
      // whoops!  we couldn't process this character
      // report an error, ignore it, whatever you want
   }
}

@nkendra

If your planning to call TranslateToMorse() from you loop, then it prob need to have a char parameter, with appropriate adjustments to the if tests.

string TranslateToMorse(const string& character)

@esazs

If you are comfortable with arrays, and know about isalpha, isdigit, tolower, and ascii codes, you can compact your code right, right down!

Andy

http://www.asciitable.com/

http://www.cplusplus.com/reference/cctype/isalnum/
http://www.cplusplus.com/reference/cctype/isdigit/
http://www.cplusplus.com/reference/cctype/tolower/
Topic archived. No new replies allowed.