Thread: Probrem With Switch (Another Easy Task):

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Unhappy Probrem With Switch (Another Easy Task):

    Oki I'm trying to lean C so don't blame on me, I'm having problems with this switch, the program itself is useless I'm just trying to learn the language syntax, here goes the code:

    Code:
    #include<stdio.h>
    
    //Dumb Program to Count Digits
    int main(){
    	int c, i, nblanks, nothers, ndigit[10];
    	
    	nblanks = nothers = 0;
    	 for (i = 0;i < 9;i++)
    		ndigit[i] = 0;
    	 while ((c=getchar())!= EOF)
    	 {
    	 switch(c){
    		case:'0':case:'1':case:'2':case:'3':case:'4':
    		case:'5':case:'6':case: '7':case:'8':case:'9':
    		ndigit[c-'0']++;
    		break;
    		
    	    case ' ':
    	    case '\n':
    		case '\t':
    		 nblanks++;
    		break;
    
    		default:
    		 nothers++;
    		break;
    		}
    	 }//switch
    	 printf("Digits = ");
    	 	for (i = 0; i < 10; i++)
    			printf("%d",ndigit[i]);
    		printf("Blanks = %d and Others = ",nblanks,nothers);
    	return 0;
    }//main
    It says I have a parse error before : at line 13, also says that I have unreachable code on the beginning of the switch statement, can you folks helo me?

  2. #2
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    It should be case <blah>:, not case:<blah>:. You have extraneous colons lying around.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Talking

    Quote Originally Posted by Robc
    It should be case <blah>:, not case:<blah>:. You have extraneous colons lying around.
    Thx a lot I've just figured it out, damm Pascal addictions...

  4. #4
    Quote Originally Posted by Maragato
    Oki I'm trying to lean C so don't blame on me, I'm having problems with this switch, the program itself is useless I'm just trying to learn the language syntax, here goes the code:

    It says I have a parse error before : at line 13, also says that I have unreachable code on the beginning of the switch statement, can you folks helo me?
    You have extra colons, which appears more obviously once the code has been correctly reformatted (I suggest GNU Indent) as follow (not fixed):
    Code:
    #include<stdio.h>
    
    /* Dumb Program to Count Digits */
    int main ()
    {
       int c, i, nblanks, nothers, ndigit[10];
    
       nblanks = nothers = 0;
       for (i = 0; i < 9; i++)
          ndigit[i] = 0;
       while ((c = getchar ()) != EOF)
       {
          switch (c)
          {
          case:
          '0': case:
          '1': case:
          '2': case:
          '3': case:
           '4':
          case:
          '5': case:
          '6': case:
          '7': case:
          '8': case:
           '9':
             ndigit[c - '0']++;
             break;
    
          case ' ':
          case '\n':
          case '\t':
             nblanks++;
             break;
    
          default:
             nothers++;
             break;
          }
       }                            /* switch */
       printf ("Digits = ");
       for (i = 0; i < 10; i++)
          printf ("%d", ndigit[i]);
       printf ("Blanks = %d and Others = ", nblanks, nothers);
       return 0;
    }                               /* main */
    Note that the 'recommenting' has been made by some utility of mine)
    Last edited by Emmanuel Delaha; 07-25-2004 at 05:29 AM. Reason: typos
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch - continue
    By DavidP in forum C Programming
    Replies: 2
    Last Post: 06-24-2004, 10:09 AM
  2. unwanted number in switch
    By chrismax2 in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2004, 05:43 AM
  3. Can a switch have poor quality?
    By Xei in forum Tech Board
    Replies: 1
    Last Post: 10-27-2003, 06:48 AM
  4. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 11:00 AM