Thread: whats wrong with my code

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

    whats wrong with my code

    after i execute the code by ./code
    >i have entered a pragraph
    >i have pressed Ctrl+Enter to denote EOF
    >then a nothing happened i m supposed to see the results. the console keep on taking input,ie.condition in while got something wrong
    >are there any syntax errors in program

    Code:
    //Code to count spaces and newlines and alpha a and A in a paracgraph
    #include<stdio.h>
    int main()
    {
    static int word,line,a,ca;	
    char c;
    while((c=getchar( ))!=EOF)
    {
    	switch(c)
    	{
    	case ' ' :word++;
    		  break;
    	case '\n':line++;
    		  break;
    	case 'a' :a++;
    		  break;
    	case 'A' :ca++;
    		  break;
    	}
    }
    printf("No:Of Words\t No:Of Lines\tNo:of a's\t No:ofA's\t");
    printf("\n%d\t\t%d\t%d\t%d\t",word,line,a,ca);
    return 0;
    }
    my simple question is whats wrong with the code why am i unable to see results, why the loop is never ending
    i have attached .c and .out file
    Attached Files Attached Files
    Last edited by shyam.sunder91; 03-12-2012 at 06:05 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    >i have pressed Ctrl+Enter to denote EOF
    What OS is that? The ones I know of are *nix (ctrl-d) and windows (ctrl-z).
    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
    May 2011
    Posts
    14
    very thanks that was my fault used ctrl+Enter instead ctrl+d very sorry im a beginner execuse me admin

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Also, just FYI, using static variables when it's not necessary isn't generally a good idea. (because, afaik, the compiler won't optimize them into registers)

    Instead, just make them normal ints and initialize them to zero manually.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with this code?
    By smahdi1991 in forum C Programming
    Replies: 18
    Last Post: 11-01-2009, 11:12 AM
  2. whats wrong wid dis code!!
    By smooth in forum C Programming
    Replies: 4
    Last Post: 09-13-2008, 09:54 AM
  3. whats wrong with this code i have?
    By stehigs321 in forum Game Programming
    Replies: 1
    Last Post: 10-29-2003, 08:38 PM
  4. Code: Tell me whats wrong !
    By slx47 in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2002, 04:39 PM
  5. whats wrong with my code?
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 11-01-2001, 04:18 PM

Tags for this Thread