what could be wrong with this loop!?

This is a discussion on what could be wrong with this loop!? within the C++ Programming forums, part of the General Programming Boards category; well, i dont really understand , this is supposed to work! by the loops seem to not be working! Code: ...

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

    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 07: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
    595
    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
    493
    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 08: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
    595
    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
    493
    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 07: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
    21,636
    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.
    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

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    ... 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
    493
    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
    493
    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,185
    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
    493
    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,185
    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
    493
    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 08: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,185
    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
    493
    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 08: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


Page 1 of 2 12 LastLast
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, 04: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

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