•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,191 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 2,228 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: 2785 | Replies: 58 | Solved
![]() |
•
•
•
•
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.
This depends on how you define error. Since we don't know your definition, can't help.
Age is unimportant -- except in cheese
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
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?
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?
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 4:15 pm.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
I would like to post the final solution that i now have... so as to help others!
C Syntax (Toggle Plain Text)
#include "stdafx.h" #include "stdio.h" #include "ctype.h" #include "string.h" #include "stdlib.h" void encode ( char ch ); //encode function void enter(); // text entry function void file(); // read from a file function void main_menu(); 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 _tmain(int argc, _TCHAR* argv[]) { main_menu(); } void main_menu() { //main menu int opt; printf("################################################\n"); printf("## ##\n"); printf("## Main Menu ##\n"); printf("## ##\n"); printf("################################################\n"); printf("## Please Select An Option To Proceed ##\n"); printf("## 1. Convert keyboard entry to morse code ##\n"); printf("## 2. Convert a file to morse code ##\n"); printf("## 3. Exit ##\n"); printf("################################################\n\n"); printf("Enter an Option: "); scanf_s("%d",&opt); if ((opt!= 1) && (opt != 2) && (opt != 3)) { printf("\nInvalid Option..... Try Again: "); //checks to see a valid option is entered scanf_s("%d",&opt); } else if (opt == 1) { enter(); } else if (opt == 2) { file(); } else if (opt == 3) { exit(0); } } void enter() { char text[500]; // 250 buffer for text fputs ( "Please Enter The Phrase You Would Like To Convert: ", stdout ); gets(text); // prints the message out and gets the input fflush ( stdout ); //clears stdout if ( fgets ( text, sizeof text, stdin ) != NULL ) // checks to see test is not null { char *newline = strchr ( text, '\n' ); /* search for newline character */ if ( newline != NULL ) { *newline = '\0'; /* overwrite trailing newline */ } printf ( "Trying To Convert = \"%s\" To Morse\n", text ); } putchar ( '\n' ); int j; for ( j = 0; j < strlen ( text ); j++ ) // loops through the string { encode ( toupper ( text[j] ) ); //calls the encode function after changing the case of the char } getchar(); putchar ( '\n' ); //new line main_menu(); //calls main menu } void encode ( char ch ) { size_t i; if((isalpha(ch)) || (isspace(ch))) { for ( i = 0; i < 26; ++i ) { //loops through the aplh[] if ( ch == alph[i] ) { //check to see the character is present in alph[] printf ( "%s ", morsecode[i] ); //gets the morse index of that char break; } } } else { printf("\nInput %c Not Valid, Error File Generated. Check C\\errors.txt: \n", ch); FILE *fp; /* open the file */ fp = fopen("C:\\errors.txt", "a"); if (fp == NULL) { printf("I couldn't open errors.txt for writing.\n"); exit(0); } fprintf(fp, "%c\n", ch); /* close the file */ fclose(fp); } } void file() { FILE *fp; //pointer to file char buf[BUFSIZ]; //define buf with bufsize of 512 int i; char text[100]; fputs ( "Enter the path and file name that you want to read from: ", stdout ); gets(text); //asks for the path to the file fflush ( stdout ); //clear stdout gets(text); if ( ( fp = fopen ( text, "r" ) ) == NULL ) //check to see if the file exists { perror ( text ); } i = 0; while ( fgets ( buf, sizeof ( buf ), fp ) != NULL ) //gets the contents of the file assigns to buf { int j; int foo = strlen ( buf ); // gets length of the string in the file printf("Contents of the file: \"%s\" \n",buf); //prints the string from the file for ( j = 0; j < foo; j++ ) //loops throught the string { encode ( toupper ( buf[j] ) ); //calls encode for each char } } fclose ( fp ); //closes file putchar('\n'); getchar(); main_menu(); }
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.
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 5:13 pm.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
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 ...
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 ...
>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.
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 5:40 pm.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- help needed on a loop again (C++)
- Square root program without sqrt or pwr (C++)
- IT project help (Pascal and Delphi)
- open a file and divide its data in arrays (C)
- C++ nest loop program - Help (C++)
- I don't know where is the question occured. (C)
- sorting parallel arrays (C)
Other Threads in the C Forum
- Previous Thread: HELP: Day 0 - Chroma Key Programming
- Next Thread: Pascal's Triangle in C



Linear Mode