Text to morse code with Beep() function C++

Nov 12, 2016 at 7:51pm
Hello guys! I have a problem with my homework. I need to make program without touch main.cpp file. I should add just morse.h and morse.cpp. Could you help me? I really dont know how to even start with this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include "morse.h"

using namespace std;

int main()
{

	morse m;
	m.setFrequency(1000);
	m.setPause(100);
	m.setSpacePause(1000);
	m.setCharPause(200);
	m.setDotTime(200);
	m.setDashTime(400);
	m << "Ala ma kota";
	long i = 0xA5A5A5A;
	double d = 1.23456789;
	//m << i<<pause << d;
	return 0;
}
Last edited on Nov 12, 2016 at 8:54pm
Nov 12, 2016 at 9:02pm
The function calls in main give a hint to the data members in a morse object and the names of the member functions to be created to set values of those data members. I'd probably start there.

m << "Ala ma kota"; seems like it might use the Beep function to output beeps matching the morse equivalents letters in the string there?
Nov 12, 2016 at 9:36pm
Yup m << "Ala ma kota"; should be Beeped and also long i and double d. I've set all of these setFrequency etc. and they assign correctly all of these values, but i 100% sure that i should overload operator<<. Theres one more thing, i just can't solve problem with 2 beeps after compiling without m << "Ala...; morse.h
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
29
30
31
32
33
#pragma once
#ifndef _MORSE_CPP_
#define _MORSE_CPP_
#include <Windows.h>
#include <iostream>

class morse
{
	int Frequency;
	int Pause;
	int SpacePause;
	int CharPause;
	int DotTime;
	int DashTime;
	
	

public:

	morse();
	~morse();

	void setFrequency(int f);
	void setPause(int p);
	void setSpacePause(int sp);
	void setCharPause(int cp);
	void setDotTime(int dt);
	void setDashTime(int dat);

	friend std::ostream& operator<<(std::ostream& m,const morse& ko);
};

#endif 



and theres morse.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <windows.h>
#include "morse.h"

morse::morse()
{
}

morse::~morse()
{
}

void morse::setFrequency(int f)
{
	Frequency = f;
}

void morse::setPause(int p)
{
	Pause = p;
}

void morse::setSpacePause(int sp)
{
	SpacePause = sp;	
}

void morse::setCharPause(int cp)
{
	Beep(0, cp);	
}

void morse::setDotTime(int)
{
	Beep(Frequency, DotTime);
	Beep(0, Pause);	
}

void morse::setDashTime(int)
{
	Beep(Frequency, DashTime);
	Beep(0, Pause);	
}

std::ostream & operator<<(std::ostream & m, const morse & ko)
{
	
}
Nov 16, 2016 at 5:46pm
UP! Please guys help
Nov 16, 2016 at 5:55pm
Why are you calling Beep in setCharPause, setDotTime and setDashTime? These functions should only set the internal members of your class - they shouldn't produce beeps.
Nov 16, 2016 at 8:01pm
That's how it looks now
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
29
30
31
32
33
34
35
36
37
38
39
40
#include <windows.h>
#include "morse.h"

morse::morse()
{
}

morse::~morse()
{
}

void morse::setFrequency(int f)
{
	Frequency = f;
}

void morse::setPause(int p)
{
	Pause = p;
}

void morse::setSpacePause(int sp)
{
	SpacePause = sp;	
}

void morse::setCharPause(int cp)
{
	Beep(0, cp);	
}

void morse::setDotTime(int dt)
{
	DotTime = dt;
}

void morse::setDashTime(int dsht)
{
	DotTime = dsht;
}


But i have a bigger problem with overloading <<. I need to overload << 3 times, 1 for pause, 2 for long and 3 for double but kill me i don't know how to do it.
Nov 16, 2016 at 10:56pm
Your setCharPause function still invokes Beep. All it's supposed to do is set your class' CharPause member.

I'm having a hard time understanding what the long and double represent.

The hexadecimal value "0xA5A5A5A" is "173693530" in decimal - what does 173693530 (and 1.23456789 for the double) mean? Are these beep durations or frequencies? Something else entirely?
Nov 17, 2016 at 12:16am
I just fixed setCharPasue to
1
2
3
4
void morse::setCharPause(int cp)
{
	CharPause= cp;
}

I must also Beep that two values (double and long). I am pretty sure that i need to make them both string. Durations are set in the setDotTime and setDashTime with setFrequency.
Nov 19, 2016 at 8:13am
up!
Nov 19, 2016 at 5:54pm
must also Beep that two values (double and long). I am pretty sure that i need to make them both string.


You still haven't explained how these values should be interpreted.

For example, should "0xA5A5A5A" be turned into a string, and then each character should be beeped individually (first '0', then 'x', then 'A' and so on...)? Should 0xA5A5A5A be interpreted as a decimal integer (173693530), then as a string and each character printed individually (first '1', then '7', then '3' etc)?
Nov 19, 2016 at 7:50pm
Yup it should be done just like You said. 0x... to int and after that individually beep'ed.
Nov 23, 2016 at 7:50pm
Take a look here. I've created this skeleton Morse class for you. Some important things to note:
1.) You don't need to overload std::ostream's extraction operator to parse strings, just roll with your own which returns a reference to a Morse object (itself).

2.) This doesn't support pauses.

3.) You'll still have to convert numbers to strings. You can do that with "std::stringstream"objects found int the <sstream> header - or you can use the function "std::to_string" which is found in the <string> header.
Additionally, the codes array only has 26 elements - one for each letter in the alphabet. You'll have to probably create another array to contain the codes for numbers.

4.) This also doesn't support upper-case characters. The easiest way to handle them is to simply convert everything you parse to lower-case.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "morse.hpp"
#include <iostream>

int main() {

	Morse morse;

	morse << "hello" << "world";

	std::cin.ignore();
	return 0;
}


morse.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#pragma once
#include <string>

class Morse {
public :
	Morse();

	void beep(bool);

	Morse& operator<<(std::string);

private :

	int time_dot;
	int time_dash;

	const static int number_of_codes = 26;
	const static std::string codes[number_of_codes];

};


morse.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "morse.hpp"
#include <windows.h>

const std::string Morse::codes[number_of_codes] = {
	".-",//a
	"-...",//b
	"-.-.",//c
	"-..",//and so on...
	".",
	"..-.",
	"--.",
	"....",
	"..",
	".---",
	"-.-",
	".-..",
	"--",
	"-.",
	"---",
	".--.",
	"--.-",
	".-.",
	"...",
	"-",
	"..-",
	"...-",
	".--",
	"-..-",
	"-.--",
	"--.."//z
};

Morse::Morse() : time_dot(200), time_dash(400) {

}

void Morse::beep(bool is_dot) {
	const static int frequency = 440;
	const int duration = (is_dot ? time_dot : time_dash);

	::Beep(frequency, duration);
}

Morse& Morse::operator<<(std::string string) {
	for (const auto& it : string) {
		const int index = char(it - 'a');
		std::string code = codes[index];
		for (const auto& it : code) {
			this->beep((it == '.'));
		}
	}
	return *this;
}
Last edited on Nov 23, 2016 at 7:54pm
Topic archived. No new replies allowed.