Search code examples
ckeyboardcygwin

cygwin on win7, ctrl-d doesn't have effect


I'm using Cygwin to compile C programs from the book Head First C. On my Win 7 laptop, when I enter text that the program is supposed to process, the book says to end the list of strings with Ctrl-d. Well, nothing happens. If I hit Ctrl-c, Cygwin closes. I heard to use Ctrl-Z instead but that will print "Stopped."

I tried adding a line that replaces \n with \0 because that was the answer to a problem that was on the errata.

How do I show the program that I'm done entering text? I see a lot of questions about Ctrl-C or if they ask about Ctrl-D they want to end the program. I want to keep the program running and just end the input.

Cygwin mintty 1.2-beta1

Here's the 3 files; the exercise is about making your own header files.

message_hider.c

#include <stdio.h>
#include <string.h>
#include "encrypt.h"

int main() {

    char msg[80];
    while(fgets(msg, 80, stdin)) {
        if(msg[strlen(msg)-1] == '\n')
            msg[strlen(msg)-1] = '\0';
        encrypt(msg);
        printf("%s", msg);
    }
}

encrypt.c

#include "encrypt.h"


void encrypt(char * message) {

    char c;
    while (*message) {
        *message = *message ^ 31;
        message++;
    }
}

encrypt.h

void encrypt(char * message);

Solution

  • Put the following statement:

    setbuf(stdout, NULL); 
    

    just after the opening brace of your main function.

    This turns out buffering for stdout. In this way you should see your output as soon as it is produced and it may help you diagnose the problem.