getline, stringstream, delimiter stuff - delimiter

how do I use getline to separate a string ex. "you8only8live8once"? This solution only sets my first word to "you" and then stops at the first "8".
Also if I want to the "8" to be an arbitrary delimiter do I just put in cin >> delimiter; then change "getline(cin, phrase, '8');" to "getline(cin, phrase, delimiter);"?
istringstream inSS;
string phrase;
string firstWord;
string secondWord;
string thirdWord;
string fourthWord;
cout << "Please enter a digit infused string to explode:" << endl;
cout << "Please enter the digit delimiter:" << endl;
getline(cin, phrase, '8');
inSS.str(phrase);
inSS >> firstWord >> secondWord >> thirdWord >> fourthWord;
cout << "The 1st word is: " << firstWord << endl;
cout << "The 2nd word is: " << secondWord << endl;
cout << "The 3rd word is: " << thirdWord << endl;
cout << "The 4th word is: " << fourthWord << endl;
return 0;

Related

I need some help getting the right output stream format in decimal in C++17

I made this little currency converter program that convert dollar to franc, but when I put an amount of, for example $2000 and more, I do not have the correct format, I have this: 1.15165e+006.
I want the entire decimal amount.
Thanks
Convert USD to Franc CFA
#include <iostream>
using namespace std;
int main()
{
const double cfa_per_usd {575.825};
cout <<"**********Welcome to the USD to Franc CFA Converter************" << endl;
double cfa {0.0};
cout <<"\nEnter value in USD: ";
double dollar {0};
cin >> dollar;
cfa = dollar * cfa_per_usd;
cout << dollar <<" Dollar(s) is equivalent to " << cfa << " Francs CFA" <<endl;
return 0;
}
I found a partial answer to my problem, I have added the library , then added "fixed" key word and the "setprecision()":
cout << dollar <<" Dollar(s) is equivalent to " << fixed << setprecision(2) << cfa << " Francs CFA" <
But I realized that I only have zeros after the decimal point: just 2 “.00”, I changed the constant to 576.212 for a dollar, so if I convert $2000 I should have 1,1512,424.54 francs, but I just have 1152424.00 francs, the .54 is not there, any idea how to fix it?
I found a partial answer to my problem, I have added the library, then add "fixed" key word and the "setprecision()":
cout << dollar <<" Dollar(s) is equivalent to " << fixed << setprecision(3) << cfa << " Francs CFA" <<endl;
But I realized that I only have zeros after the decimal point: just 2 “.00”, I changed the constant to 576.212 for a dollar, so if I convert $2000 I should have 1,1512,424.54 francs, but I just have 1152424.00 francs, the .54 is not there, any idea how to fix it?

opencv2 covariance matrix strange results

The following code gives inconsistent covariance matrix sizes.
cv::Mat A = (cv::Mat_<float>(3,2) << -1, 1, -2, 3, 4, 0);
cv::Mat covar1, covar2, covar3, covar4, mean;
calcCovarMatrix(A, covar1, mean, CV_COVAR_NORMAL | CV_COVAR_ROWS);
calcCovarMatrix(A, covar2, mean, CV_COVAR_SCRAMBLED | CV_COVAR_ROWS);
calcCovarMatrix(A, covar3, mean, CV_COVAR_NORMAL | CV_COVAR_COLS);
calcCovarMatrix(A, covar4, mean, CV_COVAR_SCRAMBLED | CV_COVAR_COLS);
std::cout << "size: " << covar1.size() << "\n";
std::cout << "size: " << covar2.size() << "\n";
std::cout << "size: " << covar3.size() << "\n";
std::cout << "size: " << covar4.size() << "\n";
covar1 and covar2 should have the same size because they both describe the covariance over the rows, and covar3 and covar4 should have the same size because they both describe the covariance over the columns, respectively. However, the output is:
size: [2 x 2]
size: [3 x 3]
size: [3 x 3]
size: [2 x 2]
The calcCovarMatrix() docs, specifically say that when using CV_COVAR_SCRAMBLED "The covariance matrix will be nsamples x nsamples."

libusb_bulk_transfer timeout while write

