GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Oct-2005, 01:01
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road

english to morse revisited


I recently had a post about this and am sort of revisting my problem in more detail now. i am trying to write a program that will convert English to morsecode. i am trying to make it where there will be a space between each morse equivalent, and will print to a new line when the output gets to the
80th column, but not starting a new line in the middle of the morse equivalent. for a simple test i use the string me[] = "greg".
i expect the results to be...
--. .-. . --.

i am not getting this result.. i would apprectiate it if some one can lend me a helping hand and tell me why its not giving me the output i expect. my code is below.

CPP / C++ / C Code:

#define MAX_STR_SIZE 80
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


 static char morsecode[45][8] = {
      "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--..","0-----",
      "1.----","2..---","3...--","4....-","5.....","6-....","7--...",
      "8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
      "--....-","/-..-.","(-.--.-","\".-..-."};
      
main()
{
      char me[] = "greg";
      int i = 0;
      int j = 0;
      int k = 1;
      int q = 0;
      for (i=0; (i < (strlen(me)));i++)
      {
          for (j=0; j<45;j++)
          {
              if (me[i] == morsecode[j][0])
              {   
                  for (k=1;k<7;k++)
                  {
                      if(morsecode[j][k]!= NULL)
                      {
                          q++;
                          if (q>74)
                          {
                          
                              printf("\n%c",morsecode[j][k]);
                              q = 0;
                          }
                          
                          else
                          
                              printf("%c",morsecode[j][k]);
                          
                      }
                  }
              }
          }
          getchar();
      }
}

  #2  
Old 31-Oct-2005, 01:52
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: english to morse revisited


Quote:
Originally Posted by fadedg2
i am not getting this result.. i would apprectiate it if some one can lend me a helping hand and tell me why its not giving me the output i expect.

Your code does the work. Simply remove the
CPP / C++ / C Code:
getchar();
and it will print your name.
What you actually did is wait for your to enter a character for the program to print each morse character after the first one.

Why not write a function that returns the morse code matching the character you passed it ?
CPP / C++ / C Code:
if(morsecode[j][k]!= NULL)
is not good ...
You are comparing an integer with void * (as the warning tells ... )

Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #3  
Old 01-Nov-2005, 11:54
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: english to morse revisited


Quote:
Originally Posted by fadedg2
I recently had a post about this and am sort of revisting my problem in more detail now. i am trying to write a program that will convert English to morsecode. i am trying to make it where there will be a space between each morse equivalent, and will print to a new line when the output gets to the
80th column, but not starting a new line in the middle of the morse equivalent. for a simple test i use the string me[] = "greg".
i expect the results to be...
--. .-. . --.

i am not getting this result..
You forgot to tell us what result you do get... Guideline #2

CPP / C++ / C Code:
 static char morsecode[45][8] = {
      "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--..","0-----",
      "1.----","2..---","3...--","4....-","5.....","6-....","7--...",
      "8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
      "--....-","/-..-.","(-.--.-","\".-..-."};
