Thread: what's wrong with this program?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    what's wrong with this program?

    /* hello, this is a simple program that uses a while with nested switch. The problem is that why the EOF isn't wokring or is it a different case? and thanks */
    Code:
    #include <stdio.h>
    int main( void ) {              
                  int grade;
    	int acount = 0;
    	int bcount = 0;
    	int ccount = 0;
    	int dcount = 0;
    	int fcount = 0;
    
    	printf( "enter the letter grades(A,B,C,D,F).\n");
    	printf( "enter the EOF character to end input\n");
    	
    	while( (grade = getchar()) != EOF )  {
    	
    		switch (grade) {
    
    		 case'A':
    		 case'a':
    			++acount;
    			break;
    		
    		 case'B':
    		 case'b':
    			++bcount;
    			break;
    			
    		 case'C':
    		 case'c':
    			 ++ccount;
    			 break;
    			
    		 case'D':
    		 case'd':
    			 ++dcount;
    			 break;	
    		 
    		 case'F':
    		 case'f':
    			 ++fcount;
    			 break;	
    		
    		 case '\n':
    		 case '\t':
    		 case '  ':
    			 break;
    
    		 default:
    			 printf( " \nincorrect letter grade entered. ");
    			 printf( " Enter a new grade. \n");
    			 break;
    		}
    	}
    	printf( "\ntotals for each letter grade are :\n");
    	printf( "A: %d\n", acount );
    	printf( "B: %d\n", bcount );		 
    	printf( "C: %d\n", ccount );
    	printf( "D: %d\n", dcount );
    	printf( "F: %d\n", fcount );
    return0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What are you using to enter EOF, and what operating system are you using?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you're using EOF to break out of the loop from the keyboard... On Windows it's CTRL-Z and on Linux I believe it's CTRL-D ... lord knows what it might be on a Mac.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    2
    "If you're using EOF to break out of the loop from the keyboard... On Windows it's CTRL-Z and on Linux I believe it's CTRL-D ... lord knows what it might be on a Mac. " thanks this helped.
    thank you rags_to_riches too.
    regards,

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Tater, as Mac is based on BSD, it's Ctrl-D as well.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rags_to_riches View Post
    Tater, as Mac is based on BSD, it's Ctrl-D as well.
    Okey dokey... noted for future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone tell me what's wrong with this program?
    By LightYear in forum C Programming
    Replies: 15
    Last Post: 04-10-2010, 09:57 PM
  2. What is wrong with my program
    By bijan311 in forum C++ Programming
    Replies: 18
    Last Post: 12-13-2009, 05:04 PM
  3. What was wrong with my program?
    By cjmdjm in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 03:39 AM
  4. Help! What is wrong with this program?
    By Northstar in forum C Programming
    Replies: 1
    Last Post: 11-05-2007, 01:05 AM