Thread: What's problem with this program?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    27

    What's problem with this program?

    //it calculates the number of space/tab/enter key of the string you input from the keybord. But when I run it and input the string with "CTRL-Z" to end, nothing display on the dos screen. why??
    Code:
     
    #include<stdio.h>
    #include <string.h>
    int main()
    {
    	char c;
    	int space=0,table=0,enter=0;
    	printf("please input a string:\n");
    	scanf("%c",&c);
    	while(c!=(char)EOF)
    	{
    		switch(c)
    		{
    			case 32:space++;break;	
    			case 9:table++;break;
    			case 10:enter++;break;
    			default : break;
    		}
    	    scanf("%c",&c);
    	}
    	
    	printf("the number of space:%d\n",space);
    	printf("the number of table:%d\n",table);
    	printf("the number of enter:%d\n",enter);
    
    	getchar();
    	return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    1) it's not a "dos" screen, it's the bash shell. You don't signal EOF with ctrl-Z, you signal it with ctrl-D.

    However, EOF is not really a char value, it's an int. Read this:
    C file input/output - Wikipedia, the free encyclopedia
    Which exactly describes your problem. Unfortunately, it does not exactly describe a solution for scanf(), because using scanf("%c") to read into an int will not produce very satisfactory results. You need to use getchar() or fgetc().

    You can also use fgets(), which handles EOF properly:
    Code:
    #include<stdio.h>
    #include <string.h>
    int main()
    {
    	char line[4096];
    	int len, i, space=0,table=0,enter=0;
    	printf("please input a string:\n");
    	while(fgets(line,4096,stdin)) {
    		len = strlen(line);
    		for (i=0;i<len;i++) {
    			switch(line[i]) {
    				case 32:space++;break;	
    				case 9:table++;break;
    				case 10:enter++;break;
    				default : break;
    			}
    		}
    	}
    	
    	printf("the number of space:%d\n",space);
    	printf("the number of table:%d\n",table);
    	printf("the number of enter:%d\n",enter);
    
    	/* no MS IDE, no final getchar */
    	return 0;
    }
    However, you might sometimes have to hit ctrl-D twice.

    The best way to do this is with getchar or fgetc.
    Last edited by MK27; 06-23-2010 at 08:38 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    Yeah!It is working now.But I want to run it in VB, then I did try . However, it is a pity that it didn't work.Why?

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by Steve Cao View Post
    Yeah!It is working now.But I want to run it in VB, then I did try . However, it is a pity that it didn't work.Why?
    You do know this is the Linux subforum right?? Like VB doesn't run here....so how did you expect it to work?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Code:
    				case 32:space++;break;	
    				case 9:table++;break;
    				case 10:enter++;break;
    Do not use magic numbers

    Use ' ', '\t' , '\n' - they are readable
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM