•
•
•
•
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
![]() |
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?'
•
•
Join Date: Nov 2007
Posts: 31
Reputation:
Rep Power: 1
Solved Threads: 0
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;} } void enter() { char text[250]; // 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 ( "Morse Conversion Of = \"%s\"\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 chnaging the case of the char } getchar(); putchar ( '\n' ); //new line main_menu(); //calls main menu } void encode ( char ch ) { size_t i; 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; } } } 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(); }
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.
![]() |
•
•
•
•
•
•
•
•
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