User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 392,098 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,820 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 2525 | Replies: 58 | Solved
Reply
Join Date: Nov 2007
Posts: 31
Reputation: r5ingh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
r5ingh r5ingh is offline Offline
Light Poster

Re: Help needed with loop.

  #11  
Nov 24th, 2007
ok at runtime, if i enter "hello" in the console. the program should return "morse: .... . .-.. .-.. ---"currently it deosnt return anything. just ends.
Last edited by r5ingh : Nov 24th, 2007 at 12:30 pm.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,668
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 17
Solved Threads: 298
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #12  
Nov 24th, 2007
And this one...

void encode ( char ch );
char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/* morsecode is an array of pointers to char. It is initialized
* so that each element points to a string literal with
* the ascii representation of each char of the array alph[]
*/

char *morsecode[] =
  { /* an array of pointers to char */
    ".- "  , "-..." , "-.-." , "-.."  , "."    ,    /* a-e */
    "..-." , "--."  , "...." , ".."   , ".---" ,    /* f-j */
    "-.-"  , ".-.." , "--"   , "-."   , "---"  ,    /* k-o */
    ".--." , "--.-" , ".-."  , "..."  , "-"    ,    /* p-t */
    "..-"  , "...-" , ".--"  , "-..-" , "-.--" ,    /* u-y */
    "--.." ,                                        /* z   */
  };
int main ( void )
{ 
  char text[100];
  fputs ( "enter some text to convert to morse: ", stdout );
  fflush ( stdout );
  if ( fgets ( text, sizeof text, stdin ) != NULL )
  {
    char *newline = strchr ( text, '\n' ); /* search for newline character */
    if ( newline != NULL )
    {
      *newline = '\0'; /* overwrite trailing newline */
    }
    printf ( "text = \"%s\"\n", text );
  }

  int j;
  int foo = strlen(text); 
  for ( j = 0; j < foo; j++ )
  {
    encode ( toupper ( text[j] ) );
  }
  getchar();
  getchar();
  return 0;
}

void encode ( char ch )
{
  size_t i, j;
  for ( i = 0; i < 26; ++i )
  {
    if ( ch == alph[i] )
    {
      printf ( "%s", morsecode[i] );
      break;
    }
  }
  putchar ( '\n' );
}
Last edited by iamthwee : Nov 24th, 2007 at 12:45 pm.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,775
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 330
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Help needed with loop.

  #13  
Nov 24th, 2007
r5ing:

A few things:
» Don't use scanf or it's non-standard variants for accepting user input. There are better ways of achieving it. Read this.

» The way you expose the interface doesn't seem to be right. Make encode() return a char array instead of nothing.

» Also since Morse code is case insensitive you might consider converting the user input to uppercase before applying encode function to it.
  1. > char *ch;
  2. > printf("please enter char to convert to morse: ");
  3. > scanf_s("%s", &ch);
How do you expect this to work? You are assigning the string entered by the user to something which doesn't belong to you. You need to allocate enough memory. Something like char ch[BUFSIZ] = { 0 }; Plus scanf requires you pass the starting address of the memory location. Since ch itself is a char array you just need to pass ch instead of &ch.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Aug 2005
Posts: 4,668
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 17
Solved Threads: 298
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #14  
Nov 24th, 2007
well?
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Nov 2007
Posts: 31
Reputation: r5ingh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
r5ingh r5ingh is offline Offline
Light Poster

Re: Help needed with loop.

  #15  
Nov 24th, 2007
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooo!
this works!!!
returns each char on a line. will chage this to return onto one line
Reply With Quote  
Join Date: Nov 2007
Posts: 31
Reputation: r5ingh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
r5ingh r5ingh is offline Offline
Light Poster

Re: Help needed with loop.

  #16  
