I am reading a data set where each data line contains a value that I want to read over and then ignore.

So, I create three variables as:

int number1, number3;
float number2;

and read them in. I use the data in number1 and number3, but never use number2 (other than to be able to skip over it).

When I compile the program I get a warning that states that the number2 variable is set but not used.

This, of course, is not a problem in that my code compiles and runs just fine. But, in the spirit of cleaning up my code to eliminate warnings, what is the best practice for doing so in this situation?

I suppose I could add a line that states number2+=0; That would make the warning go away, and would not actually change the value of number2 in case I want to use it latter. But it adds an unnecessary calculation. So, while that eliminates the warning, it also does not seem like the best way to do it.

Is there a better suggestion?

Can you post the code that is giving you the warning? If you never use number2 then you should be able to just get rid of it.

Here is the relevant code:

  int ml, bl; // move and base number of associated landmarks
  MYgetline(MoveClusterFile,moveline); //skip move file header
  MYgetline(BaseClusterFile,baseline); //skip base file header
  writeCSVheader(OutputFile,"AV", "T"); //write header to output file

  //cycle through the data
  while (MYgetline(MoveClusterFile,moveline))
   {
    if (!MYgetline(BaseClusterFile,baseline))
     {
      Log.report(" ERROR: Move and Base files not compatible for joint: ");
      Log.report(movecluster);
      Log.report(basecluster);
      Log.report(" \n");
      Log.report("   One of these files is not available.\n");
      Log.ProgramExit();
     }
    mf=comma_read(skip,moveline);
    bf=comma_read(skip,baseline);
    ml=comma_read(skip,moveline); ml+=0;
    bl=comma_read(skip,baseline);
    if (mf!=bf)
     {
      Log.report(" ERROR: Move and Base files not compatible for joint: ");
      Log.report(movecluster);
      Log.report(basecluster);
      Log.report(" \n");
      Log.report("   The frame numbers in these files do not match up.\n");
      Log.ProgramExit();
     }
    Ma(0)=comma_read(skip,moveline);    Ba(0)=comma_read(skip,baseline);
    Ma(1)=comma_read(skip,moveline);    Ba(1)=comma_read(skip,baseline);
    Ma(2)=comma_read(skip,moveline);    Ba(2)=comma_read(skip,baseline);
    Mt(0)=comma_read(skip,moveline);    Bt(0)=comma_read(skip,baseline);
    Mt(1)=comma_read(skip,moveline);    Bt(1)=comma_read(skip,baseline);
    Mt(2)=comma_read(skip,moveline);    Bt(2)=comma_read(skip,baseline);
    if (!Ma.isMissing() && !Ba.isMissing())
     {
      OutputFile << mf << ",";
      Matrix Out=Ba.toMatrix("Degrees").make4x4(Bt).inverse()*Ma.toMatrix("Degrees").make4x4(Mt);
      Out.inner().toAttitude("Degrees").dump(OutputFile);
      OutputFile << ",";
      Out.RecoverTranslation(Pre).dump(OutputFile);
      OutputFile << endl;
     }
   } // Cycle through the data
  OutputFile.close();
  MoveClusterFile.close();
  BaseClusterFile.close();
 } // End MoveJoint Function

int bl and ml are variables that are set but not used. In my query, I tried to give a more generic example so as to not complicate the issue with my comma_read function.

The comma_read function reads numbers from a string where then numbers are separated by commas. The target number to be returned by the function is based on the "skip" comma requested. The function then updates the value of "skip" so that the next value can be read from the string. So, if x=1, comma_read(x, dataline) would return the number located after the first comma in the dataline string and update the value of x to 2. Keeping track of the updated value of the x index can be important when starting the data read process from a spot in the middle of the string.

That is why I need to read past the unused variables. In the example above, the next important variable reads the next value in the string, so I could just do skip++ instead of comma_read(skip, ml); But, that is not always the case, so that obvious solution will not always work.

This is a common compiler issue with new programs. If you intend to use that variable in the future, then just comment it out for now if you want to make the compiler to shut up. :-)

Edited 1 Year Ago by rubberman

This article has been dead for over six months. Start a new discussion instead.