Thread: how to stop this program?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    4

    how to stop this program?

    /* This is a program intended to copy its input to its output, replacing
    each string of one or more blanks by a single blank.
    My question is that how can I terminate the program in VC instead of running
    it without stop?*/

    #include <stdio.h>
    #define NONBLANK 'a'

    void main (void)
    {
    char i=getchar();
    int lastc=NONBLANK;
    while (i!=EOF){
    if (i!=' ')
    putchar(i);
    if (i==' ')
    if (lastc!=' ')
    putchar(i);
    lastc=i;
    }
    }

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You should build in a statement which causes the loop to stop, in the current situation, the value of i doesn't ever change in the loop. So i will never be equal to EOF.

    You should put

    i=getchar();

    somewhere in the loop.

    To kill the process running you could use the task manager, CTRL-ALT-DEL.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Shiro
    So i will never be equal to EOF.
    EOF is an int, i has been defined as a char. oops

    And don't even start on the void main() bit ! Change it to int main quick before the regulars see it


    When you get the code sorted, you'll find EOF can be generated from the keyboard via CTRL+d or CTRL+z depending on your OS, or it's automatically generated if you're reading a file and you get to the end.
    Last edited by Hammer; 04-30-2002 at 05:56 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The loop for simply copying chars is
    Code:
    #include <stdio.h>
    int main ( ) {
      int ch;
      while ( (ch=getchar()) != EOF ) {
        // add your decision code here
        putchar( ch );
      }
      return 0;
    }
    ctrl-D or ctrl-Z usually generate the EOF

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Early program stop problem
    By bmb_ksu in forum C Programming
    Replies: 9
    Last Post: 02-19-2006, 09:24 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. continuing program until ready to stop
    By jlmac2001 in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2003, 07:19 PM