[C++] using std::map[C++] using std::map
⚙️
ABCD Message
Week 21, 2026
#include "allheaders.hpp"
#include "main.hpp"
using namespace std;
//---------------------------------------------------------[VARIABLES]
vectorbcdStr;
string bcdString;
fstream infile;
string bcdToText(std::string bcdCode);
//-----------------------------------------------------[PROGRAM LOGIC]
int main()
{
infile.open("BCDdata.txt", ios::in | ios::out);
if(!infile.is_open())
{
cout << "An error has occured.\n";
}
while(getline(infile, bcdString))
{
bcdStr.push_back(bcdString);
}
int size = bcdStr.size();
for(int i = 0; i < size; i++)
{
string s = bcdToText(bcdStr[i]);
}
return 0;
}
//------------------------------------------------[FUNCTION DEFINITIONS]
string bcdToText(std::string bcdCode)
{
map BCDMap
=
{
{"0000", "0"},
{"0001", "1"},
{"0010", "2"},
{"0011", "3"},
{"0100", "4"},
{"0101", "5"},
{"0110", "6"},
{"0111", "7"},
{"1000", "8"},
{"1001", "9"},
{"1010", "A"},
{"1011", "B"},
{"1100", "C"},
{"1101", "D"},
{"1110", "E"},
{"1111", "."}
};
size_t pos = 0;
while ((pos = bcdCode.find(' '))
!= std::string::npos)
{
std::string token = bcdCode.substr(0, pos);
if (BCDMap.find(token) != BCDMap.end())
{
std::cout << BCDMap[token];
}
else
{
std::cout << " ";
}
bcdCode.erase(0, pos + 1);
}
std::cout << BCDMap[bcdCode];// << std::endl;
return bcdCode;
}