0

So i need to write an whileloop for a program that's supposed to prompt the user with this:

char vector[10];
while(????){
    print("Write a number");
    scanf("%s",vector);
}

printf("Goodbye");

The program is supposed to print goodbye and close when the user presses ctrl+c. Im pretty sure I cant use putchar in this case?

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
Tensora
  • 53
  • 8
  • @ajay What is the EOF character? The constant EOF is a constant that should be different from any other character. – Brandin Feb 16 '14 at 20:53

4 Answers4

1
#include <windows.h>
#include <stdio.h>

static end_flag = 0;

BOOL WINAPI controlHandler(DWORD type){
    if(type == CTRL_C_EVENT){
        end_flag = 1;
        return TRUE;
    }
    return FALSE;
}

int main(){
    char vector[10];

    if (!SetConsoleCtrlHandler(controlHandler, TRUE)) {
        fprintf(stderr, "Failed SetConsoleCtrlHandler");
        return -1;
    }
    while(!end_flag){
        printf("Write a number ");
        scanf("%s",vector);
    }

    printf("Goodbye");
    return 0;
}

CTRL+Z version

#include <stdio.h>

int main(){
    char vector[10];

    while(1){
        printf("Write a number ");
        if(scanf("%s", vector)==EOF)//press CTRL+Z
            break;
    }

    printf("Goodbye");
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • thanks ,it works!, and ouch, super complicated for me, one more problem I have is actually that there are lines of code in the whileloop that I don't want to run after the user has pressed ctrl+c? are there any rescues? – Tensora Feb 16 '14 at 22:34
  • @Tensora I think that it use `exit(1)` instead of setting the flag. – BLUEPIXY Feb 16 '14 at 22:38
  • the ctrl+z version works fine since it brakes it now, however it prints z^ and then the user has to press enter one time before it quits the loop. I have some real trouble with this – Tensora Feb 16 '14 at 22:47
  • what I mean is, is there any way to make the program quit the loop without pressing enter afterwards? – Tensora Feb 16 '14 at 23:15
  • @Tensora use getch or kbhit or key-status ...., but in everything yourself do. It is not realistic because no longer do without preparing. you do to CTRL+C and exit. – BLUEPIXY Feb 16 '14 at 23:29
0

see https://superuser.com/questions/214239/whats-the-command-prompts-equivalent-to-cygwins-ctrlz:

Depends on what you mean by "quit something"; within Windows cmd:

Ctrl+Z sends the EOF character, which could terminate a process if you're providing input, but otherwise will probably do nothing.

Ctrl+C normally sends SIGINT to the foreground process, which should terminate it, but programs can respond however they like - ie, they can catch the signal but then ignore it. The command can also be remapped to other jobs (such that for a specific program it doesn't really send a signal) or ignored entirely.

Ctrl+Break always sends SIGBREAK, which again should terminate the process, but unlike Ctrl+C cannot be remapped, but can still be ignored. This is probably what you need.

Community
  • 1
  • 1
Matthew
  • 2,035
  • 4
  • 25
  • 48
  • I think we are supposed to use the EOF, but I just want to get out of the whileloop with some kind of ctrl+? – Tensora Feb 16 '14 at 22:20
  • thanks, the exercise is actually to end the whileloop with ctrl+D in Linux, what would be the same on windows? – Tensora Feb 16 '14 at 22:23
0

I think this might be what you're looking for:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683242%28v=vs.85%29.aspx

McLovin
  • 3,554
  • 1
  • 14
  • 15
0

Post similar to this one on Stack Overflow: Catch Ctrl-C in C

You might want to check it out. Cheers!

Community
  • 1
  • 1
msmolcic
  • 6,407
  • 8
  • 32
  • 56
  • thanks, the exercise is actually to end I with ctrl+D in Linux, what would be the the same on windows? – Tensora Feb 16 '14 at 22:23