How to get rid of these c++ warnings? [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
warning: argument to `int' from `lua_Number'
I get these warnings from using the lua_tonumber function. What is the problem?

When you want an int from a lua_Number, use lua_tointeger.

From http://pgl.yoyo.org/luai/i/lua_tonumber:
lua_Number lua_tonumber (lua_State *L, int index);
It wants an int for the 2nd parameter. Your warning says, helpfully, "argument to int' fromlua_Number'". So you're probably passing in a 'lua_Number' for the 2nd parameter, rather than an int. a 'lua_Number' is a double.
Since it's an index number into Lua's stack, passing a double doesn't make any sense. I would check your code as passing in a lua_Number is probably a mistake.

Based on the provided and spare informations, my answer is this:
lua_Number is a double. So it is complaining about getting converted to an int.
The easiest method to convert it is this:
lua_Number a = 3.7;
int b;
b = static_cast<int>(a)
However, b will be 3. If you want to round it, you can do something like this:
lua_Number a = 3.7;
int b;
b = static_cast<int>(a+0.5)
This way you can ensure that every number which has a suffix higher or equal to 0.5 is rounded.
But you need to decide yourself, what kind of solution you want here.

Try static_cast? It usually suppresses warnings, but I'm not familiar with Lua.

Related

String Encode in C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to convert the string "Test €" to "Test &#8364 ;" and vice versa. Please make a note that its a string and not from the xml. For your information I am developing the application in C++ and using Xerces also for XML Parsing. Please help me how it can be achieved in c++ application.
Thanks,
Ram
I think this answer may be platform-dependent, though I don't know for sure.
You can use stringstreams and casting. If lookup is a string holding the decimal version of the character code, this function will return the character version:
char fixchar(string lookup){
stringstream converter (lookup);
int i;
converter >> dec >> i;
return (char)i
(Note that for hex strings, which are prefixed with #x instead of #, you can just use hex instead of dec).
You can get the lookup strings by using the find function on the original string. Here's a loop that uses the above function to convert a string (called fixd) with &#x[number] substrings into a normal string with no character codes:
while (fixd.find("&#x")!=string::npos){
tag = int(fixd.find("&#"));
endtag = int(fixd.find(";"));
fixd = fixd.substr(0,tag) + fixchar(fixd.substr(tag+3,endtag-tag-3)) + fixd.substr(endtag+1, fixd.length()-tag-4);
}
Similarly, you should be able to get the int version of a character just by casting it, after which you can do whatever you want with it, including adding it in decimal form to a string.

Function to choose a pivot for quick sort [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
int choose_pivot(int lo, int hi){
int mid = ((hi-lo)/2)+1;
if((arr[hi]<arr[lo])^(arr[hi]<arr[mid])){
return hi;
}
else if((arr[lo]<arr[hi])^(arr[lo]<arr[mid])){
return lo;
}
else{
return mid;
}
}
This function should choose the pivot as follows. Consider the first, middle, and final elements of the given array. (If the array has odd length it should be clear what the "middle" element is; for an array with even length 2k, use the kth element as the "middle" element. Identify which of these three elements is the median. Return the its value.
Is there anything wrong with this function which may not let it do what it is meant for? Am I missing some case for which the function doesn't give desired result?
If you are trying to solve task from coursera.org, you need to use
int mid = ((hi-lo)/2);
There is one thing that I would consider a fatal error: there's
no documentation with regards to pre-conditions. In particular,
is calling it with lo == hi legal or not. (If it's legal, the
code has undefined behavior.) And what do lo and hi
represent? Under the usual C++ conventions, which I find work
very well, lo would be inclusive, and hi exclusive. But
again, you don't tell us, despite the fact that it is important
to know if we are to judge the correctness of the code. (The
fact that you do arr[hi] makes me think that you've adopted
the somewhat unusual convention of making both ends inclusive.)
Without such information, it's impossible to do anything but
make stylistic comments (e.g. use != rather than ^ for
comparing results, use return with ?: rather than if ... else
if ... else, etc.).
You should probably use != instead of ^ to compare boolean results for inequality.
Based on the textual description, you should be returning arr[hi] instead of hi (and similar for mid, lo).

Print a string store into the stack - Tricky challenge [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I found following C++ source code:
void victory()
{
printf("xxxxxxxxxxxxxxxxxxxxxxxx"); //random string
}
int main()
{
if(0) victory();
/* code here! */
}
Your main objective is to print the random string(from function victory) on stdout
How can It possible? How can I print the random string? It's a tricky traps, just for curiosity. Above all, I can't use pointers...
Rules:
Max 12 chars.
You can't use: "main", "victory", "asm", "%", "*", "_", "#", "/", "&".
You have only one semicolon.
It is pretty clear that there is no way to solve this in any remotely standards-compliant way.
Since this is being framed as a "security" question, this suggest that it's OK to use compiler- and OS-specific hacks. With this in mind, the following works on my system using gcc:
#include <stdio.h>
void victory()
{
printf("SxxxxxxxxxxxxxxxxxxxxxxE"); //random string
}
int main()
{
if(0) victory();
puts(""-25);
}
It relies on the fact that the "" gets placed by the compiler immediately after the string literal that we're trying to recover. Here, 25 is the (known) length of the unknown string.
Of course, this has UNDEFINED BEHAVIOUR written all over it in huge neon letters. Handle with care.

Getting char from array of strings [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of strings and an array of ints. Usually when I want to get a certain char from string I use:
string[char_position];
But when I have two arrays the same way of getting char doesn't work:
string[i][char_position[i]];
How can I get certain char from array of strings?
Try it like this instead:
string[i][char_position];
You sholdn't be subscripting char_position.
In first example you are using char_position as int but in second you are using it as array. Either one of this is wrong obviously if you mean same variable in two cases. But what it looks like you want to access jth charecter of ith string. If it is that then
string[i][j]; // j is position of char in string[i]
string[i][char_position[i]]; means the instructions:
get the INTEGER from char_position array indexed at i
get the character in ith string, in the position got from the first step
This is valid and correct ONLY if char_position is an ARRAY and not a SCALAR VARIABLE.
On the other hand, if char_position IS a scalar variable THEN:
string[i][char_position] is the way to retrieve char_positionth character in the ith string, in the array of character arrays string

Find function doesn't return the right value [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Is it not possible to fill a map like this:
void Spel::Fill( void )
{
int buildslist[] = {3,3,2,2,2,2,3,2,2,2,2,3,3,2,2,2,2,1,1,1,1,1};
Building::buildings p;
for( int i = Building::INDIGOK; i < Building::STADSHUIS; i++)
{
p = (Building::buildings) i;
gebouwenMap[p] = buildslist[i];
}
}
This gives all 0. Building::buildings is an enum with some building names. The buildslist is a list of how many people could join that building.
First, there is not enough code to give an actual answer. Please improve your question so that we can help you properly.
Second, the title is misleading "Find function doesn't return the right value". There is no "find" function and there is no "return value" because the only function you show returns void.
Now thet this is sorted out, let me try to help you:
p = (Building::buildings) i;
This is a cast from int to enum. I think this is bad C++ (probably undefined as in might work for some compiler but not as a rule of thumb). You would have to use a switch here I think.
Please write code in English. Do you imagine if someone you work with is Japanese and would write the code with japanese variable names? Even if the project is in Dutch, write code in English including comments.
EDIT: You might want to use strings instead of an enum here.
Try using a std::map<std::string, int> to encode your building names instead of an enum, then use an std::map::iterator to iterate through it.