Nov 24th, 2007
S.O.S Thank you for the tips. i will read up on this. seems a bit advanced for me!! im a complete beginner!
Reply With Quote  
Join Date: Aug 2005
Posts: 4,668
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 17
Solved Threads: 298
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #17  
Nov 24th, 2007
Mark this as being solved. (bottom rhs)

Next time use standard c and intent your code with spaces instead of tabs.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Nov 2007
Posts: 31
Reputation: r5ingh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
r5ingh r5ingh is offline Offline
Light Poster

Re: Help needed with loop.

  #18  
Nov 24th, 2007
I also need to be able to read input from a file. I have wrtten read methods for java, where you can define how data is parsed. but i do not know how to write the file read method in C. How will i parse the data into the program as a string?
Reply With Quote  
Join Date: Aug 2005
Posts: 4,668
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 17
Solved Threads: 298
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #19  
Nov 24th, 2007
What have you got so far?
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Nov 2007
Posts: 31
Reputation: r5ingh is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
r5ingh r5ingh is offline Offline
Light Poster

Re: Help needed with loop.

  #20  
Nov 24th, 2007
  1.  
  2.  
  3. void encode ( char ch );
  4. char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  5. /* morsecode is an array of pointers to char. It is initialized
  6. * so that each element points to a string literal with
  7. * the ascii representation of each char of the array alph[]
  8. */
  9.  
  10. char *morsecode[] =
  11. { /* an array of pointers to char */
  12. ".- " , "-..." , "-.-." , "-.." , "." , /* a-e */
  13. "..-." , "--." , "...." , ".." , ".---" , /* f-j */
  14. "-.-" , ".-.." , "--" , "-." , "---" , /* k-o */
  15. ".--." , "--.-" , ".-." , "..." , "-" , /* p-t */
  16. "..-" , "...-" , ".--" , "-..-" , "-.--" , /* u-y */
  17. "--.." , /* z */
  18. };
  19. int main ( void )
  20. {
  21. for (;;) {
  22. printf("################################################\n");
  23. printf("## ##\n");
  24. printf("## Main Menu ##\n");
  25. printf("## ##\n");
  26. printf("################################################\n");
  27. printf("## Options: Type the option no: to proceed ##\n");
  28. printf("## 1. Convert keyboard entry to morse code ##\n");
  29. printf("## 2. Convert a file to morse code ##\n");
  30. printf("## 3. Type Quit To Exit ##\n");
  31. printf("################################################\n\n");
  32.  
  33. printf("## Enter an Option: ##\n");
  34.  
  35. }
  36.  
  37.  
  38. char text[150];
  39. fputs ( "enter some text to convert to morse: ", stdout );
  40. fflush ( stdout );
  41. if ( fgets ( text, sizeof text, stdin ) != NULL )
  42. {
  43. char *newline = strchr ( text, '\n' ); /* search for newline character */
  44. if ( newline != NULL )
  45. {
  46. *newline = '\0'; /* overwrite trailing newline */
  47. }
  48. printf ( "text = \"%s\"\n", text );
  49. }
  50. putchar ( '\n' );
  51.  
  52. int j;
  53. for ( j = 0; j < strlen ( text ); j++ )
  54. {
  55. encode ( toupper ( text[j] ) );
  56. }
  57. getchar();
  58. getchar();
  59. return 0;
  60. }
  61.  
  62. void encode ( char ch )
  63. {
  64. size_t i;
  65. for ( i = 0; i < 26; ++i )
  66. {
  67. if ( ch == alph[i] )
  68. {
  69. printf ( "%s ", morsecode[i] );
  70. break;
  71. }
  72. }
  73.  
  74. }


I need to get it to be something like this. so that i can meet the minimum requirements of the assessment.

i am thinking of useing a switch case statement, to call the encode function and i will write a separate function for the file read method.

iamthwee: i will mark it as solved if i do not need further assistance! Thanks for your help so far. I REALLY appreciate it!
Last edited by r5ingh : Nov 24th, 2007 at 12:52 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 12:48 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC