home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Morse Code

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1584
Discussions: 200,982, Posts: 2,379,843, Members: 246,344
Old March 13th, 2002, 11:17 PM   Digg it!   #1 (permalink)
Senior Member
 
Pshawn5's Avatar
 
Join Date: Oct 2001
Location: Orange,CA
Posts: 782
Send a message via ICQ to Pshawn5 Send a message via AIM to Pshawn5 Send a message via MSN to Pshawn5 Send a message via Yahoo to Pshawn5
Morse Code

ok this is for a school assignment, we are suppose to create a program where the user inputs english and the program outputs the morse code for it and vice versa.
so far i've done only option 1: english to morse code but i'm running into a problem
say if the user enters :"hello"
the program prints out the morse code.
but if the user enters: "hello world"
the program goes haywire.. i dont know why either,since i have a case for spaces....any help would be appreciated...
btw yes i know the way im doing it is the long way but that's how the teacher sort of explained how to do it.
Code:
#include <iostream>
using namespace std;
void engconvert(char[]);
//void morseconvert(char[]);

int main()
{
	int menuChoice;
	char englstring[20];
	char morsestring[20];
	//allows user to make option and continue to use it until they are finished
	while(menuChoice !=3) 
	{
	cout <<"Menu" <<endl;
	cout <<"1 - English to Morse Code \n";
	cout <<"2 - Morse Code to English\n";
	cout <<"3 - End Program\n";
	cout << "Enter your choice now: ";
	cin >> menuChoice;
	if (menuChoice == 1) //option 1
	{	
		cout <<"Enter the phrase which you would like to be converted to Morse Code: \n";
	
		cin >> englstring;
		
		engconvert(englstring);
	}
	if (menuChoice == 2){ //option 2
		cout <<"Enter the Morse Code for which you would like to be converted to English:\n";
		cin >> morsestring;
		//morseconvert(morsestring);
	}
		
    if (menuChoice == 3) //option 3
	{
		cout << "Thanks for using the program. Good Bye!\n";
	}
	}
	
	return 0;
}

void engconvert(char english[])
{
	cout << "Here is the morse code translation:\n";
	for (int i= 0;i<20;i++)

	{
	switch (english[i])
	{
	case 'a':
	case 'A':
	cout<<".- ";
	break;
	
	case 'b':
	case 'B':
	cout<<"-... ";
	break;
	
	case 'c':
	case 'C':
	cout <<"-.-. ";
	break;
	
	case 'd':
	case 'D':
	cout <<"-.. ";
	break;
	
	case 'e':
	case 'E':
	cout<<". ";
	break;
	
	case 'f':
	case 'F':
	cout<<"..-. ";
	break;
	
	case 'g':
	case 'G':
	cout<<"--. ";
	break;
	
	case 'h':
	case 'H':
	cout <<".... ";
	break;

	case 'i':
	case 'I':
	cout<<".. ";
	break;

	case 'j':
	case 'J':
	cout <<".--- ";
	break;

	case 'k':
	case 'K':
	cout<<"-.- ";
	break;

	case 'l':
	case 'L':
	cout<<".-.. ";
	break;

	case 'm':
	case 'M':
	cout<<"-- ";
	break;

	case 'n':
	case 'N':
	cout<<"-. ";
	break;
	case 'o':
	case 'O':
	cout << "--- ";
	break;

	case 'p':
	case 'P':
	cout<<".--. ";
	break;

	case 'q':
	case 'Q':
	cout <<"--.- ";
	break;

	case 'r':
	case 'R':
	cout << ".-. ";
	break;

	case 's':
	case 'S':
	cout <<"... ";
	break;

	case 'T':
	case 't':
	cout<<"- ";
	break;

	case 'u':
	case 'U':
	cout <<"..- ";
	break;

	case 'V':
	case 'v':
	cout<< "...- ";
	break;

	case 'W':
	case 'w':
	cout <<".-- ";
	break;

	case 'X':
	case 'x':
	cout << "-..- ";
	break;

	case 'Y':
	case 'y':
	cout << "-.-- ";
	break;

	case 'z':
	case 'Z':
	cout << "--.. ";
	break;
	
	case ' ':
	cout<<"   ";
	break;
	case '1':
	cout << ".----";
	break;

	case '2':
	cout <<"..---";
	break;

	case '3':
	cout << "...--";
	break;
	
	case '4':
	cout <<"....-";
	break;

	case '5':
	cout << ".....";
	break;

	case '6':
	cout << "-....";
	break;
	
	case '7':
	cout << "--...";
	break;

	case '8':
	cout << "---..";
	break;

	case '9':
	cout << "----.";
	break;

	case '0':
	cout << "-----";
	break;

	case '\0': // if the string is less than 20 characters it ends the loop
	i=20;
	break;

		

	}
	
}
cout <<endl;


return;	
}
Pshawn5 is offline   Reply With Quote
Old March 13th, 2002, 11:23 PM     #2 (permalink)
mickwish
 
