I need to convert English letters and Arabic numbers to Morse code until enter
is pressed. I used a do while
loop to read letters until enter
is pressed.
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int j = 0;
char a[100];
const char *latin = "**ETIANMSURWDKGOHVF?L?PJBHCYZQ??54?3??2?????6????7??890";
void dash(){cout<<"-";}
void dot(){cout<<".";}
void conv(int decimal)
{
if(decimal){
conv(decimal/2);
if(decimal!=1) decimal%2 ? dash() : dot();
}
}
void morse(char a[], int lenght)
{
for(int i = 0; i <= lenght; i++)
{
if(a[i] >= 'a' && a[i] <= 'z') a[i] -= 32;
if(a[i] < 'A' && a[i] > 'Z') return;
while(latin[++j] != a[i]);
conv(j);
}
}
int main()
{
int lenght = 0;
cout<<"letter=";
do{
cin>>a[lenght];
lenght++;
} while(0);
morse(a, lenght);
return 0;
}
When I return the length it is always one, so the function makes only one loop. Also, there are some extra symbols that appear when the code is compiled.