C Programming Help

ConcernedMember
10+ year member

******
Part of the requirement of my program is to convert a word to morse code. By first storing the word entered in an array, and then comparing each letter in the array to the correct morse code for that letter and storing it in a second array. The first part is working fine, the array is storing hte word entered, but I can't get it to convert to morse code. Any help is greatly appreciated.

#include

#include

#define SMALL 80

#define BIG 320

void main()

{

char sentence[sMALL];

char morsecode[bIG]=" ";

int i,

num=0;

printf("Enter your info");

scanf("%s",sentence);

printf("%s",sentence);

num=strlen(sentence);

printf("%d", num);

for(i=0;i

{

if(sentence=='a')

strcat(morsecode,".-");

strcat(morsecode," ");

if(sentence=='b')

strcat(morsecode,"-...");

strcat(morsecode," ");

if(sentence=='c')

strcat(morsecode,"-.-.");

strcat(morsecode," ");

if(sentence=='d')

strcat(morsecode,"-..");

strcat(morsecode," ");

if(sentence=='e')

strcat(morsecode,".");

strcat(morsecode," ");

if(sentence=='f')

strcat(morsecode,"..-.");

strcat(morsecode," ");

if(sentence=='g')

strcat(morsecode,"--.");

strcat(morsecode," ");

if(sentence=='h')

strcat(morsecode,"....");

strcat(morsecode," ");

if(sentence=='i')

strcat(morsecode,"..");

strcat(morsecode," ");

if(sentence=='j')

strcat(morsecode,".---");

strcat(morsecode," ");

if(sentence=='k')

strcat(morsecode,"-.-");

strcat(morsecode," ");

if(sentence=='l')

strcat(morsecode,".-..");

strcat(morsecode," ");

if(sentence=='m')

strcat(morsecode,"--");

strcat(morsecode," ");

if(sentence=='n')

strcat(morsecode,"-.");

strcat(morsecode," ");

if(sentence=='o')

strcat(morsecode,"---");

strcat(morsecode," ");

if(sentence=='p')

strcat(morsecode,".--.");

strcat(morsecode," ");

if(sentence=='q')

strcat(morsecode,"--.-");

strcat(morsecode," ");

if(sentence=='r')

strcat(morsecode,".-.");

strcat(morsecode," ");

if(sentence=='s')

strcat(morsecode,"...");

strcat(morsecode," ");

if(sentence=='t')

strcat(morsecode,"-");

strcat(morsecode," ");

if(sentence=='u');

strcat(morsecode,"..-");

strcat(morsecode," ");

if(sentence=='v')

strcat(morsecode,"...-");

strcat(morsecode," ");

if(sentence=='w')

strcat(morsecode,".--");

strcat(morsecode," ");

if(sentence=='x')

strcat(morsecode,"-..-");

strcat(morsecode," ");

if(sentence=='y')

strcat(morsecode,"-.--");

strcat(morsecode," ");

if(sentence=='z')

strcat(morsecode,"--..");

strcat(morsecode," ");

 

}

printf("%s",morsecode);

 

return;

}

 
you writting in c++?

wow been along time, dont think i have ever used it since being in college.

every thing is so much better now, they are starting to get into more natural languae programming.

i do most sql stuff now so im not much of a help here anymore, 10 years ago i would have been but not now, sorry

 
you writting in c++?
wow been along time, dont think i have ever used it since being in college.

every thing is so much better now, they are starting to get into more natural languae programming.

i do most sql stuff now so im not much of a help here anymore, 10 years ago i would have been but not now, sorry
Nope thats C.

 
I'm not sure, but I think what you should be doing is:
morsecode = strcat(morsecode,".-");
that's not it.... strcat works like this

strcat(receiving arr, donating); and it copies the donating into the receiving so there is no need for an operator.

 
That's why, I'm used to Visual Studio C++, or a different Linux Compiler I used to use on Fedora Core.
nG
Yea, I'm not too familiar with C either, they barely teach it in schools nowadays even though plenty of serious jobs require the skill.

 
that's not it.... strcat works like this
strcat(receiving arr, donating); and it copies the donating into the receiving so there is no need for an operator.
Yea your right, I was thinking that strcat returns a whole new string which is a concatenation of the other two.

What happens when you run that code?

 
Well here's a hint. Something I always do when debugging code that doesn't work.

Test small parts of the code...I'm not sure what output you're expecting but if you can get the core pieces of your code working a little bit at a time then it's easier to build up the program that way...

#include

#include

#define SMALL 80

#define BIG 320

void main()

{

char sentence[sMALL];

char morsecode[bIG]=" ";

int i,

num=0;

printf("Enter your info\n");

scanf("%s",sentence);

printf("%s\n",sentence);

num=strlen(sentence);

printf("%d\n", num);

for(i=0;i

{

if(sentence=='a')

strcat(morsecode,".-");

strcat(morsecode," ");

}

 

printf("%s",morsecode);

 

return;

}

 

 

--You may want to use the \n (newline) to help make the printouts look more formatted

 

./a.out

Enter your info

aaa

aaa

3

.- .- .-

 

 

 

./a.out

Enter your info

bbb

bbb

3

 

 

--I think you have to figure out the rest since it is your homework

 
One thing is those if statements are two lines long. If the expression is longer than one line you need to use {} to ecapsulate them. Hundredth is right about your loop too. You need to run that loop as long as the input string, but no longer.

 
Ok, thanks for the help with the {}. I didn't realize you needed them if there was more than one statement. I kept wondering why I was getting errors using else if statements, so I changed it to if's. Anyhow, I now have it working and converted it to use in a function. Only 5 more functions to go and this program will be done.

#include

#include

#define SMALL 80

#define BIG 320

void get_code(int *num, char sentence[], char morsecode[]);

void main()

{

char sentence[sMALL]=" ";

char morsecode[bIG]=" ";

int number=0;

printf("Enter your info\n");

scanf("%s",sentence);

printf("%s\n",sentence);

number=strlen(sentence);

printf("%d\n", number);

get_code(&number, sentence, morsecode);

return;

}

void get_code(int *num, char sentence[], char morsecode[])

{

int i=0;

for(i=0;i

{

if(sentence=='a')

{

strcat(morsecode,".-");

strcat(morsecode," ");

}

else if(sentence=='b')

{

strcat(morsecode,"-...");

strcat(morsecode," ");

}

else if(sentence=='c')

{

strcat(morsecode,"-.-.");

strcat(morsecode," ");

}

else if(sentence=='d')

{

strcat(morsecode,"-..");

strcat(morsecode," ");

}

else if(sentence=='e')

{

strcat(morsecode,".");

strcat(morsecode," ");

}

else if(sentence=='f')

{

strcat(morsecode,"..-.");

strcat(morsecode," ");

}

else if(sentence=='g')

{

strcat(morsecode,"--.");

strcat(morsecode," ");

}

else if(sentence=='h')

{

strcat(morsecode,"....");

strcat(morsecode," ");

}

else if(sentence=='i')

{

strcat(morsecode,"..");

strcat(morsecode," ");

}

else if(sentence=='j')

{

strcat(morsecode,".---");

strcat(morsecode," ");

}

else if(sentence=='k')

{

strcat(morsecode,"-.-");

strcat(morsecode," ");

}

else if(sentence=='l')

{

strcat(morsecode,".-..");

strcat(morsecode," ");

}

else if(sentence=='m')

{

strcat(morsecode,"--");

strcat(morsecode," ");

}

else if(sentence=='n')

{

strcat(morsecode,"-.");

strcat(morsecode," ");

}

else if(sentence=='o')

{

strcat(morsecode,"---");

strcat(morsecode," ");

}

else if(sentence=='p')

{

strcat(morsecode,".--.");

strcat(morsecode," ");

}

else if(sentence=='q')

{

strcat(morsecode,"--.-");

strcat(morsecode," ");

}

else if(sentence=='r')

{

strcat(morsecode,".-.");

strcat(morsecode," ");

}

else if(sentence=='s')

{

strcat(morsecode,"...");

strcat(morsecode," ");

}

else if(sentence=='t')

{

strcat(morsecode,"-");

strcat(morsecode," ");

}

else if(sentence=='u')

{

strcat(morsecode,"..-");

strcat(morsecode," ");

}

else if(sentence=='v')

{

strcat(morsecode,"...-");

strcat(morsecode," ");

}

else if(sentence=='w')

{

strcat(morsecode,".--");

strcat(morsecode," ");

}

else if(sentence=='x')

{

strcat(morsecode,"-..-");

strcat(morsecode," ");

}

else if(sentence=='y')

{

strcat(morsecode,"-.--");

strcat(morsecode," ");

}

else if(sentence=='z')

{

strcat(morsecode,"--..");

strcat(morsecode," ");

}

else if(sentence==' ')

strcat(morsecode," ");

}

 

printf("%s",morsecode);

 

return;

}

 
Activity
No one is currently typing a reply...

About this thread

ConcernedMember

10+ year member
******
Thread starter
ConcernedMember
Joined
Location
Around
Start date
Participants
Who Replied
Replies
12
Views
174
Last reply date
Last reply from
ConcernedMember
IMG_20260516_193114554_HDR.jpg

sherbanater

    May 16, 2026
  • 0
  • 0
IMG_20260516_192955471_HDR.jpg

sherbanater

    May 16, 2026
  • 0
  • 0

New threads

Top