So what am i doing wrong now? (RichEdit problem in C++ builder)

This is a discussion on So what am i doing wrong now? (RichEdit problem in C++ builder) within the C++ Programming forums, part of the General Programming Boards category; Ok, its fairly simple: Condition 1 + Condition 2 = add the lines "hello world" in richedit. But its not ...

  1. #1
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41

    So what am i doing wrong now? (RichEdit problem in C++ builder)

    Ok, its fairly simple: Condition 1 + Condition 2 = add the lines "hello world" in richedit. But its not working
    (i have 4 conditions in one button click)

    Should sound simple but its giving me headackes for example. (wonder what im doing wrong here)

    Code:
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    if(CheckBox1->Checked==true & CheckBox2->Checked==true);
    {
    RichEdit1->Lines->Add("hello world");
    }
    
    if(CheckBox3->Checked==false & CheckBox4->Checked==false);
    {
    RichEdit1->Lines->Add("goodbye world");
    }
    }
    Gives hello world
    goodbye world

    on the same buttonclick , it totally disregards that its supposed to be bounded to the if statement and the given conditions,

    Clicking on the button should give only: Hello World or Goodbye world not both

    what gives? I mean i used checkboxes, and they seemed to work perfectly fine when the conditions where given, when you use it for adding lines in richedit the whole thing goes haywire making me wonder what im doing wrong?

    The thing should only add lines when conditions are being met, why isn't it doing so?
    Last edited by darketernal; 06-15-2007 at 03:27 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    & is a bitwise and, but you want a logical and. Can you figure it out from there?

  3. #3
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    That doesn't work.

    using && as the logical and as you describe it , gives the same problem.

    Its not nice, i need assistance on this one.
    Last edited by darketernal; 06-15-2007 at 04:55 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    What states are the four checkboxes in?

  5. #5
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    all are false by default.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    So when all are false it outputs "hello world"?

    >> Clicking on the button should give only: Hello World or Goodbye world not both
    Your code would give both if boxes 1 & 2 are checked and 3 & 4 are not checked.

  7. #7
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    yep that's the problem , its giving both indiscriminatly.
    Have a look what happens if i press the button.

    http://blogphotohost.com/show.php?id...f7858B56d40C8c

    I changed the link, hopefully it will show up now.
    Last edited by darketernal; 06-15-2007 at 06:15 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    "Unauthorized referer" when I click that link.

    Check is a property, not a function, right?

    You changed it to &&, right?

  9. #9
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    Regardless on wether if i use & or && it doesn't work,

    what i noticed tho is that i should remove the ; behind my if statement. then it works for the following. http://blogphotohost.com/show.php?id...5eBfbcS7f78332

    but if i try this same thing with my RichEdit i get the goodbye hello world mumbo jumbo again =\
    Last edited by darketernal; 06-15-2007 at 06:18 PM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> Regardless on wether if i use & or && it doesn't work
    That's not the point. The point is you have to use && or it will never work. If you're trying to get it to work with & then you are wasting time. Make sure you are using &&, then continue on to see what else needs to be fixed.

    >> what i noticed tho is that i should remove the ; behind my if statement. then it works for the following.
    I didn't notice those. You must remove the semi-colons or it will never work. The semi-colons end the if statement, so the block of code after it will always be run.

    >> but if i try this same thing with my RichEdit i get the goodbye hello world mumbo jumbo again =\
    Please copy and paste the code (with the two fixes above) that still causes the problem.

    BTW, your new picture is too small to see anything.

  11. #11
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    Amen i will go along with using && , i have absolutely no problems with that as long as it will get my program to work. I removed the semi colons, and i re-hosted the pictures, hopefully you can view them now.

  12. #12
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    My code as now would be this.

    Code:
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    
    if(CheckBox1->Checked==true && CheckBox2->Checked==true)
    {
     RichEdit1->Lines->Add("hello world");
    }
    
    if(CheckBox3->Checked==false && CheckBox4->Checked==false)
    {
     RichEdit1->Lines->Add("goodbye world");
    }
    it gives

    Hello world
    goodbye world

    as it ignores my if statements

  13. #13
    Registered User darketernal's Avatar
    Join Date
    Sep 2001
    Posts
    41
    urgh, im seeing whats going wrong now, in this example.

    of course if id checked 1 and 2 in order to get hello world,

    i didn't checked 3 + 4 , so in other words the false statement is true and therefore goodbye world is printed also.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    You can also use else if if you always want good bye to be ignored when hello is printed. Glad it's working better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-03-2008, 01:21 PM
  2. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  3. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  4. strange problem, not sure what is wrong
    By Shadow12345 in forum C++ Programming
    Replies: 7
    Last Post: 07-23-2002, 11:46 PM
  5. Can't get program to work
    By theirishrover12 in forum C Programming
    Replies: 1
    Last Post: 06-08-2002, 11:10 AM

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