Now I need to create a logic to append a code to the end of the file using the following matrix rules.
00 N N N N
01 N N N Y
02 N N Y N
03 N N Y Y
04 N Y N N
05 N Y N Y
06 N Y Y N
07 N Y Y Y
08 Y N N N
09 Y N N Y
10 Y N Y N
11 Y N Y Y
12 Y Y N N
13 Y Y N Y
14 Y Y Y N
15 Y Y Y Y
In the above example these four flags are "N|Y|N|N", so I need to append the matching code at the end of file "04".
desired output :
acct|N|Y|N|N|rose@gmail.com|04|
as it matches code '04': 04 N Y N N
can anyone help me with the logic need to be used here?
Thanks in advance:)
#include <sstream>
#include <string>
#include <bitset>
#include <iostream>
#include <iomanip>
int main()
{
const std::string test[] = { "...|N|Y|N|N|...", "...|Y|N|Y|N|...", "...|N|Y|Y|N|..." } ;
for( const std::string& str : test )
{
std::istringstream stm(str) ;
// parse the four Y/N into bool values
// and form a bitset of four bits with with 'Y' == 1
constexpr std::size_t NBITS = 4 ;
std::bitset<NBITS> bits ; // set of four bits
char Y_or_N ;
for( std::size_t i = 0 ; i < NBITS ; ++i )
{
stm.ignore( 100, '|' ) ; // throw characters away upto and including the next '|'
stm >> Y_or_N ; // read the next char ('Y' or 'N')
bits[ (NBITS-1) - i ] = Y_or_N == 'Y' ; // set the corrosponding bit to 1 if it is a 'Y'
}
// get the code by converting the bits to an integral value
auto code = bits.to_ulong() ;
// check it out
std::cout << str << " " << bits << " " << std::setw(2) << std::setfill('0') << code << '\n' ;
}
}
I'm also trying to use strstr function for doing so.
like
if (strstr (rec,"|N|Y|N|N|")))!= NULL)
append "04";
else if (strstr (rec,"|N|Y|Y|N|")))!= NULL)
append "06";
.
.
.
so on..