Thread: what could be wrong with this loop!?

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497

    what could be wrong with this loop!?

    well, i dont really understand , this is supposed to work! by the loops seem to not be working!

    Code:
            void CLA::machinLanguageConvertor()
            {
                    string temp = commandnameptr;
                    int argument = 0;
                    if ( commandnameptr != "not needed" )
                    {
                            argument = Atoi(offsetptr);
                            offsetptr = Oct2Bin( argument );
    
                   
                    if ( (temp != "INX") || (temp != "LAX" ) || (temp != "LDA")|| (temp != "DEX") || (temp != "CLX" ) )
                    {
                            CommandsBinaryString = BinaryDetector( commandnameptr );
                            CommandsBinaryString.append(mixstateptr);
                            CommandsBinaryString.append(offsetptr);
    
                    }
    
                    }
                    
                    
                     else  
                    {
                            CommandsBinaryString = commandnameptr;
                    }
                    
                    cout<<"\nBinary Instruction = "<<CommandsBinaryString;
    
            }
    i expect the out put as when command name ptr is INX, it should skip the offset and MIX stat ptr initialization! and just print the INX retrived binary!
    but when it comes to INX, it adds the offset to the end of the string , meaning it runs
    Code:
    Code:
      
                    if ( (temp != "INX") || (temp != "LAX" ) || (temp != "LDA")|| (temp != "DEX") || (temp != "CLX" ) )
                    {
                            CommandsBinaryString = BinaryDetector( commandnameptr );
                            CommandsBinaryString.append(mixstateptr);
                            CommandsBinaryString.append(offsetptr);
    
                    }
    
                    }
    which it should skip! what is wrong ?
    and i think it doesnt even run the first if statement! (it sikps it!)
    Last edited by Masterx; 01-31-2009 at 08:46 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Code:
     if ( (temp != "INX") || (temp != "LAX" ) || (temp != "LDA")|| (temp != "DEX") || (temp != "CLX" ) )
    is the same as
    Code:
    if( true )
    since temp can't be "INX" and "LAX" and "LDA" and ... all at the same time.

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    many thanks bro.
    i meant if one of them is wrong ! just exit!
    i should make those && ! yes?
    Last edited by Masterx; 01-31-2009 at 09:58 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Your indentation is confusing. Do you really intend that the second "if" is inside the first "if" block, and the "else" refers to the first "if"?

  5. #5
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by R.Stiltskin View Post
    Your indentation is confusing. Do you really intend that the second "if" is inside the first "if" block, and the "else" refers to the first "if"?
    well the second if is inside the first if , and the else refers to the second if !
    Code:
                   if ( temp != "not needed" )
                    {
                            argument = Atoi(offsetptr);
                            offsetptr = Oct2Bin( argument );
    
    
                            if ( (temp != "INX") && (temp != "LAX" ) && (temp != "LDX") && (temp != "DEX") && (temp != "CLX" ) )
                            {
                                    CommandsBinaryString = BinaryDetector( commandnameptr );
                                    CommandsBinaryString.append(mixstateptr);
                                    CommandsBinaryString.append(offsetptr);
    
                            }
    
    ///Error .Inx kar nemikonan.
    
                            else
                            {
                                    CommandsBinaryString = commandnameptr;
                            }
                    }
    by the way ! the first error i used to get is gone! by changing || to && . but it seems now the second else is not functioning ! how am i supposed to attach that if with this else !?
    Last edited by Masterx; 01-31-2009 at 08:42 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Masterx
    well the second if is inside the first if , and the else refers to the second if !
    The problem is that in your original code the else is associated with the first if.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    ... and if your revised code is what you intended, you can replace
    Code:
    else if ( (temp == "INX") || (temp == "LAX" ) || (temp == "LDX")|| (temp == "DEX") || (temp == "CLX" ) )
    {
    with
    Code:
    else
    {

  8. #8
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by laserlight View Post
    The problem is that in your original code the else is associated with the first if.
    tanx .
    Quote Originally Posted by R.Stiltskin View Post
    ... and if your revised code is what you intended, you can replace
    Code:
    else if ( (temp == "INX") || (temp == "LAX" ) || (temp == "LDX")|| (temp == "DEX") || (temp == "CLX" ) )
    {
    with
    Code:
    else
    {
    tanx , i did tha , is it supposed to connect the second if and the else ? !
    ( i did it . but im still getting none sense here . )
    snapshot.JPG
    ========================
    i have two kind of commands :
    1 plain commands
    2 commands stating a specific addressing mode.(complex commands)
    ========================
    i the following loop, i search for both commands, there is a difference the complex commands dont need the offset and M.I.X (which is an addressing mode indicator)
    ========================
    so , when the command is recognized! the BinaryDetector() function takes the command name , and analyzes it , and then retrieve the commands binary representation!. if it is aplain command , it would be merged with mix and offset.
    if not it should be either (INX, or DEX, or LDX, or LAX or ,CLX) . and its binary representaion should be retrieved!
    ========================

    well i just dont know why it doesnt do it right!
    ========================
    note : that first if is to eliminate declaration instrcutions, i mean because each time one string representing one instruction is taken under analysis , this is to avoid further error! because declaration are not executive commands, they just represents offsets! so the must not get into this session and accidantaly get into the memory!
    each declaration when recognized in tokenizer() ( a function before this) would contain "not needed" string as its command name and also its offset value!
    so by using this trick i can recognize it from other actual commands.
    thats the reson its up there !

    hope this intruductory helps you understand what i meant by doing such a thing

    thank you all for you time and answers.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  9. #9
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    no idea what im doing wrong?!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, you keep talking about a loop, as though you have one somewhere, and a second else, as though you have more than one.

  11. #11
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    so whats wrong with the modified one!( edited the if else ! )
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Masterx View Post
    so whats wrong with the modified one!( edited the if else ! )
    Absolutely nothing. If you're having trouble with repeated application of this function in a loop, then you probably need to look at the loop itself -- perhaps you're calling it with incorrect data, or perhaps correct data that you did not expect and therefore are not handling correctly.

  13. #13
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    are the condition checks ok ? ? what about this one ?
    Code:
            void CLA::machinLanguageConvertor()
            {
                    string temp;
                    temp = commandnameptr;
                    int argument = 0;
    
    
                    if ( temp != "not needed" )
                    {
                            argument = Atoi(offsetptr);
                            offsetptr = Oct2Bin( argument );
    
    
                            if ( (temp != "INX") && (temp != "LAX" ) && (temp != "LDX") && (temp != "DEX") && (temp != "CLX" ) )
                            {
                                    CommandsBinaryString = BinaryDetector( commandnameptr );
                                    CommandsBinaryString.append(mixstateptr);
                                    CommandsBinaryString.append(offsetptr);
    
                            }
    
    ///Error .Inx wont work.
    
                            else
                            {
                              
                                     CommandsBinaryString = commandnameptr;
                            }
                    }
                    cout<<"Binary Instruction = "<<CommandsBinaryString;
    
            }
    Last edited by Masterx; 01-31-2009 at 09:14 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This:
    Code:
    if ( (temp == "INX") || (temp == "LAX" ) || (temp == "LDX")|| (temp == "DEX") || (temp == "CLX" )
    is completely redundant, as was pointed out to you before (apparently futilely). The only possible way to get into the else is for temp to equal one of those five strings. Therefore the check must be true, so don't do it.

    The only problem here is when temp does equal "not needed", as then the last cout will happen and Binary Instruction = garbage will get printed out.

  15. #15
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    Quote Originally Posted by tabstop View Post
    This:
    Code:
    if ( (temp == "INX") || (temp == "LAX" ) || (temp == "LDX")|| (temp == "DEX") || (temp == "CLX" )
    is completely redundant, as was pointed out to you before (apparently futilely). The only possible way to get into the else is for temp to equal one of those five strings. Therefore the check must be true, so don't do it.

    The only problem here is when temp does equal "not needed", as then the last cout will happen and Binary Instruction = garbage will get printed out.
    i dont underrstand ! is it not a (OR) ? and when i use OR if one of them appears there ( is recognized) it should enter the block, if none of them is recognized , so wont enter the block!
    i meant it by using or, those for are complex commands, and need to only contain command , no offset and mix. as you've mentioned !
    The only possible way to get into the else is for temp to equal one of those five strings.
    and i want this to happen!
    and about Binary instruction ! :Binary instruction is initialized in the upper function! it is printed there !still you think the "not needed" is the problem /?
    Last edited by Masterx; 01-31-2009 at 09:15 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  4. What is wrong with my while loop?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-19-2002, 12:07 PM
  5. Whats wrong w/ my loop?? Please help?
    By aspand in forum C Programming
    Replies: 6
    Last Post: 05-30-2002, 03:42 AM