1

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("----");
}
Eddy
  • 344
  • 5
  • 16
  • 1
    Please fix the indention if you expect others to read your code. – Lundin Feb 11 '16 at 08:36
  • You posted whole the code but the most important thing is the warning or the terminal result you didn't mention – Van Tr Feb 11 '16 at 08:49
  • @IlDivinCodino It does not wait user-input and then terminal ask me to press any key to end it. – Eddy Feb 11 '16 at 09:03
  • @Lundin I had mentioned problem and only entered the code that is producing the problem which is enough for answering I suppose. The code I have entered is very short as compared to real one. I removed most of it to make it small as much as possible so others can understand. I'm new in C and also new on this website. If I'm making any mistake sorry about it. – Eddy Feb 11 '16 at 09:08
  • Apart from the problem with `scanf`, you assume that `fgets` is always ended with a trailing new-line. – David Ranieri Feb 11 '16 at 09:12
  • BTW there is no such thing as "user-defined functions". It's just "functions". You don't write a user-defined function, you write a function. – Jabberwocky Feb 11 '16 at 09:26

2 Answers2

2

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".

LPs
  • 16,045
  • 8
  • 30
  • 61
  • @Eddy I guessed MSDN `scanf_s` woks as linux standard `scanf`, probably not. use the second solution, it is better. – LPs Feb 11 '16 at 09:08
  • Man it's working but can you please explain what is exactly happening when it reads the line - while ((c = getchar()) != EOF && c != '\n') ; - Sorry for asking noob questions. And thanks alot for fixing my code ;) – Eddy Feb 11 '16 at 09:28
  • @Eddy I edited. Take a look [HERE](http://stackoverflow.com/questions/10720821/im-trying-to-understand-getchar-eof) also. – LPs Feb 11 '16 at 09:35
  • Just bit confused. EOF means End of File but can you tell me which file does it mean and why its value is -1? Also I still not getting how that while loop is working. If possible can you please explain it to me in more simplified way? Sorry for noob questions. – Eddy Feb 11 '16 at 12:02
  • The while works as a flush of all input data after the first character. It reads (getchar) all character input by user. You can avoid EOF in your specific case, because of stdin is not a "real file". In case you'll read a file, like a .txt, you must ensure to check if you reach the end of the file. – LPs Feb 11 '16 at 12:54
  • I got it now thnx again LPs ;) – Eddy Feb 11 '16 at 12:58
1

You need to skip the \n character after you take in a character. you can command scanf for that. fgets reads that newline character up first and then hence it terminates. use this

scanf(" %c *[^\n]", &o);

This should do the trick

sameera sy
  • 1,708
  • 13
  • 19
  • Not working - Now after it takes input for single character it is waiting for one more user input and after that same thing happens - It do not read user-input for xgets. – Eddy Feb 11 '16 at 09:22
  • It's perfectly correct. please check your `while` condition. once you enter you case, you only decide to exit by making `n=1` – sameera sy Feb 11 '16 at 09:27