Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to use ostringstream to concatenate a string in a loop. Unfortunately, only the most recent stream is used. Can anyone point me in the right direction on accomplishing this or similar (other the + with string concatenation)?
Thanks
std:ostringstream os;
for (int i = stk.pop(); i != 0; i = stk.pop()) {
os << i << endl;
}
cout os.str();
So the value of 'os' is overwritten every time? Is there a way to append to the stream?
SOLUTION
This code works, I had a bug.
Thanks
That code shouldn't be overwriting os. It should be appending to it using the << operator. Perhaps your bug is elsewhere? Maybe your stack only really has the last element?
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
I am trying to output a hex value using a variable for the number. Here is an example of it with no var.
system("echo '\x6e'");
//output:
//n
Perfect.
But this doesn't work:
for (int i=1; i<7; i++) {
system(("echo '\x"+to_string(i)+"e'").c_str());
}
//Which does not even compile.
//compiler:
//error: \x used with no following hex digits
Well if you actually let the code run there would BE hex digits. I try \\x but then it just literally prints \x1e etc.
The comment explains the issue. Possible solution
system(("echo '"s + static_cast<char>(i * 16 + 0xe) + "'").c_str());
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I have a dynamic queue, so I want to check if the head pointer is pointing to something. So I do this:
if (mHeadPtr = NULL)
return false;
The pointer isn't empty (which is also why it does not go into the "return false" line) before this executes, but somehow it is afterwards, and I have no idea why.
If I instead check if the variable is not empty, it works without a problem.
if (mHeadPtr != NULL)
std::cout << "yay!" << std::endl;
else
return false;
How can it be that the if statement changes the variable it's only supposed to read?
For comparisions you should use == instead of =. By using a single = you don't compare the value of the variable but assign NULL to it.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I wanted to clear string from unwanted chars, and I tried to iterate it through a loop like this.
for(auto it=numer.begin(); it!=numer.end(); ++it)
{
if(*it=='-') numer.erase(it);
}
The error is: "expected primary-expression before '=' token";
I could, of course, I could do this with [] operator. But I am wondering why it doesn't work.
I appreciate your help.
If you want to remove all instances of a character from a string, a simple way to do that would be to use the standard erase-remove(if) idiom:
numer.erase(std::remove(numer.begin(), numer.end(), '-'), numer.end());
See also:
https://en.cppreference.com/w/cpp/string/basic_string/erase
https://en.cppreference.com/w/cpp/algorithm/remove
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
How can I convert the below statement from c language into c++ language
Char_t message[80];
sprintf(message,"Total : #chi^{2}/NDF = %.2f",fun->GetChisquare()/fun->GetNDF());
Thanks in advance for help.
There's nothing you need to do. It's legal C++ as is.
well you could do something like this
#include <sstream>
stringstream ss;
ss << std::fixed;
ss << "Total : #chi^{2}/NDF = "
<< setprecision(2)
<< fun->GetChisquare()/fun->GetNDF();
string message(ss.str());
Do nothing. This looks like valid C++ code.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am sorting my vector of strings in the following way, so that I can binary_search it later.
std::vector<std::string> vec;
...........
...........
std::sort(vec.begin(),vec.end());
Now I am searching it as follows.
if (!std::binary_search(vec.begin(), vec.end(), "SomeString"));
{
//Not Found
}
else
{
//Found
}
However, it seems that the binary_search is not working, and it returns a false to the "strings" that are present in the vector.
What might I be doing wrong?
Look at the very last character on this line:
if(!std::binary_search(vec.begin(),vec.end(),"SomeString"));
You have a misplaced ; there. Remove it and test again.