braille to english, english to braille

Hello. Im trying to write a code that converts braille into English on c++. I have a file that will read in a given statement, and then output it into english in the console. Also, i want to be able to take that english statement and read it out to a file in braille, as well as the on the consoleThis is the code I have so far. The confusion I am having right now is how to assign each letter of the alphabet to the right braille number. Here is the code that I have right 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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream fileIN("Braille.txt");
    ofstream fileOUT("message.txt");
    
    if (fileIN.fail())
    {
        cerr << "File Braille.txt could not be opened." << endl;
        return -1;
    }
    
    if (fileOUT.fail())
    {
        cerr << "File message.txt could not be opened." << endl;
        return -1;
    }
    
    double braileNum=0;
    fileIN >> braileNum;
    fout << braileNum << " " << endl; 
    
    return 0;
}


Last edited on
Hello jimmyjohn2,

Bear with me this is just a copy and paste.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Its the second link that is worth reading.

You missed the "/" in the closing tag.

While I go over your code maybe you could post the input file of as good sample of if, so that everyone is working with the same information.

What you have may work, but there is a better way to do it.

Andy
this is the input file in braille. the name of this file is "Braile.txt" at the beginning.

010100 011110 999999 010100 011100 999999 100000 999999 110100 100000 111010 999999 110100 100000 111010 999999 110000 100010 011110 011110 100010 111010 999999 011110 110010 010100 101110 110110 999999 011110 110010 100000 011110 999999 010100 999999 100110 101010 999999 011110 110010 100000 101110 999999 010100 999999 110010 100000 111001 100010 999999 100010 111001 100010 111010 999999 100110 101010 101110 100010 999999 010100 011110 999999 010100 011100 999999 100000 999999 110100 100000 111010 999999 110100 100000 111010 999999 110000 100010 011110 011110 100010 111010 999999 111010 100010 011100 011110 999999 011110 110010 100000 011110 999999 010100 999999 110110 101010 999999 011110 101010 999999 011110 110010 100000 101110 999999 010100 999999 110010 100000 111001 100010 999999 100010 111001 100010 111010 999999 101000 101110 101010 010111 101110
Hello jimmyjohn2,

Thank you.

I offer this as one possible way of doing your program.
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
#include <iostream>

#include <fstream>

//using namespace std;  // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

int main()
{
	std::ifstream fileIN("HW3_Braille.txt");
	std::ofstream fileOUT("message.txt");

	if (!fileIN) // <--- This is all you need.
	{
		std::cerr << "File \"HW3_Braille.txt\" could not be opened." << std::endl;  // <--- Changed.

		return 1;
		}

	if (!fileOUT)
	{
		std::cerr << "File \"message.txt\" could not be opened." << std::endl;  // <--- Changed.

		return 2;
	}

	double braileNum = 0;

	fileIN >> braileNum;

	fileOUT << braileNum << std::endl; // <--- Changed file stream name and removed the space for now.

	return 0;  // <--- Not required, but makes a good break point.
}

The comments should explain what I did. If not let me know.

In your if statements since you are returning an "int" (-1) is OK, but (0) zero is considered a normal return with no problems and any number greater than (0) zero is considered a problem. Even the macro "EXIT_SUCCESS" is defined as (0) zero and "EXIT_FAILURE" is defined as (1).

Sometimes it is better to use what most people use and be consistant than to do your own thing.

Next notice the blank lines. They make the code easier to read and that is what is important. It is mostly for your benefit.

Now for lines 29 and 31 this only reads one number from the input file and then prints it out. This leaves the rest of the input untouched.

What you may want to do is:
1
2
3
4
5
6
	while (fileIN >> braileNum)
	{
		// <--- Other processing here.

		fileOUT << braileNum << std::endl; // <--- Changed file stream name and removed the space for now.
	}

This will at least read the whole file.

A little thought to the program I realized that after reading the file you will need to compare it to something to change it into a letter. Have you given any thought to how you plan to do this? A first thought is that a "std::map" could be useful unless you have not learned this yet. It would help to know what you do know and can work with.

Since this appears to be a homework or lab assignment it would help if you could post the directions/instructions that you were given. This would give others an idea of what you need to do.

Andy
These are the directions

1. Open the file named ‘HW3_Braille.txt’ for reading. a. Convert the Braille writing in that file into a message using the standard English alphabet. (Note that Braille is not a separate language, but a way of writing languages – the message expressed in Braille would still be in “English” and hence the term “translate” is not used in this context). b. Display the converted, standard English message on the console. c. Write the same converted message to a file named ‘message.txt’. After writing the file, close the file. The contents of this file should be in standard English. Now open the file ‘message.txt’ for reading.

