home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18372 < prev    next >
Encoding:
Internet Message Format  |  1992-12-15  |  2.3 KB

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!spool.mu.edu!uwm.edu!csd4.csd.uwm.edu!eli
  2. From: eli@csd4.csd.uwm.edu (Elihu Lubkin)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problems with Case statements HELP!
  5. Date: 15 Dec 1992 06:28:09 GMT
  6. Organization: University of Wisconsin - Milwaukee
  7. Lines: 53
  8. Distribution: usa
  9. Message-ID: <1gjttpINNobb@uwm.edu>
  10. References: <Bz18KA.MCv@access.digex.com>
  11. NNTP-Posting-Host: 129.89.7.4
  12.  
  13. jhurwitz@access.digex.com (j. hurwitz) writes:
  14. : Okay, I'm new to C so you'll have to forgive my lame question. Anyhoot,
  15. : I'm trying to write a program that will check for balanced braces in my
  16. : C source code.: But, when I try this:
  17. : ============================== Begin Hack =========================
  18. : //programm to read a C source file and verify # of left to right braces
  19. : #include <stdio.h>
  20. : int main(int argc, char *argv[])
  21. : {
  22. :    FILE *fptr;
  23. :    int l=0, r=0;
  24. :    char ch;
  25. :    fptr  = fopen("brachek3.c","r");
  26. :    while ((ch =getc(fptr)) !=EOF)      
  27. :    {
  28. :    switch (ch)
  29. :      {
  30. :      case '{':  l++;
  31.                   break;     <====otherwise { increments l & r; } only r. 
  32. :      case '}':  r++;                       --Thelma
  33.  
  34. :      }
  35. :    }
  36. :    printf("\nl= %d r= %d", l, r);
  37. :    if (l != r)
  38. :    printf("\nBraces do not match\n");
  39. :    else printf("\nBrace match !!!\n");
  40. :    fclose(fptr);
  41. : }
  42. : ================================ End Hack ========================
  43. : I get an uneven number of braces at the end. I can't figure out what
  44. : I am doing wrong. I'm sure it is a minor oversight on my part. Any help
  45. : would be greatly appreciated
  46. :  
  47. :           __/ /          .                     +             .
  48. :         _/__/_/\                                
  49. :       _/____/ \/  J. Hurwitz                                          ,
  50. :   .  /     / \/|          .          *                   
  51. :     /     /  /|/  The MIDI master             .                  o
  52. :    /_____/  / |                                         
  53. :   //____/\\/ /  Roaming Cyberspace          "Space is the place"        
  54. :   /  o  \ ||/    .                     
  55. :  | o   o |//                                .          -Sun Ra      +
  56. :   \o   o//     jhurwitz@access.digex.com          
  57. :    --^--   .                                            *        
  58. : +                         '               +                          
  59.