#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    48
    Rep Power
    12

    Error checking function


    Hi. i am writing a program that accepts 4 characters (cin>>a>>b>>c>>d) and then decodes the characters into a date format (integers). my problem is that after the characters are entered, i want to check to make sure that the person didn't enter any non-characters (ex. !@#$%1234...etc). i can't seem to get this to work because all of the characters have to be entered at one time. if their code is not correct, i want it to repeat the loop again. anyway, here is my code....

    #include <iostream.h>
    #include <conio.h>
    #include <ctype.h>

    void main()
    {
    char chm,chfd,chsd,chy; //character values for month, first and second date digits and year
    char answer; //declares answer as type char
    int im,ifd,isd,iy; //integer values for characters

    do
    {
    cout<<"Enter the four digit alphanumeric code (ex. AQQA) followed by ENTER: ";
    cin>>chm>>chfd>>chsd>>chy;
    chm = toupper(chm);
    chfd = toupper(chfd);
    chsd = toupper(chsd);
    chy = toupper(chy);
    cout<<"You entered the following alphanumeric code: "<<chm<<chfd<<chsd<<chy<<endl;
    cout<<"Is this correct? (Y/N)";
    cin>>answer;
    answer = toupper(answer);
    }while (answer == 'N');

    im = int(chm) - 64; //calculates month value
    ifd = int(chfd) - 81; //calculates first number in date
    isd = int(chsd) - 81; //calculates second number in date
    iy= ((int(chy) - 64) + 1990); //calculates year

    if (ifd == 0 && isd == 0) //if the first and second digits of date are '0'
    {
    isd += 1; //increment second digit
    }

    if (im == 2 && ifd >= 2) //checks for 2nd month and higher than twenty days
    {
    if (isd == 9 || isd == 0 || isd == 1) //checks for higher than twenty eight days
    {
    ifd = 2;
    isd = 8; //changes values to 28 days in feb.
    }
    }
    cout<<"Your encrypted code produced the following date: "<<endl;
    cout<< (im < 10 ? "0" : "")<<im<<"/"<<ifd<<isd<<"/"<<iy<<endl; //put '0' in front of months before 10

    } //end of main function
    Last edited by webblynx; September 6th, 2003 at 09:59 PM.
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    http://www.qnx.com/developer/docs/qn...i/isalpha.html

    this function should help you out.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    48
    Rep Power
    12
    i tried using isalpha, but if it returns false, then how do i go to the beginning of my:

    do
    {
    //stuff
    }while (loop);

    ?
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Code:
    if( !(isalpha(x)))
          continue;
    continue restarts the loop.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    48
    Rep Power
    12
    [QUOTE]Originally posted by infamous41md
    [B]
    Code:
    if( !(isalpha(x)))
          continue;
    that seems to work very well thanks..

    ..but what if you only enter 2 or 3 digits, instead of 4? if i enter AQA in the program it just hangs.

    is there a way to check to num of cin>>?
  10. #6
  11. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    I think the easiest way for you to achieve this would be to read in a string and then parse it to check that the format is what you're after. I don't know how experienced you are but typically you might write a function to do this and call the function from within main(). Does that make sense?

    So you could read in the string then check that:

    [1] You have 4 characters entered;
    [2] Each one is a letter;

    and so on. If everything is OK, then fine, if not then repeat until it parses correctly.

IMN logo majestic logo threadwatch logo seochat tools logo