September 6th, 2003, 08:24 PM
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....
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
cout<<"Enter the four digit alphanumeric code (ex. AQQA) followed by ENTER: ";
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)";
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.
September 6th, 2003, 09:57 PM
September 6th, 2003, 10:00 PM
i tried using isalpha, but if it returns false, then how do i go to the beginning of my:
September 6th, 2003, 10:10 PM
continue restarts the loop.
September 7th, 2003, 08:16 AM
[QUOTE]Originally posted by infamous41md
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>>?
September 7th, 2003, 09:05 AM
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:
 You have 4 characters entered;
 Each one is a letter;
and so on. If everything is OK, then fine, if not then repeat until it parses correctly.