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 455,973 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,828 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: Programming Forums
Views: 3013 | Replies: 58 | Solved
Reply
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #41  
Nov 24th, 2007
>im guessing that it cannot convert the string because encode takes characters

Correct, so what do you think you must do?

[edit] don't use gets, it's real bad, use the example I showed you before.
Last edited by iamthwee : Nov 24th, 2007 at 3:54 pm.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #42  
Nov 24th, 2007
Look i'm running out of time this should be the final one. I don't have a compiler to test it with so it might not work...

void encode ( char ch );
char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char *morsecode[] =
  {

    ".- "  , "-..." , "-.-." , "-.."  , "."    ,

    "..-." , "--."  , "...." , ".."   , ".---" ,

    "-.-"  , ".-.." , "--"   , "-."   , "---"  ,

    ".--." , "--.-" , ".-."  , "..."  , "-"    ,

    "..-"  , "...-" , ".--"  , "-..-" , "-.--" ,

    "--.." ,

  }; 
int main ( void )
{
  FILE *fp;
  char buf[BUFSIZ] = "crrrrrrrappp";
  int i;
  char text[100];

  fputs ( "Enter the path of the file name you wanna read from: ", stdout );
  fflush ( stdout );
  if ( fgets ( text, sizeof text, stdin ) != NULL )
  {
    char *newline = strchr ( text, '\n' );

    if ( newline != NULL )
    {
      *newline = '\0';
    }
    printf ( "text = \"%s\"\n", text );
  }

  if ( ( fp = fopen ( text, "r" ) ) == NULL )
  {
    perror ( text );
    return ( EXIT_FAILURE );
  }

  i = 0;

  while ( fgets ( buf, sizeof ( buf ), fp ) != NULL )
  {
    int j;
    int foo = strlen ( buf );
    for ( j = 0; j < foo; j++ )
    {
      encode ( toupper ( buf[j] ) );
    }
  }

  fclose ( fp );

  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' );
}
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #43  
Nov 24th, 2007
what happened?
... the hat of 'is this a cat in a hat?'
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.

  #44  
Nov 24th, 2007
nah. this solution encodes the path!
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.

  #45  
Nov 24th, 2007
nvm i've got it working!
just need to print the text from the file now

^^ done this too!
thanks so much for the help mate
appreciate it
Last edited by r5ingh : Nov 24th, 2007 at 4:12 pm.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #46  
Nov 24th, 2007
what output do you get?
... the hat of 'is this a cat in a hat?'
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.

  #47  
Nov 24th, 2007


This is what it outputs now.. then loops back to the main menu!
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Help needed with loop.

  #48  
Nov 24th, 2007
Sorry I have to go. You should have enough info to complete your program.bye
... the hat of 'is this a cat in a hat?'
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.

  #49  
Nov 24th, 2007
Yes, I now have enough to get a C grade.. Thank you SO MUCH matey.
Really appreciate your help. I will post the final solution if i add anything onto it. so that others can also use this.
Last edited by r5ingh : Nov 24th, 2007 at 4:20 pm.
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.

  #50  
