morse code

i'm trying to create number to morse converter, but i got weird errors.

my code:
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
#include <iostream>
#include <limits>

int main () {
	char nums[10][5] = {'.'};
	int u_input = 0;

	for (int i = 0; i < 10; ++i) {
		if (i < 5) {
			for (int d = 0; d <= i; ++d)
				nums [i][d] += '.';
			for (int ds = (i + 1); ds < 5; ++ds)
				nums [i][ds] = '-';
			continue;
		}

		if (i >= 5) {
			for (int ds = 0; ds < (i - 4); ++ds)
				nums [i][ds] += '-';
			for (int d = (i - 4); i < 5; ++d)
				nums [i][d] += '.';
		}
	}

	do {
		std::cout << "Convert number from 0 to 9 into morse (-1 to exit): ";
		std::cin >> u_input;
		
		if (u_input > 0) {
			if (u_input <= 9) {
				for (int d = 0; d < 5; ++d)
					std::cout << nums [u_input - 1][d];
			}
		}

		else if (u_input == 0) {
			for (int d = 0; d < 5; ++d)
				std::cout << nums [9][d];
		}
		std::cout << std::endl;
	} while (u_input != -1);
	std::cout << "Thanks for using this program" << std::endl;
	std::cout << "Press any key to continue...";
	std::cin.ignore (std::numeric_limits<int>::max(), '\n');
	std::cin.get();
	return 0;
}


my results is:

* when i entered 1 the result is \---- while if i entered number between 2 to 5 it displayed the correct results

* when i submit 6 to 9 it displayed only the dashes (without the dots)

can anybody help me?
Wouldn't it be easier to create a constant map<char, string> to map the characters to the proper code?

Line 11,19,21: You're incrementing the value in the array location.
The += operator on a char is an increment, not an append.
@anon: thx, but why this line doesn't work?

1
2
3
4
5
6
if (i >= 5) {
			for (int ds = 0; ds < (i - 4); ++ds)
				nums [i][ds] += '-';
			for (int d = (i - 4); i < 5; ++d)
				nums [i][d] += '.';
		}
What exactly to you think lines 11,19 and 21 do?

At line 5, you initialize your array to dots (decimal value 46).
At line 11, you add the decimal value of two dots together. This gives you a decimal value of 92, which is a backslash.

Does this make it clearer?
 
nums [i][d] = nums [i][d] + '.';





sorry, wrong code, coz i modified it already:

1
2
3
4
5
6
7
//this doesn't work
if (i >= 5) {
			for (int ds = 0; ds < (i -  4); ++ds)
				nums [i][ds] = '-';
			for (int d = (i - 4); i < 5; ++d)
				nums [i][d] = '.';
		}
Did that solve your problem?

If so, please mark your thread as solved.
If not, let us know what's not working now.
no, that's why i comment:
 
//this doesn't work 

because i did get rid those addition operations += and replace it with assignment = and it still not working
Why are you trying to construct the Morse strings dynamically? Yes, you can do that for numbers, but if you're going to extend your program to handle letters, that is not going to work.

As jlb suggested, using a std::map will allow you to use both alpha and numbers.

A much simpler approach for handling just numbers is to use an array of strings.
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
#include <iostream>
#include <limits>
#include <string>

const std::string nums[10] = 
{   /*0*/   "-----",
    /*1*/   ".----",
    /*2*/   "..---",
    /*3*/   "...--",
    /*4*/   "....-",
    /*5*/   ".....",
    /*6*/   "-....",
    /*7*/   "--...",
    /*8*/   "---..",
    /*9*/   "----."
};        
                            
int main () 
{   int u_input = 0;

	do 
	{   std::cout << "Convert number from 0 to 9 into morse (-1 to exit): ";
		std::cin >> u_input;
        if (u_input != -1)		
		    std::cout << nums [u_input] << std::endl;
	} while (u_input != -1);
	std::cout << "Thanks for using this program" << std::endl;
	std::cout << "Press any key to continue...";
	std::cin.ignore (std::numeric_limits<int>::max(), '\n');
	std::cin.get();
	return 0;
}

bro, for real? seriously? using strings is too simple... if that's the answer, i knew it already
the purpose i'm using it because i wanna find the algorithm of converting nums to morse... because there's a pattern for nums...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::string morse( unsigned int digit )
{
    if( digit > 9 ) return morse( digit/10 ) + ' ' + morse( digit%10 )  ;

    const int NCHARS = 5 ;
    char a = '.' ;
    char b = '-' ;
    if( digit > NCHARS )
    {
        std::swap(a,b) ;
        digit %= NCHARS ;
    }

    std::string result( digit%(NCHARS+1), a ) ;
    result.resize( NCHARS, b ) ;

    return result ;
}

http://coliru.stacked-crooked.com/a/cdf2c5bc529b2249
1
2
3
4
5
6
7
if (i >= 5) {
			for (int ds = 0; ds < (i -  4); ++ds)
				nums [i][ds] = '-';
			//for (int d = (i - 4); i < 5; ++d) was testing against i for termination!
			for (int d = (i - 4); d < 5; ++d) // now testing against d
				nums [i][d] = '.';
		}


Andy

PS Your for first for-loop can be simplified a bit, too, if you use an else

1
2
3
4
5
6
7
8
9
10
11
12
13
	for (int i = 0; i < 10; ++i) {
		if (i < 5) {
			for (int d = 0; d <= i; ++d)
				nums [i][d]  = '.';
			for (int ds = (i + 1); ds < 5; ++ds)
				nums [i][ds] = '-';
		} else {
			for (int ds = 0; ds < (i - 4); ++ds)
				nums [i][ds] = '-';
			for (int d = (i - 4); d < 5; ++d)
				nums [i][d]  = '.';
		}
	}
Last edited on
@andy: thank you so much!!! i've been staring the code tens of times (and probably i even dream about it, and of course, it would be a nightmare) and i can't figure out the error!!!
Topic archived. No new replies allowed.