how to stop this program?

This is a discussion on how to stop this program? within the C Programming forums, part of the General Programming Boards category; /* This is a program intended to copy its input to its output, replacing each string of one or more ...

  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,386
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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, 08:24 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01: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, 06:19 PM

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