Getting reference error: "invalid count value" - referenceerror

I'm getting "invalid count value" with this block of code and can't figure out what I am doing wrong.
pad(string, length) {
if(length < string.length) {
return string;
}
const startPaddingLength = Math.floor(length - string.length / 2);
const endPaddingLength = length - string.length - startPaddingLength;
const paddedString = ' '.repeat(startPaddingLength) + string + ' '.repeat(endPaddingLength);
return paddedString;
}
This piece of code is written exactly the way my course tutorial shows it should be. It works in the video walk through but not when I test it in my console. I have tried using MDN to research this type of error message, but wasn't really finding anything to lead me in the right direction.
What can I try next?

Related

Microsoft Incremental Linker has stopped working VS17

Recently VS17 started giving me the "Incremental Linker has stopped working" a lot, and I mean A LOT. I did not update anything (OS or VS). It started out of a sudden for no apparent reason. Most of the time I managed to change my code so it wouldn't happen.
This is my current code (it's supposed to XOR a string and return hex escaped string):
__inline char* EncryptString(const char* String, const char* Key)
{
char* szEncrypted = new char[lstrlenA(String) + 1];
memcpy(szEncrypted, String, lstrlenA(String));
for (int32_t i = 0; i < lstrlenA(String); ++i)
szEncrypted[i] = String[i] ^ Key[i % (sizeof(Key) / sizeof(char))];
std::stringstream lpStream;
for (int32_t i = 0; i < lstrlenA(szEncrypted); ++i)
{
char cCharInd = szEncrypted[i];
int32_t nCharNum = static_cast<int32_t>(cCharInd);
lpStream << "\\x" << 2;
}
std::string sHexEscaped = lpStream.str();
lpStream.clear();
delete[] szEncrypted;
char* szReturn = new char[sHexEscaped.length() + 1];
memcpy(szReturn, sHexEscaped.c_str(), sHexEscaped.length() + 1);
return szReturn;
}
Is any hotfix coming? Or maybe you know what in my code caused this? (Yes, I am deleting the returned char*. Not that it has anything to do with the linker error but don't bully me because of it).
Or is anyone else experiencing this in VS17?
Thanks mate, it works. You can make it an answer if you want and I'll
accept it
It seems that it might be some sort of bug in VS2017. This kind of linker error happened to me even with relatively small code and something as simple as changing the output value of std::cout would trigger it for example. The solution seems to be to run Clean action on the code, which could be found in
Solution Explorer -> [Right Click on the project name] -> Clean

Finding last word in a string

I'm trying to return the last word in a string but am having trouble with the for loops. When I try to test the function I am only getting empty strings. Not really sure what the problem is. Any help is much appreciated.
string getLastWord(string text)
{
string revLastWord = "";
string lastWord = "";
if(text == "")
{
return text;
}
for(size_t i = text.size()-1; i > -1; i--)
{
if((isalpha(text[i])))
{
revLastWord+=text[i];
}
if(revLastWord.size()>=1 && !isalpha(text[i-1]))
{
break;
}
}
for(size_t k = revLastWord.size()-1; k > -1; k--)
{
lastWord+=revLastWord[k];
}
return lastWord;
}
I was coding up another solution until I checked back and read the comments; they are extremely helpful. Moreover, the suggestion from #JustinRandall was incredibly helpful. I find that find_last_of()
and substr() better state the intent of the function--easier to write and easier to read. Thanks! Hope this helps! It helped me.
std::string get_last_word(std::string s) {
auto index = s.find_last_of(' ');
std::string last_word = s.substr(++index);
return last_word;
}
/**
* Here I have edited the above function IAW
* the recommendations.
* #param s is a const reference to a std::string
* #return the substring directly
*/
std::string get_last_word(const std::string& s) {
auto index = s.find_last_of(' ');
return s.substr(++index);
}
The other answers tell you what's wrong, though you should also know why it's wrong.
In general, you should be very careful about using unsigned value types in loop conditions. Comparing an unsigned type like std::size_t and a signed type, like your constant -1, will cause the signed to get converted into an unsigned type, so -1 becomes the largest possible std::size_t value.
If you put some print statements throughout your code, you'll notice that your loops are never actually entered, because the conditional is always false. Use an int when performing arithmetic and especially when signed numbers are compared with.

C++ Wierd crash on function called

I checked the return of GetCharacterRankINT() and it is returning the right value (1). When I tested the query with the rankNum 1 I got the right result, but whenever I try to use GetCharacterRankSTR() it crashes with the following crash dump: http://fbe.am/rwl (Password: stackoverflow). I tried to set the type of the function from string to std::string and it still didn't work. The MySQL table has 8 columns, or so the field has 8 entries. Therefore the error is not related to the amount of fields.
string Player::GetCharacterRankSTR()
{
QueryResult* res = CharacterDatabase.Query("SELECT * FROM ars_ranks WHERE rankNum = %u LIMIT 1;", GetCharacterRankINT());
if (!res)
{
return "Error";
}
else
{
Field* fld = res->Fetch();
return fld[3].GetString();
}
}
My question is: What am I doing wrong in the function so that it crashes?
After some tests, I found out how to fix it... I just had to change from string to const char *.

C++ Error: Wunused-but-set-variable

I get the error I mentioned in the title when I try to compile the following code:
void Sql::select(const string table, const string column, const string condition, const string condition_2, const string condition_3) {
otl_stream s;
otl_column_desc* desc;
int desc_len;
const string select = str(format("SELECT %2% FROM %1% WHERE LEFT(%3%, 8) < %6% AND %4% = 'Ausstehend' AND (%5% = '1' OR %5% = '2') ")
% table % column % condition % condition_2 % condition_3 % getDate());
// cout << select;
try {
s.open(10, select.c_str(), con);
} catch (otl_exception &e) {
cerr << e.msg;
}
desc = s.describe_select(desc_len);
}
I am told that otl_column_desc* desc is set but not used. Can you tell me what goes wrong there?
Its exactly what it says, you are setting the variable but not using the value anywhere, meaning this variable is, for all intents and purposes, useless.
desc = s.describe_select(desc_len);//value in desc never used
This sometimes could happen if you make a mistake in your code, and use some other variable when you meant to use this one, and I guess this warning is to catch those cases.
But to answer your question, nothing is wrong since this is a warning, not an error. It is just an indication that something might be wrong.

length of sub-sequence in the string

I need to implement lastSeq function,which gets as argument string str and char chr
and returns the length of last sequence of repeated chr (the sequence can be of any length)
for example:
lastSeq("abbaabbbbacd",'a') should return 1
lastSeq("abbaabbbbacd",'b') should return 4
lastSeq("abbaabbbbacd",'t') should return 0
Is there C++ function which can solve it?
This appear to be homework, so I'm just going to give you direction so that you can find the answer by yourself.
First, how would you do it yourself, without a computer to give the correct result for your samples. From those manual run, how would you generalize then in simple steps so that you can solve the problem for all different inputs.
By this point you should have a rough algorithm to solve the problem. What do you know about storage of string in C++, and the method that are available from that class? Can some one them be used to solve some of the steps of your algorithm?
Try to write a program using those function, to compile it and to run it. Do you get the expected result? If not, can you try to print intermediate state (using std::cout << "Some value: " << variable << "\n";) to try to debug it.
Once you have done all of that, and if you are still having issues, update your question with your code, and we'll be able to give you more directed help.
int lastSeq(char *str, char chr)
{
int i = strlen(str);
int l = 0;
while(--i>=0)
if(*(str + i) == chr && ++l)
break;
while(--i>=0 && chr == *(str + i) && ++l);
return l;
}