#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2

    Checking if the character stored in an array is '{'


    I am trying to check whether the value returned from an array is '{'. But seems like my compiler treats it as if it's being used like the '{' character in the following example:
    Code:
    for (i = 0; i < n; i++) {
    So it's showing error and can't compile. What's the solution?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by arman.khandaker
    I am trying to check whether the value returned from an array is '{'. But seems like my compiler treats it as if it's being used like the '{' character in the following example:
    Code:
    for (i = 0; i < n; i++) {
    So it's showing error and can't compile. What's the solution?
    seems simple enough, assuming this is C.

    Code:
    for( i = 0; i < n; i++)
    {
        if( array[i] == '{' )
        {
             printf("found");
        }
    }
    This could be an issue with your compiler if the error persists.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by Kamikageyami
    seems simple enough, assuming this is C.

    Code:
    for( i = 0; i < n; i++)
    {
        if( array[i] == '{' )
        {
             printf("found");
        }
    }
    This could be an issue with your compiler if the error persists.
    That's not what I meant. Check the screenshot I attached. The '{' is not being treated like a character inside the array.
    Attached Images
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by arman.khandaker
    That's not what I meant. Check the screenshot I attached. The '{' is not being treated like a character inside the array.
    Ahhh, my mistake - sorry.

    Have you tried using an escape character? Like '\{' or '\\{' ?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    You need to show more code.

    How is c defined?

    What is it's value?

    What is the type and value that is being returned by pop()?

    Jim
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by jimblumberg
    You need to show more code.

    How is c defined?

    What is it's value?

    What is the type and value that is being returned by pop()?

    Jim
    Okay, here's the entire code:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    #define STACK_SIZE 100
    
    char contents[STACK_SIZE];
    int top = 0;
    
    void make_empty(void)
    {
    	top = 0;
    }
    
    bool is_empty(void)
    {
    	return top == 0;
    }
    
    bool is_full(void)
    {
    	return top == STACK_SIZE;
    }
    
    void push(char c)
    {
    	if (is_full())
    		printf("Stack Overflow");
    	else
    		contents[top++] = c;
    }
    
    char pop(void)
    {
    	if (is_empty())
    		printf("Stack Underflow");
    	else
    		return contents[--top];
    }
    
    int main(void)
    {
    	char c;
    	
    	printf("Enter parentheses and/or braces: ");
    	while ((c = getchar()) != '\n') {
    		if (c == '(' || c == '{')
    			push(c);
    		if (c == ')') {
    			if (pop() != '(') {
    				printf("The parentheses/braces are not nested properly");
    			}
    		}
    		if (c == '}') {
    			if (pop() != '{') {
    				printf("The parentheses/braces are not nested properly");
    			}
    		}
    	if (top != 0)
    		printf("The parentheses/braces are not nested properly");
    	else if (top == 0)
    		printf("The parentheses/braces are nested properly");
    	
    	return 0;		
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by Kamikageyami
    Ahhh, my mistake - sorry.

    Have you tried using an escape character? Like '\{' or '\\{' ?
    Yes, I tried. Doesn't work.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Perhaps using a more consistent indentation style would help. Look at your code formatted properly:

    Code:
    int main(void)
    {
       char c;
    
       printf("Enter parentheses and/or braces: ");
       while ((c = getchar()) != '\n') {
          if (c == '(' || c == '{')
             push(c);
          if (c == ')') {
             if (pop() != '(') {
                printf("The parentheses/braces are not nested properly");
             }
          }
          if (c == '}') {
             if (pop() != '{') {
                printf("The parentheses/braces are not nested properly");
             }
          }
          if (top != 0)
             printf("The parentheses/braces are not nested properly");
          else if (top == 0)
             printf("The parentheses/braces are nested properly");
    
          return 0;
       }
    Notice how that last brace isn't flush left? That indicates a problem.

    Jim

    Comments on this post

    • arman.khandaker agrees
  16. #9
  17. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    Actually, the picture looked more like a bug in the IDE not matching braces properly.

    I guess it didn't help matters in that there was an actual missing brace somewhere else as well.
    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
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by jimblumberg
    Perhaps using a more consistent indentation style would help. Look at your code formatted properly:

    Code:
    int main(void)
    {
       char c;
    
       printf("Enter parentheses and/or braces: ");
       while ((c = getchar()) != '\n') {
          if (c == '(' || c == '{')
             push(c);
          if (c == ')') {
             if (pop() != '(') {
                printf("The parentheses/braces are not nested properly");
             }
          }
          if (c == '}') {
             if (pop() != '{') {
                printf("The parentheses/braces are not nested properly");
             }
          }
          if (top != 0)
             printf("The parentheses/braces are not nested properly");
          else if (top == 0)
             printf("The parentheses/braces are nested properly");
    
          return 0;
       }
    Notice how that last brace isn't flush left? That indicates a problem.

    Jim
    I used proper indentation; unfortunately, it doesn't seem to work either.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by salem
    Actually, the picture looked more like a bug in the IDE not matching braces properly.

    I guess it didn't help matters in that there was an actual missing brace somewhere else as well.
    Yes, that's what I have been thinking, it must be an IDE bug. I am using Dev-C++. Which IDE should I try? And even if this is an IDE bug, the compiler should still work, right? Apparently, it doesn't! :(
  22. #12
  23. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    Perhaps you should explain where you think your while loop should end.

    The code that jimblumberg posted is missing a brace for sure (despite what you say).
    You need to decide where to place an additional }

    Comments on this post

    • arman.khandaker agrees
    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
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Originally Posted by salem
    Perhaps you should explain where you think your while loop should end.

    The code that jimblumberg posted is missing a brace for sure (despite what you say).
    You need to decide where to place an additional }
    Ahh you guys are right, I had a missing brace. It totally escaped my eyes. Thanks a lot, the code can be compiled now. :)
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    2
    Just to update you guys, this is my final code. Which is working just as I expected:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    #define STACK_SIZE 100
    
    char contents[STACK_SIZE];
    int top = 0;
    
    void make_empty(void)
    {
    	top = 0;
    }
    
    bool is_empty(void)
    {
    	return top == 0;
    }
    
    bool is_full(void)
    {
    	return top == STACK_SIZE;
    }
    
    void push(char c)
    {
    	if (is_full()) {
    		printf("Stack Overflow");
    		exit(0);
    	}
    	else
    		contents[top++] = c;
    }
    
    char pop(void)
    {
    	if (is_empty()) {
    		printf("Stack Underflow");
    		exit(0);
    	}
    	else
    		return contents[--top];
    }
    
    int main(void)
    {
       char c;
    
       printf("Enter parentheses and/or braces: ");
       while ((c = getchar()) != '\n') {
          if (c == '(' || c == '{')
             push(c);
          if (c == ')') {
             if (pop() != '(') {
                printf("The parentheses/braces are not nested properly");
             }
          }
          if (c == '}') {
             if (pop() != '{') {
                printf("The parentheses/braces are not nested properly");
             }
          }
      }
          
          if (top != 0)
             printf("The parentheses/braces are not nested properly");
          else if (top == 0)
             printf("The parentheses/braces are nested properly");
    
          return 0;
    }
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0
    Originally Posted by arman.khandaker
    Yes, that's what I have been thinking, it must be an IDE bug. I am using Dev-C++. Which IDE should I try? And even if this is an IDE bug, the compiler should still work, right? Apparently, it doesn't! :(
    The compiler that comes with Dev-C++ is anceint and should not be used. It could be updated, but perhaps you're better off with a modern, maintained IDE like eclipse or Code::Blocks.

IMN logo majestic logo threadwatch logo seochat tools logo