I have a USB printer device. I want to send file data to the USB printer from Linux. I am using libUsb for my code. I am getting timeout (libusb return value -7) always while sending. But I can able to send data in Windows for the same printer. What went wrong ? It seems ehci or uhci is not sending data to the printer. Please Help .
OS : Ubuntu 12.04 (32 Bit)
The below is my code snippet.
dev_handle = libusb_open_device_with_vid_pid(ctx, PRINTER_VID, PRINTER_PID);
if (dev_handle == NULL)
{
cout << "Cannot open device" << endl;
libusb_free_device_list(devs, 1); //free the list, unref the devices in it
return;
}
else
{
cout << "Device Opened" << endl;
}
if (libusb_kernel_driver_active(dev_handle, 0) == 1) //find out if kernel driver is attached
{
cout << "Kernel Driver Active" << endl;
if (libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
cout << "Kernel Driver Detached!" << endl;
}
r = libusb_claim_interface(dev_handle, 0);
if (r < 0)
{
cout << "Cannot Claim Interface" << endl;
return 1;
}
cout << "Claimed Interface" << endl;
cout << "Writing Data..." << endl;
memset(data_buffer,0,64);
while(fgets((char *)data_buffer,64,fp))
{
errno = 0;
r = libusb_bulk_transfer(dev_handle,0x081 | LIBUSB_ENDPOINT_OUT, data_buffer, 64,&actual, 10);
cout<<"The return value of r is "<<r<< "::::" << actual << endl ;
memset(data_buffer,0,64);
}
The output is
The return value of r is -7::::0
The return value of r is -7::::0
Try to execute following comamnd from superuser and then reconnect device.
echo 0 > /sys/bus/usb/drivers_autoprobe
It helped me with some devices.

Decoding AudioStreamBasicDescription's mFormatFlags number (ASBD.mFormatFlags)

I'm writing an iOS app that uses CoreAudio's Audio Unit API, and at a certain point I do a AudioUnitGetProperty(audioUnit, kAudioUnitProperty_StreamFormat, ...) call. At that point I set a breakpoint to look into the ASBD, and I find the mFormatFlags field is 41. The question is, can I decode the actual flag names from that number (such as kAudioFormatFlagIsNonInterleaved | kAudioFormatFlagIsPacked | ...)?
Thanks a lot.
Here is an ostream overload for printing an ASBD, taken mostly from Apple sample code:
std::ostream& operator<<(std::ostream& out, const AudioStreamBasicDescription& format)
{
unsigned char formatID [5];
*(UInt32 *)formatID = OSSwapHostToBigInt32(format.mFormatID);
formatID[4] = '\0';
// General description
out << format.mChannelsPerFrame << " ch, " << format.mSampleRate << " Hz, '" << formatID << "' (0x" << std::hex << std::setw(8) << std::setfill('0') << format.mFormatFlags << std::dec << ") ";
if(kAudioFormatLinearPCM == format.mFormatID) {
// Bit depth
UInt32 fractionalBits = ((0x3f << 7)/*kLinearPCMFormatFlagsSampleFractionMask*/ & format.mFormatFlags) >> 7/*kLinearPCMFormatFlagsSampleFractionShift*/;
if(0 < fractionalBits)
out << (format.mBitsPerChannel - fractionalBits) << "." << fractionalBits;
else
out << format.mBitsPerChannel;
out << "-bit";
// Endianness
bool isInterleaved = !(kAudioFormatFlagIsNonInterleaved & format.mFormatFlags);
UInt32 interleavedChannelCount = (isInterleaved ? format.mChannelsPerFrame : 1);
UInt32 sampleSize = (0 < format.mBytesPerFrame && 0 < interleavedChannelCount ? format.mBytesPerFrame / interleavedChannelCount : 0);
if(1 < sampleSize)
out << ((kLinearPCMFormatFlagIsBigEndian & format.mFormatFlags) ? " big-endian" : " little-endian");
// Sign
bool isInteger = !(kLinearPCMFormatFlagIsFloat & format.mFormatFlags);
if(isInteger)
out << ((kLinearPCMFormatFlagIsSignedInteger & format.mFormatFlags) ? " signed" : " unsigned");
// Integer or floating
out << (isInteger ? " integer" : " float");
// Packedness
if(0 < sampleSize && ((sampleSize << 3) != format.mBitsPerChannel))
out << ((kLinearPCMFormatFlagIsPacked & format.mFormatFlags) ? ", packed in " : ", unpacked in ") << sampleSize << " bytes";
// Alignment
if((0 < sampleSize && ((sampleSize << 3) != format.mBitsPerChannel)) || (0 != (format.mBitsPerChannel & 7)))
out << ((kLinearPCMFormatFlagIsAlignedHigh & format.mFormatFlags) ? " high-aligned" : " low-aligned");
if(!isInterleaved)
out << ", deinterleaved";
}
else if(kAudioFormatAppleLossless == format.mFormatID) {
UInt32 sourceBitDepth = 0;
switch(format.mFormatFlags) {
case kAppleLosslessFormatFlag_16BitSourceData: sourceBitDepth = 16; break;
case kAppleLosslessFormatFlag_20BitSourceData: sourceBitDepth = 20; break;
case kAppleLosslessFormatFlag_24BitSourceData: sourceBitDepth = 24; break;
case kAppleLosslessFormatFlag_32BitSourceData: sourceBitDepth = 32; break;
}
if(0 != sourceBitDepth)
out << "from " << sourceBitDepth << "-bit source, ";
else
out << "from UNKNOWN source bit depth, ";
out << format.mFramesPerPacket << " frames/packet";
}
else
out << format.mBitsPerChannel << " bits/channel, " << format.mBytesPerPacket << " bytes/packet, " << format.mFramesPerPacket << " frames/packet, " << format.mBytesPerFrame << " bytes/frame";
return out;
}

setw() - adjustfield (left, right or internal)

I am going though the string functions doing tests to learn them (I am a newbie programmer)
Anyway, I am currently looking at setw() but I seam to not understand it... I think I understand the basic use and the use of setfil
here is my test code
http://ideone.com/czAXH
Anyway the cplusplus website says.. "format flag adjustfield (left, right or internal)" but doesn't say how to use this?
I assume this means I can do the above code but place the "spacing" after the word instead of before it..
How do I do that?
std::cout << std::left
<< "[" << std::setw(3) << 1 << "," << std::setw(5) << -100 << "]\n";
std::cout << std::internal
<< "[" << std::setw(3) << 1 << "," << std::setw(5) << -100 << "]\n";
std::cout << std::right
<< "[" << std::setw(3) << 1 << "," << std::setw(5) << -100 << "]\n";
Outputs:
[1 ,-100 ]
[ 1,- 100]
[ 1, -100]

Resources