LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2018, 09:42 AM   #1
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 52

Rep: Reputation: 0
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;
}
 
Old 09-25-2018, 09:58 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 20,641

Rep: Reputation: 5068Reputation: 5068Reputation: 5068Reputation: 5068Reputation: 5068Reputation: 5068Reputation: 5068Reputation: 5068Reputation: 5068Reputation: 5068Reputation: 5068
Quote:
Originally Posted by rahulvishwakarma View Post
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.
 
1 members found this post helpful.
Old 09-25-2018, 10:49 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,588

Rep: Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101
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
 
1 members found this post helpful.
Old 10-07-2018, 02:38 AM   #4
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 52

Original Poster
Rep: Reputation: 0
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.
 
Old 10-07-2018, 07:13 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,588

Rep: Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101
Feel free to read my previous post.
 
2 members found this post helpful.
Old 10-11-2018, 09:40 PM   #6
rahulvishwakarma
Member
 
Registered: Aug 2010
Posts: 52

Original Poster
Rep: Reputation: 0
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 :
 
Old 10-11-2018, 10:10 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,874

Rep: Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387
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;
}

Last edited by BW-userx; 10-11-2018 at 10:17 PM.
 
Old 10-11-2018, 10:25 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,588

Rep: Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101Reputation: 1101
> 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
 
1 members found this post helpful.
Old 10-12-2018, 08:36 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,874

Rep: Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387
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.

Last edited by BW-userx; 10-12-2018 at 12:33 PM.
 
Old 10-12-2018, 12:29 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,874

Rep: Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387Reputation: 1387
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.

Last edited by BW-userx; 10-12-2018 at 12:39 PM.
 
1 members found this post helpful.
  


Reply

Tags
c programming, linux


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CentOS 6.3 Randomly Stopped Recognizing Keyboard & Mouse Input spunce Linux - General 3 08-08-2012 08:35 AM
xf86-input-keyboard and xf86-input-mouse masked CollieJim Gentoo 4 11-09-2009 09:57 PM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
tty not taking keyboard input plus clock problem Treize.k Linux - General 1 10-31-2006 05:46 AM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration