kinda stuck.. help please?

This is a discussion on kinda stuck.. help please? within the C Programming forums, part of the General Programming Boards category; I'm a little stuck and having late night brain farts. I have two variables. I want it so that if ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    15

    kinda stuck.. help please?

    I'm a little stuck and having late night brain farts.

    I have two variables. I want it so that if either variable is true, then a third variable becomes true. Then I want to return the third variable to false if both 1 and 2 variables are false. However, I want the third variable to function independently as well. How can I code that?

    This is what I got so far:
    Code:
    if (variable1 || variable2)
    {
    variable3 =1 ;
    }
    however this latches variable3 to true even when I return variable1 and variable2 to False. And I still want variable3 to be able to work independantly from the other two variables.

    Any suggestions??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Would the solution you are looking for be:
    Code:
    variable3 = variable1 || variable2;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    15
    awesome... thx alot. appreciate it

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    15
    ok, the above didn't work out so well. I currently have:

    Code:
    variable3 = (variable1 || variable2);
    However, this doesn't allow variable3 to function indepently since if both variable1 and variable2 are = 0 then variable3 won't go to 1 on it's own.

    any suggestions?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by w0nger
    However, this doesn't allow variable3 to function indepently since if both variable1 and variable2 are = 0 then variable3 won't go to 1 on it's own.
    What do you mean by "function independently"? You stated that "I want to return the third variable to false if both 1 and 2 variables are false". So, if both variable1 and variable2 are false, you want variable3 to be false, and if both variable1 and variable2 are false you want variable3 to be true? That is a contradiction.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    15
    i'd like that variable3 be able to function indepently of variable1 and 2 in a different part of the program.

    I have another section of code that calls for variable3 to become true, however because of the above code that links it to variable1 and variable2, that part of the code doesn't function.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    Seems to me like you need to think about what you're doing.

    Like have a 4th variable for instance.

    You're not writing a program with masses of globals are you, and attempting to "re-use" a variable?

    Posting actual code, rather than vague descriptions and one-liners would help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    15
    i'm kind looking for the equivalent of a NOR logic function. But have no idea how to do that...

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    As in
    Code:
    if ( !( a || b) ) {
    }
    http://en.wikipedia.org/wiki/NOR_gate

    Or what matsp wrote
    Code:
    variable3 = ! ( variable1 || variable2 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    15
    Code:
    if ((a | b) && !(c))
    c = (a |b)
    that's what i'm trying... i'll let you guys know... thx so far

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    15
    ok, that didn't work... here's what I have:

    Code:
    c = (a | b);
    but, because c is used in other parts of the program (of which I don't have knowledge of), I need c to be able to change values indepently of (a | b). How do I get it to ignore the statement if a and b remain at 0.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, can you make a truth table of the differnet combinations of a, b and c, something like this:
    Code:
    a   b   c
    0   0   0
    1   0   1
    0   1   1
    1   1   0
    The above is for XOR, but if you describe what you want in that way, then we can probably give you an answer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    Now are you using | because you're too lazy to write ||, or is something else going on here?

    | and || are very different animals in C, and casual use of them will just lead you astray.

    if ( a || b ) c = 1;
    Strange, this is where you started.


    > however this latches variable3 to true even when I return variable1 and variable2 to False
    This however is wrong. Something else is going on in your program, which has nothing to do with the if statement.
    If both a and b are FALSE, nothing happens to c
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    15
    a b c
    1 1 1
    1 0 1
    0 1 1
    0 0 0
    0 0 1 <--- must also be able to occur

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can not have two different results for the same input. Are you saying this "0 0 no change"? So when is c set to zero?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 11:40 AM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM

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