Escaping from commented out block [duplicate] - c++

This question already has answers here:
Nested Comments in C++
(4 answers)
Closed 9 years ago.
While this problem can apply to other languages I am looking for solutions that can apply to C++ language.
The problem is, when we comment a block like:
void doStuff() {
cout<<"doing stuff";
/*start();
cout<<"done";*/
}
The comment out works completely fine, however what I am wondering is when I want to comment out entire function by:
/*void doStuff() {
cout<<"doing stuff";
/*start();
cout<<"done";*/
}*/
It obviously doesn't work. The only way that I know to make it work is either:
/*void doStuff() {
cout<<"doing stuff";
start();
cout<<"done";
}*/
OR
/*void doStuff() {
cout<<"doing stuff";
*//*start();
cout<<"done";*//*
}*/
I know that there are IDE's that can automatically insert // at each line and then get rid off it but that's not what I am after.
My question is, is there any easier way of escaping from inner commented out blocks?

You can't do it with comment grammer.
Use #if 0 #endif to comment out a large portion of code with /* */ inside.

Related

Problem With Functions And Printing Function Values in arduino [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
First of all, I'm gonna say thank you to who help me
so recently I was working on a project which is called Green House in Arduino
then I was about to write a function that reads sensor, and a function that prints that value in function1 and I just came up with some problems,
Here is My code
First of all, I just defined every pin and then
written functions
and this is functions and the main code which has problems
void GetState();
void loop() {
// put your main code here, to run repeatedly:
GetState();
PrintState();
delay(2000);
}
void PrintState()
{
Serial.println("TEMP ");
Serial.println(temp);
Serial.println("Rotobate Khak");
Serial.println(soilstate);
Serial.println("Humidity");
Serial.println(hum);
Serial.println("LDR === ");
Serial.println(LDRSTATE);
Serial.print("\n");
}
void GetState()
{
DHT.read11(Sensor);
int LDRSTATE=analogRead(LDR);
return LDRSTATE;
int soilstate=analogRead(soil);
soilstate= map(soilstate,0,1023,100.00,0);
return soilstate;
int temp=DHT.temperature;
return temp;
int hum=DHT.humidity;
return hum;
}
and I get 'temp' was not declared in this scope error
Declare "int temp;" at the top. The same for all other variables. Do not declare the variables in GetState, just use them.
Remove all "return" lines from Getstate(). Just setting "temp=DHT.temperature;" sets the variable and is enough.
Put the GetState() function before loop() (where it is called). Or, if you prefer, you can add a prototype before loop():
void GetState();
void loop() {
...
Think you are quite new to programming. Please try giving prototypes of GetState() and PrintState() functions above their definition. This is because C/C++ compiler assumes that it returns int by default if there isn't any prototype.
Otherwise, you can create a header file and then include that header file in this program.

Remove code which is commented out not actual commments [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
We have a large legacy code base with lots of code which is commented out and is now polluting our source code files. On the other hand we have real comments which I like to preserve.
Is there a way to find comments in C/C++ which are source code and remove them in order to clean up the code base?
Imagine the following code
// the function foo is doing foo
void foo(){
// bar();
bar2();
}
The old function bar() has been commented out and is no longer used. I like to have an automated way to remove the outdated source code but plain text comments should not be touched. Thus after the clean up the code would look like
// the function foo is doing foo
void foo(){
bar2();
}
I found this and that to remove all comments. This is not what I like to do.
Can clang tidy do this job?
This really scratched my mind, so,
I have written a small program in javascript real quick, which removes the lines that have // and ; in a text just to show.
You can write your own algorithm, and remove lines like that. For example, you can put # sign to the lines you would like to get deleted and then run your program with it. You need to make your own algorithm or just a simple one like mine.
Here is my js code: it deletes the rows and logs the result to the console as an array for example.
You should code a program like this or however you like.
<html>
<textarea id="txtArea" rows="40" cols="300">
// the function foo is doing foo
void foo(){
// bar();
bar2();
}
</textarea>
</html>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script>
var x = document.getElementById("txtArea").value;
var lines = x.split("\n");
var count = lines.length;
for(var i =0; i< lines.length; i++){
if(lines[i].indexOf("//")){
console.log("haha");
}else{
if(lines[i].indexOf(";")){
lines[i] = "";
//remove row
}else{
}
}
}
document.getElementById("txtArea").value = lines;
var y;
console.log(lines);
</script>

Items in QMap get lost [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a XML-file holding among other things some groups with name and userlists. In my code in constructor I have set a dictionary for this list:
dictGroups= QMap<QString, QList<QString>>() ;
In headerfile it is declared as
public:
QMap<QString, QList<QString>> dictGroups;
Then I read the file: ReadConfig();
void AppConfig::ReadConfig(void)
{
...
while(!reader.atEnd())
{
ReadGroups(reader);
if (dictGroups.isEmpty()) qDebug()<<"ReadConfig_isEmpty";
}
...
This is my ReadGroups:
void AppConfig::ReadGroups(QXmlStreamReader &reader)
{
dictGroups.clear();
while(!reader.atEnd())
{
reader.readNext();
if (reader.error())
{
...
}
else
{
if (reader.isStartElement())
{
if (reader.name().toString().toLower()=="group"){
ReadGroup(reader);
if (dictGroups.isEmpty()) qDebug()<<"ReadGroups_isEmpty";
}
}
else if (reader.isEndElement())
{
if (reader.name().toString().toLower() == "groups")
{
if(dictGroups.count()<=0){
QList<QString> users= QList<QString>();
users.append(this->GetUsername());
dictGroups.insert("admin", users);
}
return;
}
}
}
}
}
My problem is, that the items inserted in dictGroups while ReadGroups get lost. I get the debug output
ReadConfig_isEmpty
but in ReadGroups seems everything is ok.
I'm at a loss, puzzling around for hours, can anybody help to find the reason?
You have this code:
dictGroups.clear();
Why do you expect the dictGroups to persist when you clear them on every iteration of the outer loop? Don't do that.
The clear statement belongs perhaps at the beginning of ReadConfig.
Your method name capitalizations are very much out of place in Qt code, though: capitalized names are by convention reserved for groups.

How to return error code from the main function in C++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am working on an object-oriented C++ coursework where I need to return error codes from the main function. How would one do this properly?
Unfortunately this is an assessed coursework so I cannot post my code here. But let's say the case is as follows:
I'm building an enigma machine with classes Plugboard, Reflector, and Rotor. I pass each of the configuration files as arguments in the command line. In this task, I'm provided with a file errors.h containing the following:
#define INSUFFICIENT_NUMBER_OF_PARAMETERS 1
#define INVALID_INPUT_CHARACTER 2
#define INVALID_INDEX 3
// and so on...
So I have in my program several functions to check the errors, for example a function to check whether the configuration file contains an invalid character (it has to be 0 to 25). I was thinking of setting this as a boolean function and then in my main function have the following:
if (!plugboard.check_invalid_character(/*some arguments*/)) {
cerr << "Invalid character!" << endl;
return 2;
}
But I'm not completely sure this is the right way to do it? Is it too superficial? Is there a more elegant way of returning error?
I hope my question is a little clearer this time. Thanks before.
You just need to return the value 4 in your main method like this:
int main() {
return 4;
}
Please note that your main function could also have the arguments vector and the argument count so there could be more in the brackets.
If KLibby is right and you use a method with returns the value you need to use something like that:
int doSomething() {
return 4;
}
int main() {
return doSomething();
}

#if Attribute Exists in SASS [duplicate]

This question already has answers here:
Is there a SASS.js? Something like LESS.js?
(7 answers)
Apply a class AND an attribute selector
(2 answers)
Closed 7 years ago.
I'm trying to get something similar to this SASS code to work;
.alert-danger {
#include alert-variant($alert-danger-bg, $alert-danger-border, $alert-danger-text);
#include transition(ease-in-out, 95ms, all);
#if data-interaction == true {
&:hover,
&:focus {
#include alert-variant-interactive($alert-danger-bg, $alert-danger-border, $alert-danger-text);
}
}
}
I can't find a good way to check "if" some attribute or class exists within .alert-danger, though. I would really like to keep the hover nested, instead of having to write extra blocks.
Right now I am using data-interaction="true" on the .alert-danger element.
Thanks in advance!
Solution I found
Happily I did find my own solution so I am posting it here in case others are having the same issue!
The code should have looked like this, instead of what was written above:
.alert-danger {
#include alert-variant($alert-danger-bg, $alert-danger-border, $alert-danger-text);
#include transition(ease-in-out, 95ms, all);
&[data-interaction="true"] {
&:hover,
&:focus {
#include alert-variant-interactive($alert-danger-bg, $alert-danger-border, $alert-danger-text);
}
}
}
I changed the #if directive I used &[data-attribute="true"], which appends the attribute required for the nested values to compile. :)
Again, to the statements above; yes, it is a pre-processor but of course that means if CSS can do something, SASS can surely do it. All a matter of finding the right method. :)