Morse Code encrypt & decrypt C++

These are the list of problems I am facing-
1. Will the forloop work?
2. And the "<<decode" gives junk values as output.
This is for a school project.
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
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
	char op;
	char morse[50],chr[25],decode[7];
	char letter[10];
	int i,j=0,k=0;
  cout<<"Enter option d to decrypt or e to encrypt the message -";
  cin>>op;
	switch(op)
	{
		case'd':cout<<"Enter the morse code \nEach letter should be separated by a space\n";
			gets(morse);
			cout<<"The entered morse code is ";
			puts(morse);
		      for(i=0;morse[i]!=NULL;i++)
		       {
			if(morse[i]!=' ')
			       {morse[i]=decode[j++]; //Some better write this//
				cout<<"Decode is "<<decode;}
			else if(morse[i]==' ')
			 {
				if(strcmp(decode,".-"))
				letter[k++]='a';
				else if(strcmp(decode,"-..."))
				letter[k++]='b';
				else if(strcmp(decode,"-.-."))
				letter[k++]='c';
				else if(strcmp(decode,"-.."))
				letter[k++]='d';
			  }
			cout<<letter;

			}
		break;
		case'e':cout<<"Enter the char you want to encrypt \n";
			gets(chr);
			cout<<"The entered char are "<<chr;
	}
}
When you compile your code, what worthless fossil compiler do you use? I ask because you are being taught C++ that no sane developer will write new code in (except maybe to maintain existing code, but even then I'd question their sanity). I believe several people told you about existing issues with your code in your previous thread, here: http://www.cplusplus.com/forum/beginner/256876/

To answer your questions:
1. The comparison should be against '\0', not NULL. Otherwise, yes.
2. It doesn't look like your decode code is anywhere near complete. First, it looks like you're trying decode into your "letter" array, so why are you printing out your "decode"array? Also, you're reading into your "morse" array, but all of your strcmps are comparing your "decode" array (which never gets assigned to anything from morse). I'm guessing that's what you're trying to do on line 22, but the assignment is backward, and as a side note, j never gets reset.

Please ask your teacher why you're being asked to use pre-standard C++, if you can. What you're being taught is unacceptable.

-Albatross
You are paying for this "education", so you'd better make sure you're getting value for money.

Who's paying?
- your parents, indirectly through their taxes.
- your parents, directly, because you're at a private school where you pay for tuition.
- you, indirectly, through your upcoming failure to get a decent job because these "skills" are obsolete.

> gets(morse);
If you used this in my code, it would be instant summary dismissal!
It's so irredeemably awful that it was deprecated in C99 and removed in C11.
http://c-faq.com/stdio/getsvsfgets.html

> These are the list of problems I am facing-
> 1. Will the forloop work?
> 2. And the "<<decode" gives junk values as output.
You're committing the first basic sin of programming, trying to do too much at once.
You NEED to make sure step 1 works before starting step 2.
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
#include <iostream>
using namespace std;

void text_to_morse() {
}

void morse_to_text() {
  cout<<"Enter the morse code \nEach letter should be separated by a space\n";
  char line[100];
  cin.getline(line,sizeof(line));
  char word[100];
  for ( int i = 0 ; line[i] != '\0' ; i++ ) {
    if ( line[i] == ' ' ) {
      // The double == makes sure you will see any errant leading or trailing spaces in word.
      cout << "Found word =" << word << "=" << endl;
    }
  }
}

int main ( ) {
  char line[100];
  cin.getline(line,sizeof(line));
  if ( line[0] == 'd' ) {
    morse_to_text();
  } else if ( line[0] == 'e' ) {
    text_to_morse();
  }
}

Your first step is add the MINIMUM amount of code to make this work.
1
2
3
4
5
6
Enter the morse code
Each letter should be separated by a space
.- -... -.-.
Found word =.-=
Found word =-...=
Found word =-.-.=

You must make your morse word extraction work reliably BEFORE trying to decode those words back to the associated letters.
on the grounds you would be unwise to turn this in without being able to explain it, and it being right here on the internet, here is a quick example of turning dots & dashes back to text, in the dialect of C++ you are speaking (C with cout).

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
struct mctx //lets make a little struct for this exercise
{
  char c;
  unsigned char dot;
  unsigned char dash;	
};

int main()
{
int dx = 0;
mctx mct[50]; //a table of our structs.  I left room for the numbers later. 
char init[] = "\0etianmsurwdkgohvf\0l\0pjbxcyzq"; //there is madness here. do you see it?
//hint, look at etianm as dots and dashes.  then it will leap out. 
//lets load up a table of our struct. 
for(dx = 0; dx < 30; dx++)
{
  mct[dx].c = init[dx];	 //assuming you got the pattern above, then 
  mct[dx].dot = (dx*2)+1;   //what is this doing?  do you see? 
  mct[dx].dash = (dx*2)+2;
}

//a classic example code
char word[] = "... --- ... --- "; //cheesy approach needs extra space or stx on end.

//and now we just turn the code back to text by tracing the paths. 
for(int i=0, dx = 0; dx < strlen(word); dx++)
{      
    switch(word[dx])
	{
    case '.': 
	i = mct[i].dot;
	break;
	case '-': 
	i = mct[i].dash;
	break;
	case ' ': 
	cout << mct[i].c;
	i = 0;
	break;  	
	}	
}

}


I have done a bunch of things here. There is a binary search tree, and its be jacked into an array instead of a tree construct. Ive done a traversal of this tree. Ive used integers instead of pointers (array indexing). Ive done the legwork to figure out the the data so it could be loaded into the tree simply. If you are still interested, I will answer questions, simply because, again, there is no way you would have gotten here on your own (yet). Its using a lot of stuff you won't see for a while. So you can learn a bit from it, but you can't cheat with it :P

and yea, its not 100%. it will crash if you give it like 6 dots in a row without some safeguards that I left out to keep it simple and concise.

a reverse lookup table of strings probably beats it, but its not as amusing.

and you kids, this was a lot of what c++ was in 1990. Gotta love it.
Last edited on
Topic archived. No new replies allowed.