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: May 2006
Posts: 2,779
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Help needed with loop.

  #51  
Nov 24th, 2007
Originally Posted by r5ingh View Post
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?
Why can't it? You know the length of your input value (.- is 2, -.-- is 4). So look for a match that matches both characters and length. (.-- and .-.- would be rejected for .- because neither are two characters.

Originally Posted by r5ingh View Post
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.

This depends on how you define error. Since we don't know your definition, can't help.
Age is unimportant -- except in cheese
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.

  #52  
Nov 25th, 2007
i mean error checking as in that the program should only pass, valid (in this case aplhabet characters) to the encode function.for instance if a user enters "hi. what's going on?"
the error function should reject this string and create an output file of the punctuation errors in it. in this example error file would include ". ' ?"

i was thinking that i would have another function which loops through the string and checkes if it matches the ALPH[]... if something which deosnt match then it would be written to file. .. how deos this approach sound?
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.

  #53  
Nov 26th, 2007
You should use your initiative to solve your problem. You have more than enough information to get continue.
Last edited by iamthwee : Nov 26th, 2007 at 5:15 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.

  #54  
Nov 26th, 2007
yea!
thanks
i've figured it out!!!
got to thank you once agin though matey.. saved my bacon!
Last edited by r5ingh : Nov 26th, 2007 at 5:16 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.

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

  #56  
Nov 26th, 2007
You should not use gets() for user input. I have already shown you how you use fgets() to achieve the same thing. You should use it all the time.

http://www.daniweb.com/tutorials/tutorial45806.html

I understand from a student's point of view getting the thing to work is the first thing you worry about. However, when you enter the real world you need to learn to write code that is safe and less likely to be exploited.

Another thing worth mentioning, is that you code is not standard c, meaning that it will fail if you try and compile it with gcc for example. Again, it probably doesn't concern you much from a student's point of view.
Last edited by iamthwee : Nov 26th, 2007 at 6:13 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.

  #57  
Nov 26th, 2007
as you have said. Yes.. the students perspective is there.. but since i am thinking very seriously of taking up programming professionally, i really appreciate the advice.. it would help if i could get into good programming habbits from now. i will be looking into this further.

Its a strange module because this is the only C assessment. after this i have to code another small program for assessment but that is using win main.. which is c++?. My lecturers have really cofused me as to what im studying. Things are moving so fast that i cant hang around one topic for too long. which is the reason why i dont really know whats going on my self!!

However. The forum has been very helpful. I've managed to make sense of a lot of things now ...

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.

  #58  
Nov 26th, 2007
>that is using win main.. which is c++?.
No, not necessarily, you can use c as well with win32. Think of win32 as a separate entity, severed from either c or c++. You use one or the other to help make the entire program.

>it would help if i could get into good programming habbits from now
It is always good to adopt good programming strategies. Unfortunately teachers fail to see this. It's a struggle between teaching theory and practise. Which is more important from a student's perspective?

I don't program seriously, this is nothing more than a hobby I do after school. But if you want to learn the semantics of the 'c' language stick around. I have learnt from others, just like you have learnt from me.

>However. The forum has been very helpful. I've managed to make sense of a lot of things now

I'm glad. This, after all, is why we're here. When our posts become smart assed one liners, we loose our purpose, and become nothing more than contemptible.
Last edited by iamthwee : Nov 26th, 2007 at 6:40 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.

  #59  
Nov 26th, 2007
Im definately Sticking around matey! You will see a lot more of me from now on!
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