Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The last statement of this if statement isn't making sense to me or does it?

   // Check arguments
   if ( _argc > 1 )
   {
      if ( !_stricmp(_argv[1], "icmp") )
{ //<-- Need to add brackets for more than one statement
   bShowTCP = FALSE;
   bShowUDP = FALSE;
}
else if ( !_stricmp(_argv[1], "tcp") )
{
   bShowUDP = FALSE;
   bShowICMP = FALSE;
}
else if ( !_stricmp(_argv[1], "udp") )
{
   bShowTCP = FALSE;
   bShowICMP = FALSE;
}
else
{
   bShowUDP = TRUE; //<-- Needs to be within else statement brackets
   printf( "\nUsage lsniff [ICMP|TCP|UDP]\n" );
   exit(0); //<-- FYI... this ends execution of the program

}
   }
Posted

It does make sense if your input value is not one of icmp,tcp or udp.
The developer of the program wants the program to exit if none of the above three types have been entered in the argument.
 
Share this answer
 
Comments
Member 7766180 22-Jul-11 15:09pm    
So if I remove this statement, I should be OK? It's always going to be one of these three until I expand the range of protocols.
Abhinav S 23-Jul-11 0:14am    
NO dont remove this statement. Let it be there.
No harm incase the user something other than what you want them to input.
Albert Holguin 23-Jul-11 13:46pm    
the last printf() is purely informative for a user. if they enter an invalid input, then it tells them what options they have.
I see nothing wrong. If you're wondering why it's !_stricmp it's because C string comparisons return 0 if the strings are the same, and !0 is a true value.

I'm not sure why they bother to set bShowUDP in the else though.
 
Share this answer
 
Comments
Member 7766180 22-Jul-11 15:08pm    
bShowUDP, Thats what is throwing me off! Why that? Should I erase it?
lewax00 22-Jul-11 15:27pm    
You could, there's no chance for anything else to use it before it closes.
I don't see anything wrong with the code. I believe you are expecting an expression next to the else clause as in the preceding else if clauses. The else clause does not take any expression and is executed if no other if clause in the statement is executed.
 
Share this answer
 
How are bShowTCP, bShowUDP and bShowICMP initialized before that code?

Also the case where no argument is provided also need to be handled.

The following code would be an improvement. First, it make more sense to initialize bShowSomething to FALSE and have to update a single flag in each condition. IKt is much more maintanable when no protocole are added as you won't have to update existing code to disable the new protocole everywhere but you will enable it at a single place in the newly added condition.

C++
bool bShowTCP = FALSE;
bool bShowUDP = FALSE;
bool bShowICMP = FALSE;
bool bShowUsage = FALSE;

if ( _argc > 1 )
{
  if ( !_stricmp(_argv[1], "icmp") )
  { //<-- Need to add brackets for more than one statement
    bShowICMP = TRUE;
  }
  else if ( !_stricmp(_argv[1], "tcp") )
  {
    bShowTCP = TRUE;
  }
  else if ( !_stricmp(_argv[1], "udp") )
  {
    bShowUDP = TRUE;
  }
  else
  {
    // Unkwnown protocol... 
    bShowUsage = TRUE;
  }
}
else
{
  // No protocole specified
  bShowUsage = TRUE;
}

if (bShowUsage)
{
  printf( "\nUsage lsniff [ICMP|TCP|UDP]\n" );
  exit(0); //<-- FYI... this ends execution of the program
}
 
Share this answer
 
could you replace 'if、else if' to 'switch 、case' key words?


this is my idea!
 
Share this answer
 
v2
Comments
Philippe Mori 23-Jul-11 9:49am    
This is C++ not C#.
Albert Holguin 23-Jul-11 13:47pm    
switch/case statements are C++, his solution is not valid anyway, but neither is your comment.
Philippe Mori 23-Jul-11 13:54pm    
Contrary to C#, switch/case in C++ does not works on strings.
Albert Holguin 23-Jul-11 14:10pm    
I see what you mean...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900