Whats wrong?

This is a discussion on Whats wrong? within the C Programming forums, part of the General Programming Boards category; Hello, i cant find anything wrong with it, but it isnt working correctly: Code: #include <stdio.h> /*count digits, white space, ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    5

    Whats wrong?

    Hello, i cant find anything wrong with it, but it isnt working correctly:

    Code:
    #include <stdio.h>
    
    /*count digits, white space, others */
    
    main() {
    	int c, i, nwhite, nother;
    	int ndigit [10];
    	
    	nwhite = nother = 0;
    	for(i = 0; i < 10; ++i){
    		ndigit[i] = 0;
    	}
    	while((c = getchar()) != EOF){
    		if(c >= '0' && c <= '9'){
    			++ndigit[c-'0'];
    		}
    		else if(c == ' ' || c == '\n' || c == '\t') {
    			++nwhite;
    		}
    		else{
    			++nother;
    	 	}
     	}
    	 	printf("digits =");
    	 	for(i = 0; i < 10; ++i) {
    		 	printf(" %d", ndigit[i]);
    		 	printf(", white space = %d, other = %d\n", nwhite, nother);
    	 	}
     	}
    It compiles with no errors, but when i run it in cygwin, it is just like a regular input program, it comes up with no output, doesnt say how many words etc ar in it. This is the cygwin command bash:

    Code:
    Family_2@Family-PC ~
    $ cd projects
    
    Family_2@Family-PC ~/projects
    $ ls
    charcount  count  fahr  helloworld  input  linecount  vnc1l
    
    Family_2@Family-PC ~/projects
    $ cd count
    
    Family_2@Family-PC ~/projects/count
    $ ls
    arrays.cpp  arrays.exe  charcount.cpp  linecount.cpp  wordcount.cpp
    
    Family_2@Family-PC ~/projects/count
    $ ./arrays
    hello world
    
    
    
    
    
    WTF?

  2. #2
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    It won't print anything until it hits eof. Hitting enter just gives it more and more newlines, which it parses before asking for another character

    Control-D = eof when inputting via command line

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    5
    Ok, it worked, thank you, sorry for the nub question ='B

    Oh, and sometimes, when i compile something, it says:
    warning: no newline at end of file.

    It still runs fine, so why does it come up with the message?
    Last edited by D3V1LD0G; 08-19-2008 at 08:27 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because the C standard requires every code file to end with a newline character. Obviously, if the program proper is complete, gcc (and pretty much every other compiler, I think) will compile it anyway and just tell you that you forgot that bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21