Capturing the return/enter as keystroke to quit program

270 Views Asked by At

This program reads in a file and then asks the user to enter a number of lines to be displayed. After the number of lines have been displayed, the user is prompted again for either more lines to be printed or to quit by pressing return.

I'm having difficulty quitting by capturing the newline and/or carriage returns. My program does't quit if the return button is pressed but does if I put the ascii value (10 is decimal for newline)

I want the program to quit when enter is pressed.

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
    FILE *file = fopen(argv[1], "r");
    int newLineCounter, currentChar, numOfLines;

    printf("enter a number of lines of lines to be displayed\n");
    scanf("%d", &numOfLines);

    while ((currentChar = fgetc(file)) != EOF)
    {
        printf("%c", currentChar);    //print character

        if (currentChar == '\n')      //check for newLine character
            newLineCounter++;  

        if (numOfLines == newLineCounter)
        {
            printf("\nenter a number of lines to be displayed or just return to quit\n"); 
            scanf("%d", &numOfLines);

            newLineCounter = 0; 

            //supposed to exit if return is pressed
            if (numOfLines == '\n')  //????why does this only execute when the decimal value of newline is entered
                return 0;
        }
    }

    //printf("%d lines in the text file\n", newLineCounter);

    fclose(file);

    return 0;
}
2

There are 2 best solutions below

0
On

Instead of using scanf, you can use:

That way you can detect empty lines.

To parse the integer from a non-empty line, you can use:

So your program should be something like:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
    FILE *file = fopen(argv[1], "r");
    int newLineCounter, currentChar, numOfLines;

    while (true) {
        printf("enter a number of lines of lines to be displayed\n");
        int bytes_read;
        int nbytes = 100;
        char *my_string;

        my_string = (char *) malloc (nbytes + 1);
        bytes_read = getline (&my_string, &nbytes, stdin);
        if (bytes_read == 1) break; // exits if nothing entered on line
        // I believe it reads the newline character at minimum (but check this)
        sscanf(my_string, "%d", &numOfLines);

        // now do stuff with the numOfLines variable
    }


    fclose(file);

    return 0;
}
2
On

You seem to have a faulty understanding of scanf's format string. %d explicitly means "I'm looking for an integer." This is why you only get the ASCII digit representation of the new line character.

If you want to capture a new line as a character, your scanf needs to be %c. Then you can write a simple function that will convert the character representation of the integer into the actual integer. However %c will also only read a single character, so what you really want to use is %s to read a series of characters (also known as a String) that will be placed in a character pointer variable or character array if you know the maximum number of digits you're willing to accept. There are few differences between a character array and a String. Specifically a String is a character array that is null terminated (the last element of the string is the null character, \0).

Long answer short, use a character pointer and change your scanf format string to be %s. Then check to see if the first character is \n. If it is not, convert the string to an integer and use that value to read your lines.

Below is your code altered slightly to reflect this solution.

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
    FILE *file = fopen(argv[1], "r");
    int newLineCounter, currentChar, numOfLines;

    printf("enter a number of lines of lines to be displayed\n");
    scanf("%d", &numOfLines);

    while ((currentChar = fgetc(file)) != EOF)
    {
        printf("%c", currentChar);    //print character

        if (currentChar == '\n')      //check for newLine character
            newLineCounter++;  

        if (numOfLines == newLineCounter)
        {
            char* input;
            printf("\nenter a number of lines to be displayed or just return to quit\n"); 
            scanf("%s", &input);

            if( input[0] == '\n' || input[0] == '\0' ){
                return 0;
            } else {
                numOfLines = atoi( input );
            }
            newLineCounter = 0; 

            //supposed to exit if return is pressed
//            if (numOfLines == '\n')  //????why does this only execute when the decimal value of newline is entered
//                return 0;
//        }
    }

    //printf("%d lines in the text file\n", newLineCounter);

    fclose(file);

    return 0;
}