Muhammad Waseem
Muhammad Waseem

Reputation: 19

How to take input until enter is pressed twice?

I want to break this loop when the user press enters twice. Meaning, if the user does not enter a character the second time, but only presses enter again, the loop must break.

char ch;
while(1) {
    scanf("%c",&ch);
    
    if(ch=='') { // I don't know what needs to be in this condition
        break;
    }
}

Upvotes: 1

Views: 1470

Answers (3)

Yun
Yun

Reputation: 3812

It is not possible to detect keypresses directly in C, as the standard I/O functions are meant for use in a terminal, instead of responding to the keyboard directly. Instead, you may use a library such as ncurses.

However, sticking to plain C, we can detect newline characters. If we keep track of the last two read characters, we can achieve similar behavior which may be good enough for your use-case:

#include <stdio.h>

int main(void)
{
    int currentChar;
    int previousChar = '\0';
    while ((currentChar = getchar()) != EOF)
    {
        if (previousChar == '\n' && currentChar == '\n')
        { 
            printf("Two newlines. Exit.\n");
            break;
        }
        if (currentChar != '\n')
            printf("Current char: %c\n", currentChar);

        previousChar = currentChar;
    }
}

Edit: It appears that the goal is not so much to detect two enters, but to have the user:

  • enter a value followed by a return, or
  • enter return without entering a value, after which the program should exit.

A more general solution, which can also e.g. read integers, can be constructed as follows:

#include <stdio.h>

#define BUFFER_SIZE 64U

int main(void)
{
    char lineBuffer[BUFFER_SIZE];
    while (fgets(lineBuffer, BUFFER_SIZE, stdin) != NULL)
    {
        if (lineBuffer[0] == '\n')
        {
            printf("Exit.\n");
            break;
        }

        int n;
        if (sscanf(lineBuffer, "%d", &n) == 1)
            printf("Read integer: %d\n", n);
        else
            printf("Did not read an integer\n");

    }
}

Note that there is now a maximum line length. This is OK for reading a single integer, but may not work for parsing longer input.

Credits: chux - Reinstate Monica for suggesting the use of int types and checking for EOF in the first code snippet.

Upvotes: 2

Muhammad Waseem
Muhammad Waseem

Reputation: 19

Working like charm now

char ch[10];
while(1){
    
     fgets(ch, sizeof ch, stdin);
    
    if(ch[0]=='\n'){
        break;
    }
   
    
    
}

Upvotes: 0

Lajos Arpad
Lajos Arpad

Reputation: 76551

You can store the previous character and compare it with the current character and enter, like this:

char ch = 'a', prevch = '\n';
while(1){
    scanf("%c",&ch);
    if((ch=='\n') && (ch == prevch)){// don't know what needs to be in this condition
        break;
    }
    prevch = c;
}

Note that the previous character by default is enter, because we want the program to stop if the user hits enter at the very start as well.

Upvotes: 1

Related Questions