2. a. Read the standard English text from the file and convert each letter in the text into your binary Braille “code.” b. Using the same convention as the initial file (spaces separating letters, 999999 separating words), write the binary Braille, letter by letter, to a new file called ‘message2.txt’ and also write the same to the console. c. When the last letter in the text has been converted and written in binary Braille, close the output file.

3. To ensure your program ran correctly, open ‘message2.txt’ and ‘HW3_Braille.txt’ and, by eye, check that the contents are exactly the same.
both unix and windows have a file compare utility built in.

Hello jimmyjohn2,

Thank you.

Now that I have a better understanding of what the program should do and can read the input file. What I do not know is given:

010100 011110 999999 010100 011100 999999


I understand the the "999999" is a space between words, but what letters go with the other numbers? and what do you have in mind to store these numbers and letters in to work with?

Working with the program I also found double braileNum = 0; is more than you need. An "int" will work just as good int braileNum{}; and reduce the storage space that you are not using with the "double".

Andy

Edit: typos.
Last edited on
This is my code right now . I will also include a key as to what each binary code and the letter it corresponds too. Cannot finish the second step as it will not print out to my message2.txt file.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 #include <iostream>
#include <fstream>

using namespace std;

int main()
{
    // open files
    ifstream fileIN("HW3_Braille.txt");
    ifstream converter("binary_Braille_conv.txt");
    ofstream fileOUT("message.txt");
    // check if files open
    if (fileIN.fail())
    {
        cerr << "File HW3_Braille.txt could not be opened." << endl;
        return -1;
    }
    if (converter.fail())
    {
        cerr << "File binary_Braille_conv.txt could not be opened." << endl;
        return -1;
       }
    if (fileOUT.fail())
    {
        cerr << "File message.txt could not be opened." << endl;
        return -1;
    }
    // read in the HW3_Braille.txt
    int brailleNum=0;
    fileIN >> brailleNum;
    char letter;
    int binary=0;
    char mainLetter;
    // use while loop for step one
    while(!fileIN.eof())
    {
          for (int i=0; i<26; i++)
          {
              converter >> binary >> letter;
              if (brailleNum==binary)
              {
                  mainLetter = letter;
              }
              else if(brailleNum==999999)
              {
                  mainLetter = ' ';
              }
              
          }
    // print out english letters into console and message.txt
    fileIN >> brailleNum;
    cout << mainLetter;
    fileOUT << mainLetter;
    converter.close();
    converter.open("binary_Braille_conv.txt");
    }
    // close files
    fileIN.close();
    fileOUT.close();
    converter.close();
    
    // open message.txt file and out to message2.txt
    ifstream messageIN("message.txt");
    ofstream messageOUT("message2.txt");
    //see if files will open properly
    if (messageIN.fail())
    {
        cerr << "File message1.txt could not be opened." << endl;
        return -1;
    }
   
    //initialize vairables used in step 2
    char letter2;
    messageIN.get(letter2);
    int binary2=0;
    
    // use while loop to find characters in message to convert to binary
    while(!messageIN.eof())
    {
        if(letter==' ')
        {
            binary2 = 999999;
        }
        ifstream converter("binary_Braille_conv.txt");
        if(converter.fail())
        {
            cerr << "File binary_Braille_conv.txt" << endl;
            return -1;
        }
        converter >> binary2 >> letter;
        
        while(converter.eof())
        {
            if(letter2==letter)
            {
                binary2 = binary;
            }
        converter >> binary2 >> letter;
        messageOUT << binary2;
        }
        messageIN >> letter2;
    }
    messageIN.close();
    messageOUT.close();
    converter.close();
        
    
    
    return 0;
}


100000 A
110000 B
100100 C
100110 D
100010 E
110100 F
110110 G
110010 H
010100 I
010110 J
101000 K
111000 L
101100 M
101110 N
101010 O
111100 P
111110 Q
111010 R
011100 S
011110 T
101001 U
111001 V
010111 W
101101 X
101111 Y
101011 Z
I should use a map (or two).

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cctype>
#include <map>
using namespace std;

map<char,string> Braille;
map<string,char> Alphabet;

//======================================================================

string toBraille( const string &text )
{
   string result;
   for ( char c : text ) if ( Braille.find( c ) != Braille.end() ) result += Braille[c] + ' ';
   return result;
}

//======================================================================

string toAlphabet ( const string &text )
{
   string result, br;
   for ( stringstream ss( text ); ss >> br; ) if ( Alphabet.find( br ) != Alphabet.end() ) result += Alphabet[br];
   return result;
}

//======================================================================

