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