Member Avatar for mattiapdo

Hi
I'm new in this forum. I'm an italian student (not an expert of c++) , so I perharps will not understand very well English (escuse me);

I wanted to create a program in C++ that let me to receive the translation in Morse Code of a string...
Do I do well operating with vectors, or I need something else?

I'm posting the text of the code.
I use Win XP and DEVc++ as compilator... it runs but not as i wonder!
If someone should answer me, I will be grate! :)

#include <iostream>
#include <windows.h> // WinApi header
#include <stdlib.h>
using namespace std;

int main()
{
int N,i;
N=5;
char v[N],type;
cout<<"INSERIRE MASSIMO 5 CARATTERI"<<endl;
for (i=0;i<N;i++)
 cin>>v[i];

for (i=0;i<N;i++)
{
v[i]=type;
switch (type)
      {
       case 'a':
            Beep(900,100);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            cin.get();
       break;
       case 'b':
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'c':
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'd':
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'e':
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'f':
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'g':
            Beep(900,250);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'h':
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'i':
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'l':
            Beep(900,100);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'm':
            Beep(900,250);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            cin.get();
       break;
       case 'n':
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'o':
            Beep(900,250);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            cin.get();
       break;
       case 'p':
            Beep(900,100);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 'q':
            Beep(900,250);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            cin.get();
       break;
       case 'r':
            Beep(900,100);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 's':
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
       case 't':
            Beep(900,250);
            _sleep(100);
            cin.get();
       break;
       case 'u':
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            cin.get();
       break;
       case 'v':
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            cin.get();
       break;
       case 'z':
            Beep(900,250);
            _sleep(100);
            Beep(900,250);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            Beep(900,100);
            _sleep(100);
            cin.get();
       break;
      }}
return 0;
}

One issue I see right away is line 17, where you're setting each index of v to type before switching on type and since type itself is never actually initialized, I can see why it wouldn't work properly.

I assume that you want to do type = v on that line instead of the other way around. That way, type will actually contain something useful.

one recommendation i would make.. you have several lines of beep(900,100). i would advise creating two int's that will hold these values. for example:

//instead of 
beep(900,200);

//try
int frequency = 900;
int duration  = 200;

beep(frequency, duration);

that way after you got your program up and running.. and you want to tweak its performance, all you have to do is change the value of two variables instead of editing hundreds of lines of code.

one recommendation i would make.. you have several lines of beep(900,100). i would advise creating two int's that will hold these values. for example:

//instead of 
beep(900,200);

//try
int frequency = 900;
int duration  = 200;

beep(frequency, duration);

that way after you got your program up and running.. and you want to tweak its performance, all you have to do is change the value of two variables instead of editing hundreds of lines of code.

Even better, use 3 ints: freq, short_beep & long_beep. Morse code only has 2 lengths and a frequency of the beep, so this covers you for the whole shebang.

Member Avatar for mattiapdo

One issue I see right away is line 17, where you're setting each index of v to type before switching on type and since type itself is never actually initialized, I can see why it wouldn't work properly.

I assume that you want to do type = v on that line instead of the other way around. That way, type will actually contain something useful.

yes! well done!! thanks :)

Member Avatar for mattiapdo

Even better, use 3 ints: freq, short_beep & long_beep. Morse code only has 2 lengths and a frequency of the beep, so this covers you for the whole shebang.

right! i'll do that! :)

Member Avatar for mattiapdo

Thank you to everybody...
however, the program doesn't run as i woul:

operating with vector, I should see in the execution file, for example:

INSERIRE MASSIMO 3 CARATTERI
a
b
c

... and hear the traslation... but it's not so! I can hear just one letter! Can you help me?

#include <iostream>
#include <windows.h> // WinApi header
#include <stdlib.h>
using namespace std;

int main()
{
int N,i, l=250, s=100, t=100, f=900;
N=3;
char v[N],type;
cout<<"INSERIRE MASSIMO 3 CARATTERI"<<endl;
for (i=0;i<N;i++)
 cin>>v[i];

for (i=0;i<N;i++)
{
type=v[i];
switch (type)
      {
       case 'a':
            Beep(f,s);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            cin.get();
       break;
       case 'b':
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'c':
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'd':
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'e':
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'f':
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'g':
            Beep(f,l);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'h':
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'i':
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'l':
            Beep(f,s);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'm':
            Beep(f,l);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            cin.get();
       break;
       case 'n':
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'o':
            Beep(f,l);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            cin.get();
       break;
       case 'p':
            Beep(f,s);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 'q':
            Beep(f,l);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            cin.get();
       break;
       case 'r':
            Beep(f,s);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            cin.get();
       break;
       case 's':
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
       case 't':
            Beep(f,l);
            _sleep(t);
            cin.get();
       break;
       case 'u':
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            cin.get();
       break;
       case 'v':
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            cin.get();
       break;
       case 'z':
            Beep(f,l);
            _sleep(t);
            Beep(f,l);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            Beep(f,s);
            _sleep(t);
            cin.get();
       break;
      }}
return 0;
}

I'm wondering if your app just pauses waiting for input once it continues to move on. Comment out your cin.get lines in the switch and just put one in after you accept your input. See how that works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.