Nov 24th, 2007
  1. #include "stdafx.h"
  2. #include "stdio.h"
  3. #include "ctype.h"
  4. #include "string.h"
  5. #include "stdlib.h"
  6.  
  7. void encode ( char ch ); //encode function
  8. void enter(); // text entry function
  9. void file(); // read from a file function
  10. void main_menu();
  11.  
  12. char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  13. /* morsecode is an array of pointers to char. It is initialized
  14. * so that each element points to a string literal with
  15. * the ascii representation of each char of the array alph[]
  16. */
  17.  
  18. char *morsecode[] =
  19. { /* an array of pointers to char */
  20. ".- " , "-..." , "-.-." , "-.." , "." , /* a-e */
  21. "..-." , "--." , "...." , ".." , ".---" , /* f-j */
  22. "-.-" , ".-.." , "--" , "-." , "---" , /* k-o */
  23. ".--." , "--.-" , ".-." , "..." , "-" , /* p-t */
  24. "..-" , "...-" , ".--" , "-..-" , "-.--" , /* u-y */
  25. "--.." , /* z */
  26. };
  27.  
  28. int _tmain(int argc, _TCHAR* argv[]) {
  29. main_menu();
  30. }
  31.  
  32.  
  33. void main_menu() {
  34. //main menu
  35. int opt;
  36. printf("################################################\n");
  37. printf("## ##\n");
  38. printf("## Main Menu ##\n");
  39. printf("## ##\n");
  40. printf("################################################\n");
  41. printf("## Please Select An Option To Proceed ##\n");
  42. printf("## 1. Convert keyboard entry to morse code ##\n");
  43. printf("## 2. Convert a file to morse code ##\n");
  44. printf("## 3. Exit ##\n");
  45. printf("################################################\n\n");
  46.  
  47. printf("Enter an Option: ");
  48. scanf_s("%d",&amp;opt);
  49.  
  50. if ((opt!= 1) &amp;&amp; (opt != 2) &amp;&amp; (opt != 3)) {
  51. printf("\nInvalid Option..... Try Again: "); //checks to see a valid option is entered
  52. scanf_s("%d",&amp;opt);
  53. }
  54.  
  55. else if (opt == 1) {
  56. enter();
  57. }
  58. else if (opt == 2) { file();}
  59.  
  60. else if (opt == 3) { exit;}
  61. }
  62.  
  63. void enter() {
  64.  
  65. char text[250]; // 250 buffer for text
  66. fputs ( "Please Enter The Phrase You Would Like To Convert: ", stdout );
  67. gets(text); // prints the message out and gets the input
  68. fflush ( stdout ); //clears stdout
  69. if ( fgets ( text, sizeof text, stdin ) != NULL ) // checks to see test is not null
  70. {
  71. char *newline = strchr ( text, '\n' ); /* search for newline character */
  72. if ( newline != NULL )
  73. {
  74. *newline = '\0'; /* overwrite trailing newline */
  75. }
  76. printf ( "Morse Conversion Of = \"%s\"\n", text );
  77. }
  78. putchar ( '\n' );
  79.  
  80. int j;
  81. for ( j = 0; j &lt; strlen ( text ); j++ ) // loops through the string
  82. {
  83. encode ( toupper ( text[j] ) ); //calls the encode function after chnaging the case of the char
  84. }
  85. getchar();
  86. putchar ( '\n' ); //new line
  87. main_menu(); //calls main menu
  88. }
  89.  
  90. void encode ( char ch ) {
  91. size_t i;
  92. for ( i = 0; i &lt; 26; ++i ) //loops through the aplh[]
  93. {
  94. if ( ch == alph[i] ) //check to see the character is present in alph[]
  95. {
  96. printf ( "%s ", morsecode[i] ); //gets the morse index of that char
  97. break;
  98. }
  99. }
  100. }
  101.  
  102. void file() {
  103. FILE *fp; //pointer to file
  104. char buf[BUFSIZ]; //define buf with bufsize of 512
  105. int i;
  106. char text[100];
  107.  
  108. fputs ( "Enter the path and file name that you want to read from: ", stdout );
  109. gets(text); //asks for the path to the file
  110. fflush ( stdout ); //clear stdout
  111. gets(text);
  112.  
  113. if ( ( fp = fopen ( text, "r" ) ) == NULL ) //check to see if the file exists
  114. {
  115. perror ( text );
  116. }
  117.  
  118. i = 0;
  119.  
  120. while ( fgets ( buf, sizeof ( buf ), fp ) != NULL ) //gets the contents of the file assigns to buf
  121. {
  122. int j;
  123. int foo = strlen ( buf ); // gets length of the string in the file
  124.  
  125. printf("Contents of the file: \"%s\" \n",buf); //prints the string from the file
  126. for ( j = 0; j &lt; foo; j++ ) //loops throught the string
  127. {
  128. encode ( toupper ( buf[j] ) ); //calls encode for each char
  129. }
  130. }
  131.  
  132. fclose ( fp ); //closes file
  133. putchar('\n');
  134. getchar();
  135. main_menu();
  136.  
  137. }
  138.  

im confused as to how to write an error checking function and how to write a decode function which decodes morse code. It cannot simply be a backwards of the encode function because ONE morse code is made up of FOUR characters...? (if you get what i mean?) i am not allowed to use a 2-d array... is there any other way to do this?

Error checking? :: my thought on this is that i should pass the entered string to a new function which loops through the string, filtering out errors... however. i do not know how to start coding this function.

I would appreciate some insight into how i can go about any of these two problems.
Reply With Quote  
Reply

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

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

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

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