Search code examples
cfgets

User-Defined function for reading input not working


I've made a user-defined function for reading input and replacing newline character '\n' with '\0' so when I use printf statement for printing the string it won't add newline at the end.

char xgets(char *line, int size, FILE *stdn)
{
    //READS THE LINE
    fgets(line, size, stdn);

    //REMOVES NEWLINE CHARACTER '\n' AND ADDS '\0'
    line[strcspn(line, "\n")] = '\0';

    return line;
}

When I call xgets inside main() function it works properly, but when it is called in other user-defined function it does not wait for user-input.

I'm using Visual Studio 2015 for debugging my code.

Here's my code:

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

//USER-DEFINED FUNCTION
char xgets(char *line, int size, FILE *stdn);
void sortm_hgrade();
void sortm_rcharge();
void header(void);

void header(void)
{
    printf("*-*-*-*-*HOTEL_INFO*-*-*-*-*");
    printf("\n\n");
}

char xgets(char *line, int size, FILE *stdn)
{
    //READS THE LINE
    fgets(line, size, stdn);

    //REMOVES NEWLINE CHARACTER '\n' AND ADDS '\0' END LINE CHARACTER
    line[strcspn(line, "\n")] = '\0';

    return line;
}

#define MAX 1000

//PROGRAMS STARTS HERE
int main(void)
{
    //VARIABLE-DECLARATION
    int i = 0, j = 0, n = 0;
    char line[MAX] = { 0 };
    char o = { 0 };
    char h[10] = { 0 };

    //FUCNTION CALL-OUT
    header();

    printf("Type anything : ");
    xgets(h, sizeof(h), stdin);

    printf("Enter one option from the following : \n\n");
    printf("(a) To Print out Hotels of a given Grade in order of charges. \n");
    printf("(b) To Print out Hotels with Room Charges less than a given Value. \n");
    printf("Please type a proper option. \n");

    while (n == 0){
        scanf_s(" %c", &o);

        switch (o){
            case 'a':
                sortm_hgrade();
                n = 1;
                break;

            case 'b':
                sortm_rcharge();
                n = 1;
                break;

            default:
                printf("Option INVALID \n");
                printf("Please type a proper option \n");
                n = 0;
                break;
        }
    }

    //TERMINAL-PAUSE
    system("pause");
}

void sortm_hgrade()
{
    //FOR SORTING BY GRADE
    char g[10] = { 0 };

    printf("Enter the Grade : ");
    xgets(g, sizeof(g), stdin);
    printf("\n");
}

void sortm_rcharge()
{
    printf("----");
}

Solution

  • You should change

    scanf(" %c", &o);
    

    to

    scanf("%c ", &o);
    

    This force scanf to consume trailing chars, like '\n'

    In your code '\n' of user input for scanf %c is not consumed and it is consumed by fgets in your xgets function that exit immediately with an empty buffer.

    BTW that solution can wok only if a single char is input by user. Best code would be

    char c;
    while (n == 0)
    {
        o = getchar();
        while ((c = getchar()) != EOF && c != '\n') ;
    

    EDIT

    With the second solution code is waiting, and discarding, chars until a '\n' is triggered or end of file. In your specific case (using stdin as console) EOF is not mandatory. It will be mandatory in case of input is being read from a "real file".