Thread: Not getting output from counting getchar() blanks

  1. #1
    CIS and business major
    Join Date
    Aug 2002
    Posts
    287

    Not getting output from counting getchar() blanks

    Code:
    #include "stdafx.h"
    #include <cstdio>
    
    using namespace System;
    
    int main(array<System::String ^> ^args)
    {
        int c, nl;
    	
    
    	nl = 0;
    	while ((c = getchar()) != EOF) 
    		if(c == ' ')
    			nl++;
    			
    
    		printf("The number of blanks is %d\n", nl);
    	
    
        return 0;
    }
    I'm trying to count the number of blank (" ") spaces from user input using getchar(), then outputting the value using printf. But no output is done after I put in characters into the program. Any suggestions?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,534
    One suggestion would be to actually write it in C, not the brain-damage of M$ "managed" C++.

    The other is when you're typing away at your console, to actually generate an EOF signal, you press ctrl-z (as the first 'character' on any input line).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing blanks
    By Giwrgows x in forum C Programming
    Replies: 9
    Last Post: 12-22-2015, 11:58 AM
  2. Replies: 2
    Last Post: 12-22-2012, 09:00 AM
  3. Entab - replacing blanks with tabs and blanks
    By thames in forum C Programming
    Replies: 3
    Last Post: 10-20-2012, 03:11 PM
  4. counting number of lines in my output file
    By zesty in forum C Programming
    Replies: 34
    Last Post: 12-25-2007, 09:15 PM
  5. counting and output, with arrays and functions
    By Wraithan in forum C++ Programming
    Replies: 7
    Last Post: 12-05-2005, 12:46 AM

Tags for this Thread