Hello RRacer17,
Thinking about your program and how it does not work I offer this suggestion as a start:
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 111 112 113 114 115 116 117 118 119
|
#include <iostream>
#include <limits>
#include <string>
class Encode
{
std::string morse;
std::string encode{ "a b c d" }; // <--- Set up for testing.
//std::string letter; // <--- Not used.
public:
void UserInput();
void EncodeString();
};
void Encode::UserInput() // <--- Put these here. Normally I put them after "main" or in a separate file.
{
std::cout << "\n Enter the morse code (a b c): ";
std::getline(std::cin, encode);
}
void Encode::EncodeString()
{
for (size_t lc = 0; lc < encode.size(); lc++)
{
if (encode[lc] == ' ')
continue;
if (encode[lc] == 'a')
{
morse += ".-";
morse += ' ';
}
else if (encode[lc] == 'b')
{
morse += "-.-.";
morse += ' ';
}
else if (encode[lc] == 'c')
{
morse += "-..";
morse += ' ';
}
else if (encode[lc] == 'd')
{
morse += ".";
morse += ' ';
}
}
morse.pop_back(); // <--- Removes the space at the end of the string.
std::cout << "\n " << morse << std::endl; // <--- Uses for testing. May not be something that you want to keep.
return; // <--- Not needed in the function, but makes a good break point when testing. Remove when testing is finished.
}
int main()
{
int op{ 1 }; // <--- Change to empty {}s when done testing.
Encode encode;
//codec decode; // <--- Not used yet.
//std::cout << "\n Code Char to Morse enter - 1\n Decode MORSE code enter - 2\n Exit - 3\n Enter Choice: ";
// <--- You may like this menu better. Use whichever you like.
std::cout
<< "\n 1. Code Char to Morse"
<< "\n 2. Decode MORSE code"
<< "\n 3. Exit"
<< "\n Enter Choice: ";
//std::cin >> op;
std::cout << std::endl; // <--- Used for testing. Remove when finished testing.
while (!std::cin || (op < 1 || op > 3)) // <--- Check for a non numeric entry and validates input and.
{
if (!std::cin)
{
std::cout << "\n Invalid entry! Must be a number.\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
else if (op < 1 || op > 3)
{
std::cout << "\n Entry out of range! Numbers are 1, 2 or 3\n";
}
std::cout << "\n Enter Choice: ";
std::cin >> op;
}
// <--- Uncomment for normal use.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
switch (op) // <--- Or you can use your if else statements.
{
case 1:
//encode.UserInput();
encode.EncodeString();
break;
//case 2:
// break;
case 3:
break;
default:
break;
}
// <--- An alternative to using "system("pause")". Because you should not use "system" anything.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed. Just comment it out.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue :";
std::cin.get();
return 0; // <--- Not required, but a good place for a break point.
}
|
This is a suggestion. I do not know what you can and can not use. I am also thing of a version that used an "enum" and an array. And a version that uses a "std::map". You will have to let me know if you have learned about either the "enum" or "std::map".
The not quite oblivious part is whiting this code in small parts. First I wrote the menu. Then i added the class "Encode" and worked on the class function "UserInput()". When that was working I adjusted the program to bypass the entering for "op" by initializing the variable to 1 when it was defined. Then in the case statement I could bypass calling "UserInput()" by initializing the string "encode" to "a b c d" or any variation of those four letters that you want. As
dhayden suggested.
dhayden's idea of using the arrays is also a possibility.
In the function "EncodeString()" I just used the if/else if statements because they were easy to copy and change for now and it gives you a different approach than what you started with. You could just as easily use a "switch/case" here.
I feel that there is a better way than using two lines of "+=". At this time I seem to have a brain freeze on how to do this. Open to any suggestions or direction.
Initializing the string "encode" in the class worked for me, but it would be more proper to do this in the ctor of the class:
1 2 3 4
|
Encode::Encode()
{
encode = "a b c d";
}
|
For the class "Decode" it could fundamentally start as a copy of "Encode", but would need some additional code to revers the process. Using a "stringstream" here would help along with "std::getline, letter, ' ')" where "letter" is defined as a "std::string". A thought for now.
@dhayden and dutch,
I already understand most of what you have said, so maybe you could tell me where "
iostream.h" is located because I have not found one so far. Since that was my main point I would like to know where it is or if it even existed in the first place.
Hope that helps,
Andy