LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   fgets is not taking input from keyboard after one input in centos 7.5 (https://www.linuxquestions.org/questions/programming-9/fgets-is-not-taking-input-from-keyboard-after-one-input-in-centos-7-5-a-4175639135/)

rahulvishwakarma 09-25-2018 09:42 AM

fgets is not taking input from keyboard after one input in centos 7.5
 
hi to all,
i've centos7.5 and codeblocks 17.12 in VM i am building a programm for date manipulation. shared library for this is OK. but in main file where I called these functins having problem when I tried to input for two dates ( date1 and date2 ),date1 is getting input well but date2
is skipped automatically. I am not gettiing how to get input for date2. pleaase help.code for validity and cmpDate is in lib file.
Code:

#include <stdio.h>
#include <stdlib.h>
#include <dtmanip.h>// -- this is shared lib file contains all                                    //        date related functions

int main()
{

    int choice;
    //char date[11], date1[11], date2[11], newdate[11];
    char *date, *date1, *date2, *newdate;
    int y, m, d;


    FILE *in, *out;

    while(1)
    {
        printf( "1.  Date validation \n"
                "2.  Compare Date \n"
                        "3.  Exit\n"
                );
          printf("Enter your choice : ");
        scanf("%d", &choice);

        in = fopen("/dev/tty", "r");



        switch(choice)
        {
            case 1: printf(" Enter date (dd/mm/yyyy) : ");

                    fflush(in);
                    buffer = (char*)malloc(11);
                    str = (char *)malloc(11);
                    date = (char *) malloc(11);

                    fgets(buffer, 11, in);

                    strcpy(date , buffer);

                    if(isValid(date))
                    {
                        printf("valid date \n");
                    }

                    else
                    {
                        printf("invalid date\n");
                    }


                    free(buffer);
                    free(str);
                    free(date);

                    break;
           
          case 2: fflush(in);
                    buffer = (char*)malloc(11);
                    str = (char *)malloc(11);
                    date1 = (char *) malloc(11);
                    printf("buffer = %s, str = %s, date1 = %s \n", buffer, str, date1);

                    printf("Enter date1 : ");
                    fgets(buffer, 11, in);

                    printf("buffer = %s\n", buffer);

                    strcpy(date1, buffer);

                    printf("date1 = %s\n", date1);

                    printf("Enter date2 : ");
                    fflush(in);
                    free(buffer);

                    buffer = (char *)malloc(11);

                    fgets(buffer, 11, in);
                    date2 = (char *) malloc(11);

                    strcpy(date2, buffer);

                    printf("buffer = %s, str = %s, date2 = %s \n", buffer, str, date2);
                    printf("buffer = %s\n", buffer);

                    strcpy(date2 , buffer);
                    printf("date2 = %s\n", date2);


                    if( !isValid(date1) || !isValid(date2))
                    {
                        printf("Enter only valid dates\n");
                        free(buffer);
                        free(str);
                        free(date);

                        break;


                    }

                    if( cmpDate(date1, date2) == 0)
                        printf("Both dates are same;\n");

                    else if ( cmpDate(date1, date2) == 1)
                        printf("Date1 : %s < date2 : %s\n ", date1, date2);

                    else if(cmpDate(date1, date2) == -1)
                        printf("date1 %s > date2 : %s\n",        date1, date2);

                    free(buffer);
                    free(str);
                    free(date);

                    break;
        case 3: exit(EXIT_SUCCESS);

            default: printf("wrong choice \n");
        }

    }


    return 0;
}

in file valid.c
Code:

int isValid(char *date)
{
    int y, m ,d;

    splitDate(date, &y, &m, &d);

    if( d == 0 || m == 0 || y == 0)
        return 0;

    int months[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if(isLeap(y))
    {
        months[1] = 29;
    }

    if(m > 12 || m < 1)
        return 0;

    if( d <  0 || d > months[m - 1] )
        return 0;

    if( y < 1900 || y > 2100)
        return 0;


    return 1;

}

in file cmpdate.c
Code:

int cmpDate(char *date1, char *date2)
{
    int d1, m1, y1, d2, m2, y2;

    splitDate(date1, &y1, &m1, &d1);
    splitDate(date2, &y2, &m2, &d2);

    if( y1 > y2 )
        return -1;
    if( y1 < y2 )
        return 1;
    if(m1 > m2 )
        return -1;
    if(m1 < m2 )
        return 1;
    if( d1 > d2 )
        return -1;
    if(d1 < d2 )
        return 1;

    return 0;
}


TB0ne 09-25-2018 09:58 AM

Quote:

Originally Posted by rahulvishwakarma (Post 5907607)
hi to all,
i've centos7.5 and codeblocks 17.12 in VM i am building a programm for date manipulation. shared library for this is OK. but in main file where I called these functins having problem when I tried to input for two dates ( date1 and date2 ),date1 is getting input well but date2 is skipped automatically. I am not gettiing how to get input for date2. pleaase help.code for validity and cmpDate is in lib file.

Ok, so what debugging have you done? What happens when you run the program?? Any details at all, or are we supposed to complile/run your code to see what's happening??

And again, after asking about codeblocks/C programming for eight years...surely you have made SOME progress:
https://www.linuxquestions.org/quest...7/#post5837681

Even more odd that you're flushing the input buffer, right after you're asking for the second date, and seem surprised when it's empty.

NevemTeve 09-25-2018 10:49 AM

The very first problem that you don't check buffer after fgets: if the line was longer that net 9 bytes (brt 11: don't forget \n and \0), then issue a warning, otherwise remove the \n

example: https://stackoverflow.com/questions/...om-fgets-input

rahulvishwakarma 10-07-2018 02:38 AM

i removed line

Code:


 printf("buffer = %s, str = %s, date1 = %s \n", buffer, str, date1);

after that output is :-

Code:

[rahul@client1ora11gr2 Debug]$ ./datemain
1.  Date validation
2.  Compare Date
3.  Exit
Enter your choice : 2
Enter date1 : 21/12/2009
buffer = 21/12/2009
date1 = 21/12/2009
Enter date2 : buffer =
, str = , date2 =
 
buffer =

date2 =

Enter only valid dates
1.  Date validation
2.  Compare Date
3.  Exit
Enter your choice : 1
 Enter date (dd/mm/yyyy) : 12/12/2009
valid date
1.  Date validation
2.  Compare Date
3.  Exit
Enter your choice : 3
[rahul@client1ora11gr2 Debug]$

why it is not taking input after first input of date1.

NevemTeve 10-07-2018 07:13 AM

Feel free to read my previous post.

rahulvishwakarma 10-11-2018 09:40 PM

i found there is no trailing new line or carrige return, at the end there is 0 (NULL); and still it is not taking input from second fgets
Code:

[rahul@client1ora11gr2 Debug]$ ./datemain
1.  Date validation
2.  Compare Date
3.  Exit
Enter your choice : 2
Enter date1 : 12/12/2009
*str = 1 char= 49*str = 2 char= 50*str = / char= 47*str = 1 char= 49*str = 2 char= 50*str = / char= 47*str = 2 char= 50*str = 0 char= 48*str = 0 char= 48*str = 9 char= 57*str =  char= 0

buffer = 12/12/2009
date1 = 12/12/2009
Enter date2 : buffer =
, str =
, date2 =
 
buffer =

date2 =

Enter only valid dates
1.  Date validation
2.  Compare Date
3.  Exit
Enter your choice :


BW-userx 10-11-2018 10:10 PM

check your logic for case2: in main. "Comments" hints within code
Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> //strcpy
#include <dtmanip.h>// -- this is shared lib file contains all          //        date related functions

int main()
{

    int choice;
    //char date[11], date1[11], date2[11], newdate[11];
    char *date, *date1, *date2, *newdate, *buffer, *str;
    int y, m, d;


    FILE *in, *out;

    while(1)
    {
        printf( "1.  Date validation \n"
                "2.  Compare Date \n"
                        "3.  Exit\n"
                );
          printf("Enter your choice : ");
        scanf("%d", &choice);

        in = fopen("/dev/tty", "r");



        switch(choice)
        {
            case 1: printf(" Enter date (dd/mm/yyyy) : ");

                    fflush(in);
                    buffer = (char*)malloc(11);
                    str = (char *)malloc(11);
                    date = (char *) malloc(11);

                    fgets(buffer, 11, in);

                    strcpy(date , buffer);

                    if(isValid(date))
                    {
                        printf("valid date \n");
                    }

                    else
                    {
                        printf("invalid date\n");
                    }


                    free(buffer);
                    free(str);
                    free(date);

                    break;
           
                case 2:
                  fflush(in); //???
                    buffer = (char*)malloc(11);
                    str = (char *)malloc(11);
                    date1 = (char *) malloc(11);
                    printf("buffer = %s, str = %s, date1 = %s \n", buffer, str, date1);

                    printf("Enter date1 : ");
                  fgets(buffer, 11, in); //maybe stdin,  humm??

                    printf("buffer = %s\n", buffer);

                    strcpy(date1, buffer);

                    printf("date1 = %s\n", date1);

                  printf("Enter date2 : ");
                --->  fflush(in);
                  ---> free(buffer);

                    buffer = (char *)malloc(11);

                  fgets(buffer, 11, in);
                    date2 = (char *) malloc(11);

                    strcpy(date2, buffer);

                    printf("buffer = %s, str = %s, date2 = %s \n", buffer, str, date2);
                    printf("buffer = %s\n", buffer);

                    strcpy(date2 , buffer);
                    printf("date2 = %s\n", date2);


                    if( !isValid(date1) || !isValid(date2))
                    {
                        printf("Enter only valid dates\n");
                        free(buffer);
                        free(str);
                        free(date);

                        break;


                    }

                    if( cmpDate(date1, date2) == 0)
                        printf("Both dates are same;\n");

                    else if ( cmpDate(date1, date2) == 1)
                        printf("Date1 : %s < date2 : %s\n ", date1, date2);

                    else if(cmpDate(date1, date2) == -1)
                        printf("date1 %s > date2 : %s\n",        date1, date2);

                    free(buffer);
                    free(str);
                    free(date);

                    break;
        case 3: exit(EXIT_SUCCESS);

            default: printf("wrong choice \n");
        }

    }


    return 0;
}


NevemTeve 10-11-2018 10:25 PM

> I found there is no trailing new line

That means the buffer is too small for the line; the rest will be returned by the next fgets.

Kindly read my first post: the buffer should be big enough for the complete line plus \n plus \0

BW-userx 10-12-2018 08:36 AM

gets is not taking input from keyboard
 
as it looks like you're asking user for input then using a file to get what you're asking for off the cli.
Just a little bit of code, Taking NevemTeve guidance he is trying to get across to you in declaring a size big enough for the data, plus end line and null (?), and having a few too many does not hurt either. Because it gets terminated at the end if you do it properly.

Swiping some code off the net and modifying it into a function, and making it work like this, then calling it from main, if you compile it then run it you'll see how this works. getting off cli then processing it, then with the proper data returned, you can use it in a switch/case statement.

uses pointers without the use of malloc.

Code:

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

void StripDate(char *date, int *m, int *d, int *y)
{
       
    char *p = date, *wp;
    char m1[3] = "", d1[3] = "", y1[5] = "";
 
    /* parse each date component with simple pointer and loop */
    for (wp = m1; *p && *p != '/' && wp - m1 < 3; p++, wp++)      /* mm */
        *wp = *p;  /* copy to m */
        *wp = 0;        /* nul-terminate */
    for (++p, wp = d1; *p && *p != '/' && wp - d1 < 3; p++, wp++) /* dd */
        *wp = *p;
        *wp = 0;
    for (++p, wp = y1; *p && wp - y1 < 5; p++, wp++)  /* year */
        *wp = *p;
        *wp = 0;
               
                *m = atoi(m1);
                *d = atoi(d1);
                *y = atoi(y1);
               
               
}

int main (void) {

        char Date[12] ;
        int m, d, y;
               
        printf("Enter Date, mm/dd/yyyy\n");

        fgets(Date, sizeof(Date), stdin);
       
        StripDate(Date, &m, &d, &y);
       
        printf ("\n %d-%d-%d\n\n", m, d, y);

    return 0;
}

Questions:
if you are asking for user input, then why are you opening files that make no sense /dev/tty to check for a date?



Here " is skipped automatically. I am not gettiing how to get input for date2. pleaase help."

Code:



int main()
{

    int choice;
        char date[11], date1[11], date2[11];

    while(1)
    {
        printf( "\n1.  Date validation \n"
                "2.  Compare Date\n"
                "3.  Exit\n"
                );
          printf("\nEnter your choice : ");
          scanf("%d", &choice);

                switch(choice)
        {
            case 1:
                        printf(" 1: Enter date (dd/mm/yyyy) : ");
                        getchar();
                        fgets(date, sizeof(date), stdin);
                        printf("date taken %s\n", date);
                       
                        if(isValid(date) == 0)
                            printf("valid date \n");
                        else
                            printf("invalid date\n");
                break;
           
                case 2:
                        // fgets, and getline gets skiped over on second ask of
                        // data, so ... what to do to stop that from occurring?
                               
                        printf("2: Enter date1 :(dd/mm/yyyy) ");
                        getchar();
                        fgets(date1, sizeof(date1), stdin);
                                       
                        printf("2: Enter date2 : (dd/mm/yyyy) ");
                        getchar();
                        fgets(date2, sizeof(date2), stdin);                     
                   
                            if( (isValid(date1) == 1) || (isValid(date2) == 1) )
                        {
                            printf("Enter only valid dates\n");
                            exit(EXIT_FAILURE);
                        }
                   
                    if (strcmp(date1,date2) == 0)
                        printf("is same date\n");
                  else
                        printf("is different date\n");
                  break;
                                       
                        case 3:
                                        exit(EXIT_SUCCESS);
                        default: printf("wrong choice \n");
        }

    }


    return 0;
}

using getchar is a place holder to stop that skipping over when asking for data off the cli, WHICH needs to have stdin, not some file you opened up to get it. that is illogical.


yes, it is not your original code, I redid some of it and got it working. your functions too, the comparisons are wrong as well in isValid function.

I'm contemplating whether or not I should post all of it and point out some stuff. all of the mistakes actually. but I still do not know what the opening of files is doing for you when you're asking for input off the command line.

BW-userx 10-12-2018 12:29 PM

I went looking for
dtmanip.h
because I got curious, then found this

https://github.com/sunilsp77/Date-Ma.../master/Main.c

are you trying to write this in your "own words" because it is doing what you're trying to do, with a lot more doing to the date, using dtmanip.h with the same basic setup. It is find by me because it looks like you're actually trying to figure out something.


All times are GMT -5. The time now is 09:32 AM.