Problem with % opertaor

This is a discussion on Problem with % opertaor within the C++ Programming forums, part of the General Programming Boards category; I'm creating a computerized version of a sort of game that maths teachers play with the class sometimes, but I ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Problem with % opertaor

    I'm creating a computerized version of a sort of game that maths teachers play with the class sometimes, but I have a prob. This section of the code checks whether 3 is a factor of the 'count' integer and whether 5 is a factor if it, but when it is executed, the program only reacts (by changing the contents of a child control) when it is a factor of 5 and not when it is a facor of 3. Whats wrong?



    Code:
                              div_test = count % 3; 
    			  
    			  if(div_test == 0)
    			  {
    				  SetDlgItemText(hDlg, IDC_RESPONSE, "Factor of three");
    			  }
    			  
    			  div_test = count % 5;
    			  
    			  if(div_test == 0)
    			  {
    				  SetDlgItemText(hDlg, IDC_RESPONSE, "Factor of five");
    			  }
    			  else
    			  {
    				  itoa(count, count_str, 10);
    			      SetDlgItemText(hDlg, IDC_RESPONSE, count_str);
    			  }
    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    the problem is that after your test for a factor of 3, you are always testing for a factor of 5 and then setting the text of the same edit box.

    so, if after your test for a factor of 3, if the contents say "Factor of three" it'll be overwritten straight away by "Factor of Five" or whatever the value of count is... you need to do something like this:

    Code:
    int div5_test = count % 5;
    int div3_test = count % 3;
    
    if(div5_test == 0 && div3 == 0)
    {
        // 3 and 5 are both factors as both the tests are zero
        SetDlgItemText(hDlg, IDC_RESPONSE, "Factor of three and factor of five");
    }
    else if(div5_test == 0)
    {
        // if not both, then if just 5 is a factor
        SetDlgItemText(hDlg, IDC_RESPONSE, "Factor of five");
    }
    else if(div3_test == 0)
    {
        // if not both then if it's just a factor of 3
        SetDlgItemText(hDlg, IDC_RESPONSE, "Factor of three");
    }
    else
    {
        // if we get here then neither of them is a factor
        SetDlgItemInt(hDlg, IDC_RESPONSE, count, false);
    }
    hope this helps!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    thanks !

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    63
    Or you could simply just add an else before the second if, and just put the condition straight in like so:

    if(count % 3 == 0)
    {
    SetDlgItemText(hDlg, IDC_RESPONSE, "Factor of three");
    }

    div_test = count % 5;

    else if(count % 3 == 0)
    {
    SetDlgItemText(hDlg, IDC_RESPONSE, "Factor of five");
    }
    else
    {
    itoa(count, count_str, 10);
    SetDlgItemText(hDlg, IDC_RESPONSE, count_str);
    }
    tudehopet uses Borland Compiler 5.5
    and Visual C++ 6.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21