Thread: Format User Input When using strings

  1. #1

    Question Format User Input When using strings

    Hi, could somebody help me with a simple task.
    This section of my program asks the user to enter a date, simple enough. The problem is theres nothing to stop the user from entering more than two characters for each part of the date and messing up the rest of the screen

    Eg. theres nothing stopping the user entering 12345646435465435354643456431654 in for Day which only wants 12.

    Is there any way to 'format' or restrict the amount of letters the user enters or to move to the next section or loop around once two nubers are entered?

    Note: I want to keep the 3 variables characters(no integars).
    Tab and Display menu are other functions I wrote in my program.
    Tab just clears text to the amount passed down to it. Eg. Tab(2) will clears two spaces.

    /************************************************** */

    char Day[3], Month[3], Year[3];

    gotoxy(20,10); cprintf("Enter Date : ");
    gotoxy(22,12); cprintf("[____] / [____] / [____]");
    gotoxy(23,14); cprintf("[ [DD] / [MM] / [YY] ]");
    gotoxy(22,18); cprintf("[ Press RETURN key To Quit ]");

    /* Get Day */
    do{ // Repeat
    gotoxy(24,12); Tab(2); // Clear line of text
    gotoxy(24,12); gets(Day); // Get Day for Date

    if(strcmp(Day, "") == 0) // If entered RETURN key
    Display_Menu(); // Go to Main Menu
    }while( (atoi(Day) > 31)||(atoi(Day)<= 0)); // While day is invalid

    /* Get Month */
    do{ // Repeat
    gotoxy(33,12); Tab(2); // Clear line of text
    gotoxy(33,12); gets(Month); // Get Month for Date

    if(strcmp(Month, "") == 0) // If entered RETURN key
    Display_Menu(); // Go to Main Menu
    }while((atoi(Month)>12)||(atoi(Month)<=0)); // While month is invalid

    /* Get Year */
    do{ // Repeat
    gotoxy(42,12); Tab(2); // clear line of text
    gotoxy(42,12); gets(Year); // Get Year for Date

    if(strcmp(Year, "") == 0) // If entered RETURN key
    Display_Menu(); // Go to Main Menu
    }while((atoi(Year) > 20)&&(atoi(Year) <= 96)); // While year is invalid

    /************************************************** */


  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Take the input and validate it, if the user enters something wrong then tell them and try to input again.
    /* pseudocode */
    for ( ; ; ) { /* forEVER */
      Print the input request
      if ( day > DAYLIMIT )
        Print error
      else if ( month > MONTHLIMIT )
        Print error
      else if ( year > YEARLIMIT )
        Print error
        /* Date is correct, bail */
    Of course you may need to test differently, but it's the same idea.

    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2002
    only puts the first 2 numbers in the integer

  4. #4
    Registered User
    Join Date
    Oct 2001
    I understand, that but its not that the code is wrong, it reasks to enter the date if its wrong, thats fine. I have a screen function which gets messed up when the user enters too many digits.

    What do I do?

  5. #5
    Registered User
    Join Date
    Aug 2001
    Hi, enter date as a string..character at a time..
    printf("Enter date in format DDMMYY");
    for(i = 0; i < 6; i++)
         if( i < 2) dd[i] = getch();
         else if (i < 4) mm[i] = getch();
         else  yy[i] = getch();
    /* don't forget to add null chars */
    good thing is any validation that is required can be done as user enters date

    /* in the loop */
       if(i < 2) {
            dd[i] = getch();
                printf("Invalid entry - try again");
        /* rest of code */
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. Replies: 9
    Last Post: 03-17-2006, 12:44 PM
  4. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM