Okay, I wrote a code to translate a character into Morse Code.

However, I was reading back over my assignment and I was actually supposed to define a macro PRINT_DOT which prints a period, and a macro PRINT_DASH, which prints an underscore. Then define macros PRINT_A, PRINT_B, etc.


Is there any way to easily change what I have, to what I am supposed to have?


Thanks! Here is my code.

#include <iostream.h>

int main ()
{

	char ch;

	while (ch != '=')
	{
		cout<<"What character would you like to generate into Morse Code (= to exit)? ";
		cin>>ch;
		cout<<endl;
		switch (ch)
		{
		case 'a':
		case 'A': cout<<".-"<<endl<<endl;
			break;
		case 'b':
		case 'B': cout<<"-..."<<endl<<endl;
			break;
		case 'c':
		case 'C': cout<<"-.-."<<endl<<endl;
			break;
		case 'd':
		case 'D': cout<<"-.."<<endl<<endl;
			break;
		case 'e':
		case 'E': cout<<"."<<endl<<endl;
			break;
		case 'f':
		case 'F': cout<<"..-."<<endl<<endl;
			break;
		case 'g':
		case 'G': cout<<"--."<<endl<<endl;
			break;
		case 'h':
		case 'H': cout<<"...."<<endl<<endl;
			break;
		case 'i':
		case 'I': cout<<".."<<endl<<endl;
			break;
		case 'j':
		case 'J': cout<<".---"<<endl<<endl;
			break;
		case 'k':
		case 'K': cout<<"-.-"<<endl<<endl;
			break;
		case 'l':
		case 'L': cout<<".-.."<<endl<<endl;
			break;
		case 'm':
		case 'M': cout<<"--"<<endl<<endl;
			break;
		case 'n':
		case 'N': cout<<"-."<<endl<<endl;
			break;
		case 'o':
		case 'O': cout<<"---"<<endl<<endl;
			break;
		case 'p':
		case 'P': cout<<".--."<<endl<<endl;
			break;
		case 'q':
		case 'Q': cout<<"--.-"<<endl<<endl;
			break;
		case 'r':
		case 'R': cout<<"-.-"<<endl<<endl;
			break;
		case 's':
		case 'S': cout<<"..."<<endl<<endl;
			break;
		case 't':
		case 'T': cout<<"-"<<endl<<endl;
			break;
		case 'u':
		case 'U': cout<<"..-"<<endl<<endl;
			break;
		case 'v':
		case 'V': cout<<"...-"<<endl<<endl;
			break;
		case 'w':
		case 'W': cout<<".--"<<endl<<endl;
			break;
		case 'x':
		case 'X': cout<<"-..-"<<endl<<endl;
			break;
		case 'y':
		case 'Y': cout<<"-.--"<<endl<<endl;
			break;
		case 'z':
		case 'Z': cout<<"--.."<<endl<<endl;
			break;
		case ',': cout<<"--..--"<<endl<<endl;
			break;
		case '.': cout<<".-.-.-"<<endl<<endl;
			break;
		case '?': cout<<"..--.."<<endl<<endl;
			break;
		case ':': cout<<"---..."<<endl<<endl;
			break;
		case '/': cout<<"-..-."<<endl<<endl;
			break;
		case '(':
		case ')': cout<<"-.--.-"<<endl<<endl;
			break;
		case '-': cout<<"-....-"<<endl<<endl;
			break;
		default:  cout<<"Cannot be generated. "<<endl<<endl;
		}
	}



	return 0;
}

>>Is there any way to easily change what I have, to what I am supposed to have?

Depends on your definition of easily :) It will require some rewriting of each of those cout lines, but shouldn't been too difficult. Something like below (not compiled or tested)

#define PRINT_DOT (cout << ".";)
#define PRINT_DASH (cout << "-";)

...
case A:  PRINT_DOT PRINT_DASH cout << "\n\n"; break;

Thanks so much for the help!

I got to letter 'K' but I got tired :icon_lol:

So I'm just going to stick with what I had...it's just extra credit work so I think I will get credit just if I do it.
Thanks though!

Something that will cut the tedium of switches based on letter input:

#include <cctype>
//other header stuff

char input;

cin >> input;
input = toupper( input ); //force value to upper case

switch( input )
{
   case 'A':  //stuff
                 break;
   case 'B': //stuff
                 /break
//etc
}
This question has already been answered. Start a new discussion instead.