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,819 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.

  #21  
Nov 24th, 2007
where are you reading in the file?

And why did you use strlen() within the for loop.
http://www.cprogramming.com/tips/sho...ount=30&page=1
Last edited by iamthwee : Nov 24th, 2007 at 1:55 pm.
... 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.

  #22  
Nov 24th, 2007
i have to write that function yet. I didnt get that far bcuz i was stuck on the string bit. lol
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.

  #23  
Nov 24th, 2007
Oh right, well when you've written it, come back here.
... 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.

  #24  
Nov 24th, 2007
sure mate. Coding it now
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.

  #25  
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.  
  8.  
  9. void encode ( char ch ); //encode function
  10. void enter(); // text entry function
  11. void file(); // read from a file function
  12. void main_menu();
  13.  
  14. char alph[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  15. /* morsecode is an array of pointers to char. It is initialized
  16. * so that each element points to a string literal with
  17. * the ascii representation of each char of the array alph[]
  18. */
  19.  
  20. char *morsecode[] =
  21. { /* an array of pointers to char */
  22. ".- " , "-..." , "-.-." , "-.." , "." , /* a-e */
  23. "..-." , "--." , "...." , ".." , ".---" , /* f-j */
  24. "-.-" , ".-.." , "--" , "-." , "---" , /* k-o */
  25. ".--." , "--.-" , ".-." , "..." , "-" , /* p-t */
  26. "..-" , "...-" , ".--" , "-..-" , "-.--" , /* u-y */
  27. "--.." , /* z */
  28. };
  29.  
  30.  
  31. int _tmain(int argc, _TCHAR* argv[]) {
  32. main_menu();
  33. }
  34.  
  35.  
  36. void main_menu() {
  37. int opt;
  38. printf("################################################\n");
  39. printf("## ##\n");
  40. printf("## Main Menu ##\n");
  41. printf("## ##\n");
  42. printf("################################################\n");
  43. printf("## Please Select An Option To Proceed ##\n");
  44. printf("## 1. Convert keyboard entry to morse code ##\n");
  45. printf("## 2. Convert a file to morse code ##\n");
  46. printf("## 3. Exit ##\n");
  47. printf("################################################\n\n");
  48.  
  49. printf("Enter an Option: ");
  50. scanf_s("%d",&opt);
  51.  
  52. if ((opt!= 1) && (opt != 2) && (opt != 3)) {
  53. printf("\nInvalid Option..... Try Again: ");
  54. scanf_s("%d",&opt);
  55. }
  56.  
  57. else if (opt == 1) {
  58. enter();
  59. }
  60. else if (opt == 2) { file();}
  61.  
  62. else if (opt == 3) { exit;}
  63. }
  64.  
  65. void enter() {
  66.  
  67. char text[150];
  68. fputs ( "Please Enter The Phrase You Would Like To Convert: ", stdout );
  69. gets(text);
  70. fflush ( stdout );
  71. if ( fgets ( text, sizeof text, stdin ) != NULL )
  72. {
  73. char *newline = strchr ( text, '\n' ); /* search for newline character */
  74. if ( newline != NULL )
  75. {
  76. *newline = '\0'; /* overwrite trailing newline */
  77. }
  78. printf ( "More Conversion Of = \"%s\"\n", text );
  79. }
  80. putchar ( '\n' );
  81.  
  82. int j;
  83. for ( j = 0; j < strlen ( text ); j++ )
  84. {
  85. encode ( toupper ( text[j] ) );
  86. }
  87. getchar();
  88. putchar ( '\n' );
  89. main_menu();
  90. }
  91.  
  92.  
  93. void encode ( char ch ) {
  94. size_t i;
  95. for ( i = 0; i < 26; ++i )
  96. {
  97. if ( ch == alph[i] )
  98. {
  99. printf ( "%s ", morsecode[i] );
  100. break;
  101. }
  102. }
  103. }
  104.  
  105. void file() {
  106. char path[150];
  107. fputs ( "Please Enter The Path Of The File You Would Like To Read From: ", stdout );
  108. gets(path);
  109. fflush ( stdout );
  110. if ( fgets ( path, sizeof path, stdin ) != NULL ) {
  111.  
  112. char c; /* Character read from the file. */
  113. FILE *ptr; /* Pointer to the file. FILE is a
  114. structure defined in <stdio.h> */
  115.  
  116. /* Open the file - no error checking done */
  117. ptr = fopen(path,"r");
  118. /* Read one character at a time, checking
  119. for the End of File. EOF is defined
  120. in <stdio.h> as -1 */
  121. while ((c = fgetc(ptr)) != EOF)
  122. {
  123. encode(c);
  124. }
  125.  
  126. fclose(ptr); /* Close the file. */
  127. }
  128. }


heya. Been working on this.
Got the menu to work but the solution no longer works properly!
It only converts the frist letter of the string now....
Also the file read method causes the program to crash.. What am i doing wrong?

EDIT** Fixed the problem. Converts Fine now, however the read file method is not working.
Last edited by r5ingh : Nov 24th, 2007 at 3:05 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.

  #26  
Nov 24th, 2007
What does your input file look like. Post an example.
... 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.

  #27  
Nov 24th, 2007
The input file will be a text file containing the data string to convert.
eg: when prompted user enters file path of c:\data.text
which contains the line "this is a english to morse conversion"
...
the program should convert that entire line using the encode function.
Hope that is helpful... BTW. Got the conversion working now thanks
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.

  #28  
Nov 24th, 2007
Keep it simple, do one thing at a time. Try to read in a file line by line. Then see if you can adapt that to include your morse program code.

#include <stdio.h> 
#include <stdlib.h> 

#define MYFILE "c:\\data.txt" 

int main(void)
{
  FILE *fp;
  char buf[BUFSIZ] = "crrrrrapppp";
  int i;
  
  if ((fp = fopen(MYFILE, "r")) == NULL)
  {
    perror (MYFILE);
    return (EXIT_FAILURE);
  }
  
  i = 0;

  while (fgets(buf, sizeof(buf), fp) != NULL)
  {
    printf ("Line %4d: %s", i, buf);
    i++;
  }
  
  fclose(fp);
    
  return(0);
}
... 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.

  #29  
Nov 24th, 2007
well?
... 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.

  #30  
Nov 24th, 2007
yea.. im trying to get this to work.. get an error at the moment. will post in two mins

Says no such file currently. although the file exists!
Last edited by r5ingh : Nov 24th, 2007 at 3:24 pm.
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:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC