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

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.

Related

Getting reference error: "invalid count value"

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?

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.

Abstract Data Type Pass as a parameter

Hey so I'm trying to pass a hash table as a parameter in c++, when I call the function that I am trying to run i get an error message that i do not understand.
So this is the function:
string getRandomKey(int tableNumber, int tableSize, HashTable<string>* table, int random){
random *= rand() % tableSize + 1;
string randKey = to_string(tableNumber) + to_string(random);
if((table->find(randKey)) == true){
cout << "Key: " << randKey << " found ";
return randKey;
}
return "";
}
This is by no means the final version I'm just trying to test it. Some context is that I have a couple of hash tables, and a separate integer variable that has the number of elements that i have predetermined. The keys are set to be one of the random numbers.
Anyway so here is where I call the function:
table1->print(getRandomkey(1, sizes[2], table1*, 1));
And I get this error:
error: expected expression
table1->print(getRandomKey(1, sizes[2], table1*, 1));
^
1 error generated.
So, I'm not sure what I need to change or if I messed something up somewhere else. Thanks for any help you guys can give!
It appears that table1 is a pointer to a HashTable so when you
make the call to getRandomKey the term table1* should just be table1.
table1->print(getRandomkey(1, sizes[2], table1, 1));

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 *.

returning multiple strings from a function

I have a function where i want to be able return what is printed out so then i can print the output on the page. How will i return both of the string which are outputted within both of those if statements ?
std::vector<std::string> el;
split(el,message,boost::is_any_of("\n"));
std::string a ("");
for(int i = 0; i < el.size(); i++)
{
if(el[i].substr(0,3) == ".X/")
{
DCS_LOG_DEBUG("--------------- Validating .X/ ---------------")
std::string str = el[i].substr(3);
std::vector<std::string>st;
split(st,str,boost::is_any_of("/"));
boost::regex const string_matcher(splitMask[0]);
if(boost::regex_match(st[0],string_matcher))
{
a = "Correct Security Instruction";
}
else
{
a = "Incorrect Security Instruction"
}
boost::regex const string_matcher1(splitMask[1]);
if(boost::regex_match(st[1],string_matcher1))
{
a = "Correct Security screening result"
}
else
{
a = "Incorrect Security screening result"
}
return a;
}
}
Thankfull for any help :)
You can return an std::pair of strings, for instance.
Define a class with two appropriately-named string members and return an instance of that.
Then, start to think about what methods or other data would be useful to have on that class.
You can push the strings in a std::vector that is passed to the function as a reference and later iterate over the vector upon return.
I would return a std::pair of bool values (one to indicate if the instruction is correct, and one to indicate if the screening result is correct), and let the calling code interpret the results.