Thread: Problem with If, &&, ||

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    Problem with If, &&, ||

    Hi Guys,

    I currently have the below code:

    PHP Code:
    while( (getchar()) != EOF)               
            if ( 
    != -38 && ('0' || '3') )
                return 
    false
    I would only like "false" to be returned IF c DOES NOT equal 0,1,2,3 or -38. However when c becomes equal to -38, "false" is being returned...

    Can anyone see my error here?

    Thank you for your help!!

    Kind Regards,

    Giri

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It looks "correct" as far as it goes, but how exactly are you expecting c to become equal to -38?

    Remember that c should be declared as an int so that the EOF returned by getchar will be "out of band" (not a representable char). If you've properly declared c as an int then c will never be negative, except when it's EOF. So it should never be -38.

    What exactly are you trying to do there?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Hi Oogabooga,

    Thanks for that. I realized my mistake - I should have been putting '10' instead of '-38'. I initially put -38 because I noticed that -38 was being printed when a NewLine was encountered.

    However, I am now experiencing the problem that after c = getchar() reads a NewLine and then returns to the line,

    PHP Code:
     while( (getchar()) != EOF
    nothing happens...the loop just appears to freeze...

    What I am trying to do is read individual digits from input in the following form:

    022302120222
    222223111032

    Should I perhaps start a new thread about this issue?

    Kind Regards,

    Giri

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Giri View Post
    PHP Code:
     while( (getchar()) != EOF
    nothing happens...the loop just appears to freeze...
    Are you actually entering EOF?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Hi Quzah,

    I wouldn't have thought so, because the loop freezes after reading the first line of input. It doesn't seem to get to the 2nd line of input.

    Kind Regards,

    Giri

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You'll need to show a little more code then.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Ok.

    This is what I have written so far.

    Code:
    #define MAX_X_DIM 31
    #define MAX_Y_DIM 41
    int array[MAX_Y_DIM][MAX_X_DIM];
    
    bool get_input(void) {
        int c;
        int column_counter = 0;
        int row_counter = 0;
    
        while( (c = getchar()) != EOF){               
            if ( c != 10 && (c < '0' || c > '3') )
        		return false;
    
            if (column_counter > MAX_X_DIM)
            	return false;
    
            if(c != 10){
            	array[row_counter][column_counter] = c - 48;
            	++column_counter;
            	}
            else if(c == 10){
            	++row_counter;
            	column_counter = 0;
            }
    
            if (row_counter > MAX_Y_DIM)
            	return false;
         }
    
             for (int i = 0; i < row_counter; ++i)
                  for (int j = 0; j < column_counter; ++j)
                  		printf("%d",array[i][j]);
    
    
    
        return true;
    
    
      }
    Thanks for your help guys.

    Kind Regards,

    Giri

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 10:40 PM