Help needed!

This is a discussion on Help needed! within the C++ Programming forums, part of the General Programming Boards category; I want to look in a big array for three signs in a row i use this code to do ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    Question Help needed!

    I want to look in a big array for three signs in a row
    i use this code to do it but it doesnt seems to work.
    It is supposed to jump out of the while loop when it finds "12D"
    It jumps out of the while loop as soon as it find a "D"
    Help please...

    Code:
    while (Barcode[date0] != 1 && Barcode[date1] != 2 && Barcode[date2] != 'D') 
    {
    	date0++; date1++; date2++;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    When you && things together all of them have to be true for the loop to continue. If one of the conditionals is false, then the while loop will end. So, if either

    Barcode[date0] != 1

    or

    Barcode[date1] != 2

    or

    Barcode[date2] != 'D'

    is false, then the while loop will fail.

    If you || things together, then the only way the conditional is false is if ALL of them are false.
    Last edited by 7stud; 02-01-2006 at 02:34 AM.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Also.

    What you have is a character array. What you're comparing to is integers. 1 and 2. They are characters as well, just represented in ascii value. So by the time you're finding the 'D', the other two elements I'm sure aren't ascii 1 and 2 which aren't standard characters.

    You want this:
    Code:
    while (!(Barcode[date0] == '1' && Barcode[date1] == '2' && Barcode[date2] == 'D')) 
    {
    	date0++; date1++; date2++;
    }
    
    //or if you wish: while (Barcode[date0] != '1' || Barcode[date1] != '2' || Barcode[date2] != 'D')
    Last edited by SlyMaelstrom; 02-01-2006 at 02:38 AM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. Help Please
    By Moose112 in forum C++ Programming
    Replies: 9
    Last Post: 12-10-2006, 06:16 PM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM

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