Adding braces for If-else using Uncrustify - if-statement

I was wondering if there is any way to add braces in nested If-else using Uncrustify. For example:
if( stat_error == -1 ){
if ( debug > 0 )
printf( "...ERROR ); //I would like to add braces around here.
exit( -1 );
} else {
I have seen this:
# Add or remove braces on single-line 'if' statement. Will not remove the braces if they contain an 'else'.
mod_full_brace_if = add # ignore/add/remove/force
But it doesn't seem to work for nested conditionals.
Is there any way to do it?

My experience with Uncrustify in your example :
Add or remove braces on single - line if statement. Will not remove the braces if they contain an else.
mod_full_brace_if = add
Make all if / elseif / else statements in a chain be braced or not. Overrides mod_full_brace_if.
If any must be braced, they are all braced. If all can be unbraced, then the braces are removed.
mod_full_brace_if_chain = false
And it worked for me.

you need to add a return statement should look like this
if( stat_error == -1 ){
if ( debug > 0 )
printf( "...ERROR ); //I would like to add braces around here.
exit( -1 );
} else{
Insert else statement
}
return statement here
}

Related

Making clang-format break after 'else'

I prefer the following style:
if ( a )
foo();
else // <- line break after else
if ( b )
bar();
However, clang-format seems to always want to glue 'else if' together on the same line. Is there a way to achieve what I want with clang-format?

C++ array greater than 0 print code

I am having issues with this block of code:
else if (mineOrRefine == "refine" || mineOrRefine == "Refine")
if (StoneInventory[0] == 0)
cout << "You currently have no stone!" << endl;
int a = StoneInventory[0];
else if (a == >1)
You're not saying what the problem is, but I can all but guarantee it has to do with a lack of braces. Put your if and else blocks into braces, even when it's just one line, to reduce confusion. I'm not going to get into a debate about whether to put braces around a single expression following if/else in general, only that, in your case, the lack of braces is confusing you, so put them in.
instead of ( a== >1), use (a>=1) or (a>0)
also, any 'if' statement with more than one line of code should use curly braces. ie: if (x) { /* code */ }.
You're lost in the ifs, and you really need to add curly braces to see what's going on. The code amounts to this:
if (something_you_havent_shown)
{
// something else you haven't shown
}
else if (mineOrRefine == "refine" || mineOrRefine == "Refine)
{
if (StoneInventory[0] == 0)
{
std::cout << "You currently have no stone!" << std::endl;
}
}
int a = StoneInventory[0];
else if (something_you_say_youve_changed_since_asking_the_question)
The else in that last line doesn't go with any preceding if -- they've all finished, because each one applies only to the next line.

Index of the condition that was satisfied inside if-statement

if(command[i]=='H' or command[i]=='h' or command[i]=='C' or command[i]=='c'){
do something;
}
Once the logic flow goes inside this if-statement, I want to know what exactly command[i] was. Surely I can make individual comparisons again in the inside block and find out, but is there a more elegant way of knowing, say, the index of the condition that was satisfied?
If you use
if((myC=command[i]) =='H' ||
(myC=command[i]) =='h' ||
(myC=command[i]) =='C' ||
(myC=command[i]) =='c')
then the value of the successful expression will end up in myC, because evaluation in a chain of "or"s stops at the first true subexpression.
If you go one step further you can get a number value identifying the subexpression by index.
if(((myC=1), command[i]) =='H' ||
((myC=2), command[i]) =='h' ||
((myC=3), command[i]) =='C' ||
((myC=4), command[i]) =='c')
Same concept, the first successful subexpüression is the last to be evaluated and the , operator ensures that only the second part gets used for the comparison.
Another option is to assign a value. You could use switch, an if..else tower, or a function with return statements. Here is a version with function:
int classify( char command )
{
switch( command )
{
case 'H': return 1;
case 'h': return 2;
case 'C': return 3;
case 'c': return 4;
default : return 0;
}
}
void func(void)
{
int result = classify( command[i] );
if ( result )
{
// use result value here as appropriate
}
}
It would also be possible, in fact preferable, to use an enumerator instead of magic numbers.
Just do this -
if(command[i]=='H' or command[i]=='h' or command[i]=='C' or command[i]=='c'){
print command[i]; //use whatever command is appropriate for printing
do something;
}

Make c++ interpret a pointer to NULL as zero

I have this kind of code
two->height = max(two->right->height, two->left->height);
One of the two->right or two->left can be a pointer to null so the program will seg fault . I am looking for , if the two->left is null it will get transformed into zero so the two->right will be automatically true .
Is there any trick that can overcome this issue ?
This can also work:
two->height = max(
( two->right != nullptr ? two->right->height : 0 ),
( two->left != nullptr ? two->left->height : 0 )
);
You are first going to want to perform a check on the left and right pointers and see if they are null. Something along the lines of:
if(two->right == NULL) {
...
}
else if(two->left == NULL) {
...
}
else {
two->height = max(two->right->height, two->left->height);
}
There are many ways to deal with pointers being NULL. I just picked a simple one for an example.

Pass variable "name" in C++

I currently use the following template simply as a way to check for NULL pointer and if NULL then print out an error message to a log file and then return false.
template< typename T >
static bool isnull(T * t, std::string name = "")
{
_ASSERTE( t != 0 );
if( !t )
{
if( !(name.length()) ) name = "pointer";
PANTHEIOS_TRACE_ERROR(name + " is NULL");
return false;
}
return true;
}
I currently call this as follows:
if( !(isnull(dim, BOOST_STRINGIZE(dim))) ) return false;
If you notice I need to pass in the "name" of the pointer-variable that I want to print to the log file, as the 2nd parameter. I am currently using BOOST_STRINGIZE that simply converts any text inside the parentheses to a string.
The following are the disadvantages of my template implementation (for my usage at least)
Anyone could pass in anything as parameter to BOOST_STRINGIZE to print out in log file - since the 2 parameters are not related in anyway - so I would not necessarily see the "variable name" that is actually NULL
We have to remember to pass in the 2nd parameter, else useless.
Is there anyway I can have the "name" of that 1st variable be automatically determined, so that I can omit passing it in, as the 2nd parameter, with every call?
You could put it all in one macro:
#define IS_NULL(name_) isnull(name_, #name_)
Note that BOOST_STRINGIZE expands its argument if its a macro, which may or may not be what you want:
#define X(x_) std::cout << BOOST_STRINGIZE(x_) << " = " << x_ << std::endl;
X(NULL); // prints: "0 = 0"
The only way to do anything lexically like this is with macros. If you always want the correct printout, your best option is to wrap the whole statement in a macro:
//if( !(isnull(dim, BOOST_STRINGIZE(dim))) ) return false;
#define ISNULL(a) isnull((a), #a)
if (!ISNULL(dim)) return false;
Note that, as always, macros have a number of disadvantages associated with them.
Sure, why not:
#define new_isnull(x) isnull(x, BOOST_STRINGIZE(x))