Thread: Multiple if statements and conditions

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The sequence will never run if a is not 0.

    Code:
    if (a == 0)
     {  //handle this error here }
    
    if (b == 0)
     { // handle this error here }
    
    if (c == 0)
      { // handle this one in here }
    It's the else clauses you are are tripping over....

  2. #17
    Registered User
    Join Date
    Nov 2010
    Posts
    26
    The problem is I need certain clauses for if a and b = 0 and then a,b and c = 0 etc which would mean repeating myself multiple times in those 3 if statements.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tomeatworld View Post
    The problem is I need certain clauses for if a and b = 0 and then a,b and c = 0 etc which would mean repeating myself multiple times in those 3 if statements.
    And that's a problem because?

    I've written code with some pretty wild conditions and in my experience repetition is generally more reliable than even the most clever nesting....

    Lets assume you need 2 non-zero values to make your proggy work...
    Code:
    int zcount = 0;
    
    if (a == 0)
     { zcount++; }
    
    if (b == 0)
     { zcount ++; }
    
    if (c == 0)
     { zcount++; }
    
    if (zcount > 1)
     { printf("too many zeros\n");
        exit(1); }
    There is considerable advantage in dealing with things one small step at a time and keeping things as simple as possible.


    You might like to try making sense of this...
    Code:
    // remote launch a file by association
    BOOL NetworkLaunchFile(SOCKADDR Client,PWCHAR FileName)
      { TCHAR     ss[MAX_PATH]          = {0};    // scrap string
        TCHAR     ext[MAX_TYPENAME]     = {0};    // file type name
        TCHAR     pgm[MAX_PROGRAMNAME]  = {0};    // expected pgrogram
        SLOT      pgs;                            // program slot number
        TYPEINFO  ti;                             // appoved type info
        // no launches in lockdown
        if (ServerLocked)
          { SendDatagram(Client,RM_STOP,NULL,0);
            return 0; }
        // get information about the file  
        PathGetTypeName(FileName,ext);
        if (! LoadTypeInfo(ext,&ti))
          { SendDatagram(Client,RM_STOP,NULL,0);
            LogPacket(Client,FileName);
            LogPacket(Client,L"File type not approved");
            return 0; }
        // guard against dogpiles
        if (RemLaunch)
          { SendDatagram(Client,RM_BUSY,&Setting.TimeOut,sizeof(BYTE));
            return 0; }
        // is the program already running
        _wsplitpath(ti.Program,NULL,NULL,pgm,NULL);
        pgs = TrackerGetProgramSlot(pgm);
        // program is not running
        if ((pgs == 0) && (ServerBusy > 0))  //busy by local program 
          { SendDatagram(Client,RM_BUSY,&Setting.TimeOut,sizeof(BYTE)); 
            return 0; }
        // program is running
        if (pgs > 0)
          { if ((wcsicmp(Programs[pgs]->Remote,ti.Remote) != 0) ||
                  !Programs[pgs]->AddFiles || Setting.NoAdd ) 
              { SendDatagram(Client,RM_BUSY,&Setting.TimeOut,sizeof(BYTE));
                return 0; } } 
        // launch the file
        LogPacket(Client,FileName);
        RemLaunch = 1;
        // prepare program name
        PathQuoteSpaces(ti.Program);
        // complete command line
        lstrcat(ti.Flags,L" ");
        lstrcat(ti.Flags,FileName);
        if (ShellExecute(NULL,L"Open",ti.Program,ti.Flags,NULL,SW_SHOW) < (HINSTANCE) 33)
          { SendDatagram(Client,RM_STOP,NULL,0);
            AddToLog(L"Error",L"Can't launch file");
            RemLaunch = 0;
            return 0; }
        // launch not expected
        if (PgmHasRemote(pgs))
          { SendDatagram(Client,RM_NOREMOTE,NULL,0);
            RemLaunch = 0;
            return 1; }
        // passed to client
        RemOwner = Client;
        SetTimer(MainWind,LaunchTimer,Setting.TimeOut * 1000,&TrackerNoLaunch);
        return 1; }
    All that is one giant test as to whether a file can be and was launched in windows or not.
    Last edited by CommonTater; 11-06-2010 at 10:50 AM.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The problem is I need certain clauses for if a and b = 0 and then a,b and c = 0 etc which would mean repeating myself multiple times in those 3 if statements.
    As Tater said it is better to have a program producing the correct output with some redundant code, than a program that has no redundant code but does not produce the desired output.

    Once you are sure your program works then you can try to slowly remove the redundant code.

    Jim

  5. #20
    Registered User
    Join Date
    Nov 2010
    Posts
    26
    I found my problem. I had a statement for finding the determinant before these if statements and of course the determinant was 0 if b and a or c were 0 so it was having problems. Fixed now! Thanks a load for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on the homework (if statements)
    By Neotriz in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2009, 12:55 AM
  2. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  3. tips for testing conditions
    By Silvercord in forum Game Programming
    Replies: 12
    Last Post: 04-10-2003, 01:42 PM
  4. multi conditional if statements?
    By chaser in forum Game Programming
    Replies: 3
    Last Post: 07-26-2002, 10:52 PM
  5. Winning conditions (Connect 4)
    By Dual-Catfish in forum Game Programming
    Replies: 3
    Last Post: 12-15-2001, 01:53 PM