You are paying for this "education", so you'd better make sure you're getting value for money.
Who's paying?
- your parents, indirectly through their taxes.
- your parents, directly, because you're at a private school where you pay for tuition.
- you, indirectly, through your upcoming failure to get a decent job because these "skills" are obsolete.
> gets(morse);
If you used this in my code, it would be instant summary dismissal!
It's so irredeemably awful that it was deprecated in C99 and removed in C11.
http://c-faq.com/stdio/getsvsfgets.html
> These are the list of problems I am facing-
> 1. Will the forloop work?
> 2. And the "<<decode" gives junk values as output.
You're committing the first basic sin of programming, trying to do too much at once.
You NEED to make sure step 1 works before starting step 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
using namespace std;
void text_to_morse() {
}
void morse_to_text() {
cout<<"Enter the morse code \nEach letter should be separated by a space\n";
char line[100];
cin.getline(line,sizeof(line));
char word[100];
for ( int i = 0 ; line[i] != '\0' ; i++ ) {
if ( line[i] == ' ' ) {
// The double == makes sure you will see any errant leading or trailing spaces in word.
cout << "Found word =" << word << "=" << endl;
}
}
}
int main ( ) {
char line[100];
cin.getline(line,sizeof(line));
if ( line[0] == 'd' ) {
morse_to_text();
} else if ( line[0] == 'e' ) {
text_to_morse();
}
}
|
Your first step is add the MINIMUM amount of code to make this work.
1 2 3 4 5 6
|
Enter the morse code
Each letter should be separated by a space
.- -... -.-.
Found word =.-=
Found word =-...=
Found word =-.-.=
|
You must make your morse word extraction work reliably BEFORE trying to decode those words back to the associated letters.