Thread: Help emergency about Stack!

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Arrow Help emergency about Stack!

    What is the missing code ??


    Code:
    #include "stdafx.h"
    #include < string >
    
    
       void check()
          {
          int stackSize = input.length();      
                   
          bool isError = false;                
    
          for(int j=0; j<input.length(); j++)  
             {
             char ch = input[j];               
             switch(ch)
                {
                case '{':                      
                case '[':
                case '(':
                   push(ch);          
                   break;
    
                case '}':                      
                case ']':
                case ')':
                   if( !isEmpty() )   
                      {
                      char chx = pop();  
                      if( (ch=='}' && chx!='{') ||
                          (ch==']' && chx!='[') ||
                          (ch==')' && chx!='(') )
                         {
                         isError = true;
    					 printf("Mismatched delimeter: %c at %d",ch,j);
    					 printf("\n");
    					 
                         }
                      }
                   else                        
                      {
                      isError = true;
    				  printf("Misplaced delimeter: %c at %d",ch,j);
    				  printf("\n");
                      
                      }
                   break;
                default:    
                   break;
                }  
             }  
         
          if( !isEmpty() )
    		  printf("Missing right delimeter\n");
             
          else if( !isError )
             printf("OK!!\n");
          }  
    
       
    
    int main()
    {
       string input;
       while(true)
          {
          
    	  printf("\nEnter string containing delimiters (no whitespace): ");
    	  scanf("%s",input);         
          if( input.length() == 1 )   
             break;
                                  
          
          check();      
          }  
       return 0;
    }

  2. #2
    Registered User Rennor's Avatar
    Join Date
    Aug 2006
    Location
    Finland
    Posts
    45
    Maybe I should have voted "yes" for the code tag forcing option.

    theblasphemous, I suggest you edit your post and add code tags, it might help you to get help.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Maybe I should have voted "yes" for the code tag forcing option.
    Yes you should.
    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.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What is the missing code ??
    You mean apart from the fact that this is the C board and you posted C++ code.

    @Ken
    Thanks for the tagging, I was just going to delete the untagged code in accordance with the new policy.
    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.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    you have just messed up the code with all C and C++ code in it.

    function check dosn't know anything about input string which is declared and used in the main. either input string should be sent as an argument to the check function or could declare it as an global variable(which is not recommanded)

    Your while loop should exit condition. while(1) is not a good programming pratice.

    in the check fucntion, you have default case which is of no use.

    few errors which i found on a glance

    ssharish2005
    Last edited by ssharish2005; 08-18-2006 at 04:55 PM.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    ssharish2005, some compilers require that you provide a default clause for every switch statement; others may post a warning. In any case I consider it to be a good coding practice.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM