Problem with my morse code program

This is a discussion on Problem with my morse code program within the C++ Programming forums, part of the General Programming Boards category; I have just written the morse code program and it seems fine. I just have a little problem. In the ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Problem with my morse code program

    I have just written the morse code program and it seems fine. I just have a little problem. In the query menu, I input 'a' and it works fine, then i input 'b' and it is still fine. But then I do 'a' again then it does not appear right. I think there is some problem with the code that has only 2 characters. Can anybody help me? This is my code

    Code:
    #include <iostream>
    #include <ctime>
    #include <cmath>	
    #include <string>
    #include<cstdlib>
    #include<cctype>
    #include<cstdio>
    
    using namespace std;
    
    void instruction();
    void query();
    void practice();
    void message();
    void setting();
    void quit();
    void morse();
    void flash();
    
    
    char ch;
    char code[6];
    char set;
    int trial;
    double delay = 0.5;
    
    int main()
    {
    	do
    	{
    	int choice;
    	instruction();
    	cin >> choice;
    	
    	switch (choice)
    	{
    	case 1:
    		query();
    		break;
    	case 2:
    		practice();
    		break;
    	case 3:
    		message();
    		break;
    	case 4:
    		setting();
    		break;
    	case 5:
    		quit();
    		break;
    	default:
    		cout << "Invalid Choice!." << endl;
    	}
    	}
    	while ( set = 'm');
    
    	system("pause");
    	return (0);
    }
    
    void instruction()
    {
      cout<<"MORSE CODE TUTOR" << endl;
      cout<<"~~~~~~~~~~~~~~~~" << endl;
      cout<<"1. Query Mode" << endl;
      cout<<"2. Practie Mode" << endl;
      cout<<"3. Test Message Mode" << endl;
      cout<<"4. Setting" << endl;
      cout<<"5.Exit" << endl;
      cout<<" Enter your choice:" << endl;
    }
    
     void morse()
    {	
    	switch (toupper(ch)) //run translator
    		{
    			case '0': strcpy(code,"-----");  break;
    			case '1': strcpy(code,".----");  break;
    			case '2': strcpy(code,"..---");  break;
    			case '3': strcpy(code,"...--");  break;
    			case '4': strcpy(code,"....-");  break;
    			case '5': strcpy(code,".....");  break;
    			case '6': strcpy(code,"-....");  break;
    			case '7': strcpy(code,"--...");  break;
    			case '8': strcpy(code,"---..");  break;
    			case '9': strcpy(code,"----.");  break;
    			case 'A': strcpy(code,".-");     break;
    			case 'B': strcpy(code,"-...");   break;
    			case 'C': strcpy(code,"-.-.");   break;
    			case 'D': strcpy(code,"-..");    break;
    			case 'E': strcpy(code,".");      break;
    			case 'F': strcpy(code,"..-.");   break;
    			case 'G': strcpy(code,"--.");    break;
    			case 'H': strcpy(code,"....");   break;
    			case 'I': strcpy(code,"..");     break;
    			case 'J': strcpy(code,".---");   break;
    			case 'K': strcpy(code,"-.-");    break;
    			case 'L': strcpy(code,".-..");   break;
    			case 'M': strcpy(code,"--");     break;
    			case 'N': strcpy(code,"-.");     break;
    			case 'O': strcpy(code,"---");    break;
    			case 'P': strcpy(code,".--.");   break;
    			case 'Q': strcpy(code,"--.-");   break;
    			case 'R': strcpy(code,".-.");    break;
    			case 'S': strcpy(code,"...");    break;
    			case 'T': strcpy(code,"-");      break;
    			case 'U': strcpy(code,"..-");    break;
    			case 'V': strcpy(code,"...-");   break;
    			case 'W': strcpy(code,".--");    break;
    			case 'X': strcpy(code,"-..-");   break;
    			case 'Y': strcpy(code,"-.--");   break;
    			case 'Z': strcpy(code,"--..");   break;
    			case '.': strcpy(code,".-.-.-"); break;
    			case ',': strcpy(code,"--..--"); break;
    			case '?': strcpy(code,"..--.."); break;
    			case ':': strcpy(code,"---..."); break;
    			case '/': strcpy(code,"-..-.");  break;
    			case '(': strcpy(code,"-.--.-"); break;
    			case ')': strcpy(code,"-.--.-"); break;
    			case '-': strcpy(code,"-....-"); break;
    			case ' ': strcpy(code,""); break;
    			default: strcpy(code," ");
    					 cout << "Wrong Input!!" << endl; break;
    
    		}
    	flash();
    	
    }
    
     void query()
    {
    	system("cls");
    	cout<<"Welcome to the Morse Code Translator." << endl;
    	
    	while(ch!='=')
    	{
    	cout << "Enter Character or press = to exit:" << endl;
    	cin >> ch;
    	
    	system("cls");
    	morse();
    	}
    	ch = ' ';
    	quit();
    	
     }
    
     void practice()
    {
    	system("cls");
    	srand(time(NULL));
    	char words[]={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 
    				  'b', 'c', 'd','e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 
    				  'm', 'n', 'o', 'p', 'q', 'r','s', 't', 'u', 'v', 'w', 
    				  'x', 'y', 'z', '.', ',', '?', ':', '/', '(',')', '-'};
    	float count=0;
    	int i;
    	int r;
    	int temp;
    	float number;
    	float percentage;
    	char guess;
    	cout << "How many morse codes you want to practice?" << endl;
    	cin >> number;
    	system("cls");
    	cout << "We will play the code then you will try to identify it. Good luck!" << endl;
    	system("pause");
    	system("cls");
    	while ( trial < number)
    	{
    	
    	for ( i=0; i< number; i++)
    	{
    		r = rand() % 44;
    		ch=words[r];
    		morse ();
    		cout << code << endl;
    
    		cout << "Enter your guess:" << endl;
    		cin >> guess;
    
    		trial++;
    		system("cls");
    	if( guess == ch)
    	{
    		count++;
    	}
    	}
    	}
    	percentage = (((count/number)*100) + 0.005) ;
    	temp = int( percentage * 100);
    	percentage = temp/100.0f;
    	cout << "Your Percentage is: " << percentage << "%" << endl;
    	trial = 0;
    	quit();
    }
    void message()
    {
    	system("cls");
    	int i;
    	int j;
    	int len;
    	string line[6];
    	line[0] = "This is a program that is basically about morse code. ";
    	line[1] = "The program is written in C plus plus language. ";
    	line[2] = "You have 5 options in the menu. ";
    	line[3] = "Use them wisely. ";
    	line[4] = "Have fun.";
    	string sentence;
    
    	for(i=0; i < 6;i++)
    	{
    		sentence = sentence + line[i];
    	}
    
    	cout << "Try to guess the sentence. Good luck!" << endl;
    	system("pause");
    	system("cls");
    
    	len=sentence.length();
    
    	for(j=0; j < len;j++)
    	{
    		ch=sentence[j];
    		morse();
    	}
    
    	system("cls");
    
    	cout << " This is the message: " << endl;
    	cout << sentence;
    
    	ch = ' ';
    	quit();
    }
    void setting()
    {
    	cout << "The default speed is 0.5." << endl;
    	cout << "The speed is faster if it is less than 0.5" << endl;
    	cout << "The speed is slower if it is more than 0.5" << endl;
    	cout << "Enter new speed:" << endl;
    	cin >> delay;
    	cout << "The new speed has been changed." << endl;
    	
    }
    void quit()
    {
    	char answer;
    	cout << " Want to play again? (Y/N)\n" << endl;
    	cin >> answer;
    
    	if( answer == 'y' || answer == 'Y')
    	{
    		set = 'm';
    		cout << "I hope you enjoy it again!" << endl;
    		cout << endl;
    		system("pause");
    		system("cls");
    	}
    	else if ( answer == 'n' || answer == 'N')
    	{
    		cout << "Thank you for using my program..." << endl;
    		exit(0);
    	}
    }
    
    void flash()
    {
    	clock_t cl_start;		// clock_t typedef in time
    	clock_t cl_end;			// clock_t = long int
    	double elapsed_time;	// for time calculation
    	double empty_loop;		// time for empty loop
    	int a,b,c,k,n,h;			// dummy variables
    	double d,e,f;		// dummy variables
    
    	string line;			// Flash Line
    	line = char(219);		// Extended Char Set
    	line = line + line + line + line;
    	for(h=0; h < 8;h++)
    	{
    		if ( code[h] == '.')
    		{
    			cl_start = clock();
    			cl_end = clock();
    			cout << line << endl << line << endl << line;
    			for (k=0; (cl_end-cl_start)<(CLOCKS_PER_SEC*delay);)
    			{
    				cl_end = clock();
    			}
    			system("cls");
    		}
    		else if ( code[h] == '-')
    		{
    			cl_start = clock();
    			cl_end = clock();
    			cout << line << endl << line << endl << line;
    			for (k=0; (cl_end-cl_start)<(CLOCKS_PER_SEC*delay*3); k++)
    			{
    				cl_end = clock();
    			}
    			system("cls");
    		}
    		else if ( code[h] == ' ')
    		{
    			cl_start = clock();
    			cl_end = clock();
    			for (k=0; (cl_end-cl_start)<(CLOCKS_PER_SEC*delay*3); k++)
    			{
    				cl_end = clock();
    			}
    		}
    	}
    }

  2. #2
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    cin.ignore();
    to clear the stream buffer.

    you have a lot of things being input and output to cout and cin. Unless you try safeguarding the input by excluding everything but what you want, you're likely to get unwanted input. Even still, it is good practice to clear them.
    Last edited by sh3rpa; 10-21-2007 at 06:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 02:37 PM
  3. About the Morse code Converter
    By Amber_liam in forum C Programming
    Replies: 17
    Last Post: 05-29-2002, 09:35 AM
  4. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 06:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21