Rather than this, just make the array a list of the Morse Code characters starting with SPACE (ASCII#32) thru the last character ASCII#126.

This way, any character in your buffer an be displayed/returned by
CPP / C++ / C Code:
ch = getchar();
printf("%s ",morsecode[ch-32]);
or better yet
CPP / C++ / C Code:
ch = getchar();
puts(morsecode[ch-32]);

Get a list of the ASCII characters in order and simply put the Morse Code string in the same position. For example
SPACE (32) 1st " "
Double Quote (34) 3rd, ".-..-. "
1 (49) 18th ".---- "
A (65) 34th ".- "
a (97) 66th ".- "
Something like that. I may not have the numbers exact (from memory) but this would give you the idea.

This will give you a list of all the ASCII characters in order:
CPP / C++ / C Code:
#include <stdio.h>
int main()
{
    int ch;
    printf("dec   hex \n");
    for (ch=32; ch < 127; ch++)
    {
        printf("%3d %c %02X \n", ch, ch, ch);
    }
}
__________________

Age is unimportant -- except in cheese
  #4  
Old 02-Nov-2005, 01:07
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road

Re: english to morse revisited


ok i am coming close. i didnt exactly do what you said to do. i kinda did it a different way. ive got it working correctly EXCEPT for the fact that i need 3 spaces between each work in the morse code transformation. for example, if i am trying to convert "greg glisch" to morse code i expect to get

--. .-. . --. {3 spaces here} --. .-.. .. ... -.-. ....

i am not getting this. i am getting

--. .-. . --. {1 space here } --. .-.. .. ... -.-. ....

can someone tell me why its not printing the 3 spaces between the 2 words? my code is as follows...

CPP / C++ / C Code:

#define MAX_STR_SIZE 80
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


 static char morsecode[45][8] = {
      "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--..","0-----",
      "1.----","2..---","3...--","4....-","5.....","6-....","7--...",
      "8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
      "--....-","/-..-.","(-.--.-","\".-..-."};
      
main()
{
      char me[] = "greg glisch";
      int i = 0;
      int j = 0;
      int k = 1;
      int q = 0;
      FILE *fout;
      fout = fopen("greg.out", "w");
      for (i=0; (i < (strlen(me)));i++)
      {   
          if (me[i] == NULL)
          {
             fprintf(fout,"\ ");
             fprintf(fout,"\ ");
             fprintf(fout,"\ ");
             q = q + 3;
          }
          else
          {       
             for (j=0; j<45;j++)
             {
              if (me[i] == morsecode[j][0])
              {   
                  for (k=1;k<7;k++)
                  {
                      if(morsecode[j][k]!= NULL)
                      {
                          q++;
                          if (q>74)
                          {
                          
                              fprintf(fout,"\n%c",morsecode[j][k]);
                              q = 0;
                          }
                          
                          else
                          
                              fprintf(fout,"%c",morsecode[j][k]);
                          
                      }
                      else
                      {
                          q++;
                          fprintf(fout,"\ ");
                          goto start;
                      }
                           
                  }
              }
              check: ;
          }
          start: ;
      }
}
}



some one please help me...i am at the end of the rope here. thank you in advance.
  #5  
Old 02-Nov-2005, 03:00
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: english to morse revisited


Quote:
Originally Posted by fadedg2
ive got it working correctly EXCEPT for the fact that i need 3 spaces between each work in the morse code transformation.

You need to check for space and insert three spaces to the output where encountered ...
Where are you checking for space ?

Kobi.

P.S

You are using an escape sequence that doesn't exist . Look at the warnings from the compiler.
__________________
It's actually a one time thing (it just happens alot).
  #6  
Old 02-Nov-2005, 03:08
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road

Re: english to morse revisited


i am checking for space here...

CPP / C++ / C Code:

if (me[i] == NULL)
          {
             fprintf(fout,"\ ");
             fprintf(fout,"\ ");
             fprintf(fout,"\ ");
             q = q + 3;
          }



now, when looking at this i did see that i was checking for NULL instead of ' '.
but i went back and changed it to test for space and it still doesnt work correctly.
  #7  
Old 02-Nov-2005, 04:16
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: english to morse revisited


This is how I would approach this problem :

CPP / C++ / C Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_MORSE_LENGTH	6
#define MORSE_ELEMENTS		36
#define MAX_LINE_LENGTH		80

typedef struct mnode
{
	char character;
	char morse[MAX_MORSE_LENGTH];
} morse_node;

/*Portion of the international morse code table, as described at
[url]http://en.wikipedia.org/wiki/Morse_code[/url] */
morse_node international_morse_table[36] = 
{{'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',"--.."},{'0',"-----"},{'1',".----"}, 
{'2',"..---"}, {'3',"...--"}, {'4',"....-"}, {'5',"....."}, {'6',"-...."}, {'7',"--..."}, 
{'8',"---.."}, {'9',"----."}};

morse_node *translate_character_to_morse(char character);
morse_node *translate_morse_to_character(char morse[MAX_MORSE_LENGTH]);

int main()
{
	unsigned i;
	char line[MAX_LINE_LENGTH];
	morse_node *current;
	printf("Please enter line to translate into morse code : \n");
	fgets(line,MAX_LINE_LENGTH,stdin);
	
	for (i = 0;i < strlen(line);i++)
	{
		current = translate_character_to_morse(line[i]); 
		if (current != NULL)
		{
			printf("%s ",current->morse);
		}
		else
		{
			if (line[i] == ' ')
				printf ("   ");
		}
	}
}

morse_node *translate_character_to_morse(char character)
{
	morse_node *result = NULL;
	unsigned i;
	for (i = 0;i < MORSE_ELEMENTS;i++)
		if (character == international_morse_table[i].character)
		{
			/*Allocate memory for morse node.*/
			result = malloc(sizeof(struct mnode));
			if (result != NULL)
			{
				result->character = international_morse_table[i].character;
				strcpy(result->morse,international_morse_table[i].morse);
			}
			else
			{
				fprintf (stderr,"Couldn't allocate morse node.\n");
			}
		}
	return (result);
}

morse_node *translate_morse_to_character(char morse[MAX_MORSE_LENGTH])
{
	morse_node *result = NULL;
	unsigned i;
	for (i = 0;i < MORSE_ELEMENTS;i++)
		if (strcmp(morse,international_morse_table[i].morse) == 0)
		{
			/*Allocate memory for morse node.*/
			result = malloc(sizeof(struct mnode));
			if (result != NULL)
			{
				result->character = international_morse_table[i].character;
				strcpy(result->morse,international_morse_table[i].morse);
			}
			else
			{
				fprintf (stderr,"Couldn't allocate morse node.\n");
			}
		}
	return (result);
}

Does this approach seem simpler ? to me it does.
Functions are very convenient to use if you define them correctly. Note that I didn't do much work after defining the conversion functions...

Try to use the conversion from morse to english on your own ...

Best regards,
Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #8  
Old 02-Nov-2005, 17:36
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road

Re: english to morse revisited


ok...
this "program' that i am writing is eventually going to be converted into a function. i am just running it as a program at the moment so i can see my results. the code below is going to become a function called engltomorse() when i get it working correctly. all i need for this to happen is to have the printing start printing to a new line when the output of the morse line becomes greater than 80. BUT, it must not start the new line in the middle of the morsecoded word. so somehow i need to check to see that the position of the printer plus the length of the next "morse word is < 80. if it is i may print it on the same line, if not i must start a new line in the very first column. my code is below. suggetstions are greatly appreciated.

CPP / C++ / C Code:

static char morsecode[45][8] = {
      "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--..","0-----",
      "1.----","2..---","3...--","4....-","5.....","6-....","7--...",
      "8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
      "--....-","/-..-.","(-.--.-","\".-..-."};
      
main()
{
      char me[] = "this program is making me angry.";
      int i = 0;
      int j = 0;
      int k = 1;
      int q = 0;
      int b;
      FILE *fout;
      fout = fopen("greg.out", "w");
      for (i=0; (i < (strlen(me)));i++)
      {
          if ( me[i] == ' ')
          {
               fprintf(fout,"\ ");
               fprintf(fout,"\ ");
               q = q + 2;
          }     
          for (j=0; j<45;j++)
             {
              if (me[i] == morsecode[j][0])
              {   
                  for (k=1;k<7;k++)
                  {
                      if(morsecode[j][k]!= NULL)
                      {
                          q++;
                          
                          
                              fprintf(fout,"%c",morsecode[j][k]);
                          
                      }
                      else
                      {
                          q++;
                          fprintf(fout,"\ ");
                          if (q > 73)
                          {
                              fprintf(fout,"\n");
                              q = 0;
                          }
                                
                          goto start;
                      }
                           
                  }
              }
              check: ;
          }
          start: ;
      }
}



thank you in advance.
  #9  
Old 02-Nov-2005, 22:14
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Re: english to morse revisited


Hey Greg.

You are very stubborn. I like that.
What you need to do is count the number of characters you print.
Do so by declaring a counter, and each time you print something, add it's length to the counter.
Now - Before you actually print anything, check the counter. If it's length is 80 or more, set it back to 0 and start a new line.

1. I intentionally didn't write code. You seem to know how to use counters and strlen.
2. Try to get rid of the compiler warnings, you will find that they are very logical ... (Ask me how, if you try and fail).

Best regards,
Kobi.
__________________
It's actually a one time thing (it just happens alot).
  #10  
Old 02-Nov-2005, 22:32
fadedg2 fadedg2 is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
fadedg2 is on a distinguished road

Re: english to morse revisited


kobi... or anyone...
i have just finished "debugging" my code when you replied. now..i know my problem is that i am printing the characters to the screen while i am testing them. but, i am completly stumped as of how to keep my code " the same" and test the future length of the words without printing. all i can think of is some type of loop somewhere. here are the existing code below.

CPP / C++ / C Code:

#define MAX_STR_SIZE 80
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


static char morsecode[45][8] = {
      "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--..","0-----",
      "1.----","2..---","3...--","4....-","5.....","6-....","7--...",
      "8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
      "--....-","/-..-.","(-.--.-","\".-..-."};
      
main()
{
      char me[] = "a naked fairy was named markymark666";
                   /*i know this phrase is a little weird but it fits in the scenario*/
      int i = 0;            
      int j = 0;
      int k = 1;
      int q = 0;
      int b=0;
      FILE *fout;
      fout = fopen("greg.out", "w");
      for (i=0; (i < (strlen(me)));i++)
      {
          if ( me[i] == ' ')
          {
               fprintf(fout,"\ ");
               fprintf(fout,"\ ");
               q = q + 2;
               b=0;
               i++;
               
          }     
          for (j=0; j<45;j++)
          {
              if (me[i] == morsecode[j][0])
              {   
                  for (k=1;k<7;k++)
                  {
                      if(morsecode[j][k]!= NULL)
                      {
                          q++;
                          
                          
                              
                              if ( (b + q)> 80)
                              {
                                   fprintf(fout,"\n");
                                   q = 0;
                                   b = 0;
                              }
                              else
                              {
                              fprintf(fout,"%c",morsecode[j][k]);
                              b++;
                              }
                      }
                      else
                      {
                          q++;
                          fprintf(fout,"\ ");
                          b++;
                          goto start;
                      }
                           
                  }
              }
              check: ;
          }
          start: ;
      }
}


I GREATLY appreciate your help. please reply soon. time is starting to become crucial.
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help converting between morse and english fadedg2 C Programming Language 2 29-Oct-2005 10:02
Dynamic memory allocation - Dangling pointers revisited karthikeyansen C++ Forum 13 27-Jul-2005 15:30
D3D error when running fullscreen games/programs - help?! daa709 Computer Hardware Forum 4 01-Jul-2005 08:03
Morse Code for C sibyl03 C Programming Language 1 13-Oct-2004 00:00
For the 'Johny English' in you JdS Open Discussion Forum 3 09-May-2003 04:49

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.