int main()
{
// ifstream fileIN( "BrailleDef.txt" );
   istringstream fileIN( "100000 A\n"
                         "110000 B\n"
                         "100100 C\n"
                         "100110 D\n"
                         "100010 E\n"
                         "110100 F\n"
                         "110110 G\n"
                         "110010 H\n"
                         "010100 I\n"
                         "010110 J\n"
                         "101000 K\n"
                         "111000 L\n"
                         "101100 M\n"
                         "101110 N\n"
                         "101010 O\n"
                         "111100 P\n"
                         "111110 Q\n"
                         "111010 R\n"
                         "011100 S\n"
                         "011110 T\n"
                         "101001 U\n"
                         "111001 V\n"
                         "010111 W\n"
                         "101101 X\n"
                         "101111 Y\n"
                         "101011 Z\n" );

   string br;
   char c;
   while( fileIN >> br >> c )
   {
      Braille[tolower(c)] = Braille[c] = br;
      Alphabet[br] = c;
   }
   Braille[' '] = "999999";   Alphabet["999999"] = ' ';

   string message = "This is fun";                cout << message    << '\n';
   string asBraille = toBraille( message );       cout << asBraille  << '\n';
   string asAlphabet = toAlphabet( asBraille );   cout << asAlphabet << '\n';
}

This is fun
011110 110010 010100 011100 999999 010100 011100 999999 110100 101001 101110 
THIS IS FUN
Last edited on
Hello jimmyjohn2,

I beg to differ with lastchance this has not been fun.

First question I have is could you find a more difficult way of writing the program?

Some parts of the program are so poorly written that I had a hard time trying to make them work and eventually had to change it.

In your last code posting lines 1 - 29 are OK With the exception of the return value in the if statements. You should use at least (1) or the numbers (1 , 2, 3) if your streams' do not open. The different numbers can help to track down the problem. I would also write the line cerr << "File \"HW3_Braille.txt\" could not be opened." << endl; adding the quotes helps set off the file name to make it easier to understand. The quotes are optional, I just think it looks better.

I moved line 30 to just before the while loop. Not really necessary, but again I think it make the code easier to follow.

You initialized your "int", but left the "char"s uninitialized. It is a good idea to initialize your variables when defined if for no other reason than to know they do not contain a garbage value.

Although the while loop is not the best way to read the file you have at least managed to do it correctly.

This is what I came up with for what I think of as the first section:
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
fileIN >> brailleNum;

// use while loop for step one
while (!fileIN.eof())
{
	for (int i = 0; i < 26; i++)
	{
		converter >> binary >> letter;

		if (brailleNum == binary)
		{
			mainLetter = letter;

			break;
		}

		if (brailleNum == 999999)
		{
			mainLetter = ' ';

			break;
		}

	}

	// print out english letters into console and message.txt

	cout << mainLetter;

	fileOUT << mainLetter;

	converter.seekg(0); // <--- Sets file pointer to the beginning.

	fileIN >> brailleNum;

	//converter.close(); // <--- Replaced with "seekg()".

	//converter.open("binary_Braille_conv.txt"); // <--- Replaced with "seekg()".
}

Since the for loop never sets the "eof" bit there is no need to close and open the file. Line 32 moves the file pointer back to the beginning so that you can read the file again. It is less work to move the file pointer than to close and open the file for no reason.

After the while loop where you close the file streams DO NOT close "converter" you can still use it in the next section.

In the next section I made this change:
1
2
3
//initialize vairables used in step 2
char letter2; // <--- Defines, but does not initialize. [char letter2{};] The {}s initialize the variable.
std::string binary2; // <--- Needs to be a string to preserve any leading zeros. 

In this part defining "binary2" as an "int" does not work. The "int" will store the number, but with out the leading (0) zeros. The string will read it as is keeping any leading zeros. You could use an "int", but that would require the header file "iomanip" and the use of "std::setfill()" and "std::setw()" to make it work. Since you did not include the header file "iomanip" you may not have learned about this yet.

When changing from text to braille I had to change the inner while loop to a for loop for it to work. The overall concept should be similar to what was done in the first section.

The if statements I had to change compleetly:
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
        converter.seekg(0); // <--- Sets file pointer to the beginning. Since you do not close the stream.

        converter >> binary2 >> letter;

	for (int i = 0; i < 26; i++) // <--- Better used here. Does not set "eof" bit.
	{
		if (letter2 == ' ')
		{
			//binary2 = "999999";
			messageOUT << "999999 ";

			break; // <--- Once a match is found there is no need to keep checking.
		}

		if (letter2 == letter)
		{
			//binary2 = binary;
			messageOUT << binary2 << ' ';

			break;
		}

		//messageOUT << binary2 << ' ';

		converter >> binary2 >> letter;
	}

Although I do not find anything wrong with line 19 for some reason, that I do not understand, it did not work. It always output the wrong number.

In your last code you have the lines:
1
2
converter >> binary2 >> letter;
messageOUT << binary2;

These lines are reversed. You are reading a new value for "binary2" before you output what you processed from the previous read. This is outputting the wrong information.

I know the code that lastchance has offered may be ahead of what you know, but it is worth trying to understand.

Andy
Topic archived. No new replies allowed.