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
A program I'm debugging is receiving SIGINT at this point:
#0 0x00007ffff590784e in toupper () from /lib/x86_64-linux-gnu/libc.so.6
The code in question is this:
std::string search = name;
std::transform(search.begin(), search.end(), search.begin(), ::toupper);
Right now I don't know whats the content, but special symbols are allowed.
I already got that I'm doing something wrong here, sinse its actually UTF-8 and I should be converting it to std::wstring before performing case transformations. Howover, even though I was doing it the wrong way I don't understand why it should crash.
What are the reasons for toupper to crash for SIGINT? And how can I avoid that some user input can this to my server application?
UTF-8 uses numbers that will appear negative when viewed as a signed char (which is the usual). Giving those negative numbers to toupper gives undefined behavior.
Try:
std::transform(search.begin(), search.end(), search.begin(),
[](unsigned char ch) {return ::toupper(ch); });
...and see if things don't work a little better.
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 3 years ago.
Improve this question
My code was converted to these random characters at some point after I saved my program using Vi. I did this project for a grade in one of my college courses and didn’t get any credit, despite the fact that I spent hours working on my code for this to happen. If anyone knows how to convert it back to C++ I would be thankful.
Turns out I had saved my file under the wrong folder and I was able to recover my original file. Thanks to all for helping out with this! It seems like it always tends to be something so simple...
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
I'm trying to create a simple program to take serial numbers as an input and then searche them to find out what year the product was made in.
I am having some trouble getting a std::string to hold both numbers and letters.
std::string serial;
serial = b1234;
For example, if I run this through my compiler, I get the error message 'invalid digit in decimal constant'.
Is there a simple way to hold letters and numbers together in a string?
Basic C++ syntax says that string literals are delimited by " quotation marks.
const std::string serial = "b1234";
No. A a std::string is a string of characters, so you can't save integers in that string.
You're trying to write "b1234", I guess. Have you read any tutorial on C++ strings?
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
double testpower;
testpower = pow(400,-9);
testpower giving me 3.8146972656250003e-024 which is different calculator output of 4E-7
Anyone have any idea why??
calculator output of 4E-7
You entered the wrong calculation into your calculator.
You entered 400×10-9, instead of 400-9.
These are absolutely not the same thing!
The C++ program is correct: pow(400, -9) calculates 400-9, which is approximately 3.815×10-24.
Here is some further reading for you:
http://en.wikipedia.org/wiki/Scientific_notation#E_notation
4E-7 seems like you accidentally input 400 * 10^-9 or 400E-9.
You're looking for 400^-9, which should give 3.8146972656250003e-024.
The result you are getting 3.8146972656250003e-024 is completely correct. Maybe your calculator does not have that precission and that is why you are getting that error. Try to do 1/400^9.
I just tested 400^(-9) on the Windows calculator tool and I got the same output as your program. I think the program is fine, it may be your manual calculation that is the problem here.
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
Twice now my server has crashed after several days of running on this line.
int randomValue = rand() % m_list.size();
where m_list is
std::list<int> m_list;
The crash is
Program terminated with signal 8, Arithmetic exception.
getting the size of a list should be guaranteed to not be negative. What could cause this crash? Can something with rand be the cause? I seed rand at the start of my server with
srand(time(NULL));
Any tips are appreciated!
I don't have that much information about the situation, but does the list have anything in it? If not, you'd be dividing by zero, and that would explain everything.
So, first step is to make sure that m_list is not zero.
If it is, perhaps you could check to make sure that the size of the list is not zero before performing the operation.