Posts: n/a
This isn't my thing, but is ' ' a null character or a space?? maybe there is another char for space , like a hex code or something?

Just a thought. Ignore me if I'm no where near.

Actually, another thought: maybe text strings with spaces need to be enclosed in quotation marks, like in good ol basic ie :""hello world"" instead of "world" with no spaces.

I have no idea of C or C++ I will admit, this is just a stab in the dark....
  Reply With Quote
Old March 13th, 2002, 11:26 PM     #3 (permalink)
Member
 
Join Date: Oct 2001
Location: Duncanville,Texas
Posts: 223
Have you tried to do the entire alphabet? Does it have a problem with any other letter possibly 'n'?

cstierhoff
__________________
Out Playing in Traffic
cstierhoff is offline   Reply With Quote
Old March 13th, 2002, 11:31 PM     #4 (permalink)
mickwish
 
Posts: n/a
Just noticed something else in the code - dunno if it's significant or not.

Your cout commands don't all have the same syntax - some include a space after <<, some don't. Is this a problem?

eg
case ' ':
cout<<" ";

and

case 'z':
case 'Z':
cout << "--.. ";
  Reply With Quote
Old March 13th, 2002, 11:37 PM     #5 (permalink)
Senior Member
 
Pshawn5's Avatar
 
Join Date: Oct 2001
Location: Orange,CA
Posts: 782
Send a message via ICQ to Pshawn5 Send a message via AIM to Pshawn5 Send a message via MSN to Pshawn5 Send a message via Yahoo to Pshawn5
i figured out the problem it's not reading anything after the "hello"
because i tried "1 2"
and what it did was it printed out the morse code for 1
then went back to the main menu and used 2 for that..

so the question now is how do i get it to read the whole string...
Pshawn5 is offline   Reply With Quote
Old March 13th, 2002, 11:37 PM     #6 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,398
well its definately puking after that space....

I ran it in VC++ and I'm stepping through it one step at a time. I look at the array and my input was 'stupid a$$'

'stupid' worked just fine, but as you said soon as I put 'stupid a$$' it died.

if you look at the array its
0=s
1=t
2=u
3=p
4=i
5=d
6= (space)
7= the rest is crap :/
ascii char -52 LOL whatever that is...
vass0922 is offline   Reply With Quote
Old March 13th, 2002, 11:38 PM     #7 (permalink)
Senior Member
 
Pshawn5's Avatar
 
Join Date: Oct 2001
Location: Orange,CA
Posts: 782
Send a message via ICQ to Pshawn5 Send a message via AIM to Pshawn5 Send a message via MSN to Pshawn5 Send a message via Yahoo to Pshawn5
mickwish the space doesnt effect the program,just make it neater...so i will go back and do that.... grrr this is only one step of the problem, still have to do morse code to english...joy joy joy
Pshawn5 is offline   Reply With Quote
Old March 13th, 2002, 11:40 PM     #8 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,398
btw, should of just upper cased the input string LOL
that way you don't have to test for both cases
stupid teachers
__________________
Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.
vass0922 is offline   Reply With Quote
Old March 13th, 2002, 11:44 PM     #9 (permalink)
Senior Member
 
Pshawn5's Avatar
 
Join Date: Oct 2001
Location: Orange,CA
Posts: 782
Send a message via ICQ to Pshawn5 Send a message via AIM to Pshawn5 Send a message via MSN to Pshawn5 Send a message via Yahoo to Pshawn5
ok i have to use a cin.get line to get it to read the whole thing (did some reading)
but how would i run the string into the function?
Pshawn5 is offline   Reply With Quote
Old March 13th, 2002, 11:51 PM     #10 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,398
its a function so you should be able to do a variable = cin.getline etc etc.
vass0922 is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Charges against non-tippers dropped.. (20)
Is It Just Me? (3063)
Health Care Rationing (10)
Delete an OS (16)
Nvidia GTX 260 problem (9)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
windows vista security holes (18)
Regular Build (11)
Point and Shoot Camera Suggestions. (7)
windows 7 problem (7)
Internet Lost (5)
Multiple Restarts Required at Boot (5)
Recent Discussions
New Server Configuration Suggestions (0)
Desktop Calendar Application (0)
updating BIOS via winflash, claims fi.. (0)
cheap gaming laptop? (12)
Unallocated Space (2)
help me pls laptop just stopped worki.. (1)
C# + LINQ Help (7)
windows vista security holes (18)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (38)
Nvidia GTX 260 problem (9)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
Point and Shoot Camera Suggestions. (7)
Delete an OS (16)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)
Linksys WMP54GS wireless card problem.. (5)
Help getting around port 80 for camer.. (5)
Skillsoft Network+ Study Software Que.. (10)
Browsers wont load websites (3)


All times are GMT -4. The time now is 02:32 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



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