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]
Related
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?
Given
template<typename ...Types>
void print(Types&& ...args) {
(cout << ... << args);
}
// ....
print(1, 2, 3, 4); // prints 1234
How to add spaces so we get 1 2 3 4?
Update:
Correct answer:
((std::cout << args << ' ') , ...);
The usual workaround is to fold over the comma operator instead, though the simplistic approach will leave a trailing space:
((std::cout << args << ' '), ...);
Changing it to avoid the trailing space is left as an exercise for the reader.
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;
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."
my problem is just astonishing. This is the code
#define NCHANNEL 3
#define NFRAME 100
Mat RR = Mat::zeros(NCHANNEL, NFRAME-1, CV_64FC1);
double *p_0 = RR.ptr<double>(0);
double *p_1 = RR.ptr<double>(1);
double *p_2 = RR.ptr<double>(2);
cout<< p_0[NFRAME-1] << endl << p_1[NFRAME-1] << endl << p_2[NFRAME-1] << endl;
And the output is: 0 0 -6.27744e+066 .
Where is that awful number come from? it seems I'm printing a pointer or something rough in memory. (uh, 0 is the value of all other elements, of course).
You are accessing after the last element of Mat. If you use NFRAME-1 for initialization then the last element has NFRAME-2 index.