char* check not evaluating. Morse to Text Prob. Please helplem

Hello :)
So I'm learning pointers and we're supposed to convert morse to text and vice versa. As you can see from the code below, the string library is not allowed and we're supposed to make our own functions to append and stuff. Basically, the "charToDigit" function is supposed to return a digit but the morse I send into it doesn't come out as any digit. VS 2017's debugger shows the strings to be the same but the check doesn't evaluate to true. From my several attempts to figure out the problem myself, I can say that the problem is in the code that is making the string to be sent to charToDigit, "digittemp". I've been at this for a few days and the deadline's coming up so some help would really be appreciated.
Thank you for your time :)

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
#include<iostream>

using namespace std;

char *StrCat(char* s1, const char* s2)
{
	int i;
	int len = 0;
	while (*(s1 + len) != '\0')
		len++;
	for (i = 0; *(s2 + i) != '\0'; i++)
	{
		*(s1 + len+i) = *(s2 + i);
	}
	*(s1 + len+i) = '\0';
	return s1;
}
const char *charToDigit(const char* morse)
{

	const char* m = morse;

	if (m == ".-")
		return "A";
	else if (m == "-...")
		return "B";
	else if (m == "-.-.")
		return "C";
	else if (m == "-..")
		return "D";
	else if (m == ".")
		return "E";
	else if (m == "..-.")
		return "F";
	else if (m == "--.")
		return "G";
	else if (m == "....")
		return "H";
	else if (m == "..")
		return "I";
	else if (m == "...")
		return "S";
	else if (m == "-")
		return "T";
	else if (m == "..-")
		return "U";
	else if (m == "...-")
		return "V";
	else if (m == ".--")
		return "W";
	else if (m == "-..-")
		return "X";
	else if (m == "-.--")
		return "Y";
	else if (m == "--..")
		return "Z";
	else if (m == "-----")
		return "0";
	else if (m == ".----")
		return "1";
	else if (m == "..---")
		return "2";
	else if (m == "...--")
		return "3";
	else if (m == "....-")
		return "4";
	else if (m == ".....")
		return "5";
	else if (m == "-....")
		return "6";
	else if (m == "--...")
		return "7";
	else if (m == "---..")
		return "8";
	else if (m == "----.")
		return "9";
	else if (m == ".-.-.-")
		return ".";
	else if (m == "--..--")
		return ",";
	else if (m == "..--..")
		return "?";
	else if (m == ".----.")
		return "\'";
	else if (m == "-.-.--")
		return "!";
	else if (m == "-..-.")
		return "/";
	else if (m == "-.--.")
		return "(";
	else if (m == "-.--.-")
		return ")";
	else if (m == ".-...")
		return "&";
}
char * convertToString(char *string)
{
	char *digittemp=new char;
	const char *digitmade = new char;
	char *stringConverted=new char;
	*stringConverted = '\0';
	int j = 0;
	for (int i = 0; *(string + i) != '\0'; i++)
	{
		if(*(string + i) != '#' && *(string + i) != '/' && *(string + i) != '\0')
		{
			*(digittemp + j) = *(string + i);
			j++;
		}
		else
		{
			*(digittemp + j) = '\0';
			StrCat(stringConverted, charToDigit(digittemp));
			j = 0;
		}
		if (*(string + i) == '/')
			StrCat(stringConverted, " ");
	}
	return stringConverted;
}
int main()
{
	char s[] = "....#.#.-..#.-../..#.../....#....#.-..";
	char *s1 = s;


	//cout << Strlen(s1);
	cout << convertToString(s1);
	/*cout << *StrTok(NULL, ' ')<<"\n";
	cout << *StrTok(NULL, ' ') << "\n";
	cout << *StrTok(NULL, ' ') << "\n";
	cout << *StrTok(NULL, ' ') << "\n";
	cout << *StrTok(NULL, ' ') << "\n";*/
}
Comparing C strings using == will only compare pointers. If you are not allowed to use std::string or std::strcmp you'll have to write your own string comparison function that loops through the characters in the strings to test if they are the same.
I'm sorry to say that there are many problems with this code. Let's start by getting charToDigit() working.

The name charToDigit() implies that the function takes a character and returns a digit. In truth, it does neither. It takes a Morse string representing a single character and returns the character (as a string). Let's rename it morseToChar(const char *). Since it returns just one character, let's make it
char morseToChar(const char *morse)

You can't compare strings with ==. Instead you'll have to write your own function. Since the standard library function is called strcmp(), let's call it StrCmp() to follow your naming convention in StrCat(). See the description of strcmp here: http://www.cplusplus.com/reference/cstring/strcmp/

Write this function. Then test it with the following code which you should temporarily insert at the beginning of main()
1
2
3
4
5
cout << "hello vs. hello: " << StrCmp("hello", "hello") << '\n';
cout << "hello vs. hello there: " << StrCmp("hello", "hello there" << '\n';
cout << "empty string vs xyz: " << StrCmp("", "xyz") << '\n';
cout << "two empty strings: " << StrCmp("", "") << '\n';
return 0;


Once you're convinced that StrCmp works, change charToDigit() to morseToChar() like this:

1
2
3
4
5
6
7
char morseToChar(const char *morse)
{
    if (StrCmp(morse, ".-") == 0)
        return 'A';   // Note that this is the character 'A' and not the string "A"
    else if (StrCmp(morse, "-...") == 0)
        return 'B';
    // etc; 


When that's done, put some temporary code at the beginning of main() to test morseToChar(), similar to what you did for StrCmp.

I think you'll find it much easier to use array syntax (e.g, s1[i]) rather than pointer syntax (e.g., *(s1+i)).

To help out a little, I've written StrLen() and StrCpy(), and also implemented StrCat using StrLen and StrCpy. These are untested but I think they're correct.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
unsigned StrLen(const char *s)
{
    unsigned i;
    for (i=0; s[i]; ++i)
        ;
    return i;
}

char *StrCpy(char *dst, const char *src)
{
    unsigned i=0;
    do {
        dst[i] = src[i];
    } while (src[i++]);
    return dst;
}

char *
StrCat(char *s1, const char *s2)
{
    StrCpy(s1+StrLen(s1), s2);
    return s1;
}

Thank you guys :)
I already had my own set of functions to replace the string library ones so I fixed the problem with those, i.e: Strlen and StrCmp. I was using the pointer notation instead of the array notation because we're studying pointers and were instructed to use it for some practice. And.... I understand my mistake with the naming conventions so I'll work on those more :). Any other criticism would be appreciated :)))
Here's an idea:

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
char morseToChar(const char *m)
{
    if (!m || !*m)
        return '\0';
    int n = 0;
    for ( ; *m; ++m)
    {
        if (*m != '.' && *m != '-')
            return '\0';
        n = n * 10 + (*m == '-') + 1; // '-' is 2; '.' is 1
    }
    char c = '\0';
    switch (n) {
    case   12: c = 'A'; break;
    case 2111: c = 'B'; break;
    case 2121: c = 'C'; break;
    case  211: c = 'D'; break;
    case    1: c = 'E'; break;
    case 1121: c = 'F'; break;
    case  221: c = 'G'; break;
    case 1111: c = 'H'; break;
    case   11: c = 'I'; break;
    case 1222: c = 'J'; break;
    ...
    }
    return c;
}

Last edited on
Topic archived. No new replies allowed.