morse code program c++ please help!

This is the text file:
.- ...- ..--- .-- .... .. . -.-. -..-  ....- .....
-... .... ...-- 


The text file outputs:
1
2
AV2WHIECX45
BH3


If there are two spaces instead of one space then that means new word, which is after the X, so it supposed to output:
1
2
AV2WHIECX 45
BH3


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
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main (){
	ifstream file ("input.txt"); 
	string line;
	while(getline(file, line)){
		stringstream ss(line);
		while(getline(ss, line, ' ')){
			if (line == ".-") {cout <<"A";}
			if (line == "-...") {cout <<"B";}
			if (line == "-.-.") {cout <<"C";}
			if (line == "-..") {cout <<"D";}	
			if (line == ".") {cout <<"E";}
			if (line == "..-.") {cout <<"F";}
			if (line == "--.") {cout <<"G";}	
			if (line == "....") {cout <<"H";}
			if (line == "..") {cout <<"I";}
			if (line == ".---") {cout <<"J";}
			if (line == "-.-") {cout <<"K";}
			if (line == ".-..") {cout <<"L";}
			if (line == "--") {cout <<"M";}
			if (line == "-.") {cout <<"N";}
			if (line == "---") {cout <<"O";}
			if (line == ".--.") {cout <<"P";}
			if (line == "--.-") {cout <<"Q";}
			if (line == ".-.") {cout <<"R";}
			if (line == "...") {cout <<"S";}
			if (line == "-") {cout <<"T";}
			if (line == "..-") {cout <<"U";}
			if (line == "...-") {cout <<"V";}
			if (line == ".--") {cout <<"W";}
			if (line == "-..-") {cout <<"X";}
			if (line == "-.--") {cout <<"Y";}
			if (line == "--..") {cout <<"Z";}
			if (line == ".----") {cout <<"1";}
			if (line == "..---") {cout <<"2";}
			if (line == "...--") {cout <<"3";}
			if (line == "....-") {cout <<"4";}
			if (line == ".....") {cout <<"5";}
			if (line == "-....") {cout <<"6";}
			if (line == "--...") {cout <<"7";}
			if (line == "---..") {cout <<"8";}
			if (line == "----.") {cout <<"9";}
			if (line == "-----") {cout <<"0";}
		}
			cout << '\n';			
	}
	return 0;
}
Last edited on
I think your input file is missing some key information :P [EDIT] the OP was modified and the full input is there now [/EDIT]

Anyways, you are going to want to read in the spaces and then if there are 2 in a row output a space.

You could do that by doing something like if(ss.peek() == ' ') {cout << ' ';}

Though I personally would be using if / else and not if / if / if...

PS put the peek statement after line 47 but before line 48.


Here is a slightly modified version of your code (I used cpp.sh so I changed it from input file to string/stream)

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
54
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main (){
	string input = ".... . .-.. .-.. ---  .-- --- .-. .-.. -..";
	stringstream inputStream(input);
	string line;
	while(getline(inputStream, line)){
		stringstream ss(line);
		while(getline(ss, line, ' ')){
			if (line == ".-") {cout <<"A";}
			else if (line == "-...") {cout <<"B";}
			else if (line == "-.-.") {cout <<"C";}
			else if (line == "-..") {cout <<"D";}	
			else if (line == ".") {cout <<"E";}
			else if (line == "..-.") {cout <<"F";}
			else if (line == "--.") {cout <<"G";}	
			else if (line == "....") {cout <<"H";}
			else if (line == "..") {cout <<"I";}
			else if (line == ".---") {cout <<"J";}
			else if (line == "-.-") {cout <<"K";}
			else if (line == ".-..") {cout <<"L";}
			else if (line == "--") {cout <<"M";}
			else if (line == "-.") {cout <<"N";}
			else if (line == "---") {cout <<"O";}
			else if (line == ".--.") {cout <<"P";}
			else if (line == "--.-") {cout <<"Q";}
			else if (line == ".-.") {cout <<"R";}
			else if (line == "...") {cout <<"S";}
			else if (line == "-") {cout <<"T";}
			else if (line == "..-") {cout <<"U";}
			else if (line == "...-") {cout <<"V";}
			else if (line == ".--") {cout <<"W";}
			else if (line == "-..-") {cout <<"X";}
			else if (line == "-.--") {cout <<"Y";}
			else if (line == "--..") {cout <<"Z";}
			else if (line == ".----") {cout <<"1";}
			else if (line == "..---") {cout <<"2";}
			else if (line == "...--") {cout <<"3";}
			else if (line == "....-") {cout <<"4";}
			else if (line == ".....") {cout <<"5";}
			else if (line == "-....") {cout <<"6";}
			else if (line == "--...") {cout <<"7";}
			else if (line == "---..") {cout <<"8";}
			else if (line == "----.") {cout <<"9";}
			else if (line == "-----") {cout <<"0";}
			if (ss.peek() == ' '){cout << ' ';}
		}
			cout << '\n';			
	}
	return 0;
}
HELLO WORLD


[EDIT2]*added output statement in the code[/EDIT2]
Last edited on
Thanks!
Topic archived. No new replies allowed.