Thread: Validity Check.

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    Exclamation Validity Check.

    Greeting,

    I wonder how can I improve my validity check for user input? I only want them to key in certain range of digit.

    Also for my validity check, when I key in character such as ABC, it lead to infinity loop

    Here is my code :
    Code:
    /*Write a program that can calculate user's age by getting user input their birth date.*/
    
    #include "stdafx.h"
    #include <iostream>
    #include <time.h>
    
    
    using namespace std;
    
    
    int main()
    {
    	int yyyy, mm, dd; //year, month, day
    	int i = 0; //for the sake of validity check
    
    
    	// current date/time based on current system
    	time_t t = time(0);
    	tm *now = localtime(&t);
    
    
    	/* Find a better way to validate user input
    	 * "isDigit" should able to do the trick
    	 * Bug : Key in character lead to infinity loop
    	*/
    	//Year
    	do
    	{
    		cout << "Enter your birth date of Year:\n?";
    		cin >> yyyy; //do checking
    		if (yyyy > 1900 && yyyy < 2500) { //2500..? Lol
    			i++; //i=1
    		}
    	} while(i == 0);
    
    
    	//Month
    	do
    	{
    		cout << "Enter your birth date of Month:\n?";
    		cin >> mm; //do checking
    		if (mm > 0 && mm < 13) { // 1 ~ 12
    			i++; //i=2
    	}
    	} while(i == 1);
    
    
    	//Day
    	do
    	{
    		cout << "Enter your birth date of Date:\n?";
    		cin >> dd; //do checking
    		if (dd > 0 && dd < 32) { //I'm kind of lazy to check how many days in a month, well, 1 ~ 31
    			i++; //i=3
    	}
    	} while(i == 2);
    	/*End of user input*/
    
    
    	//Think a way to improve this so those comment will not needed
    	if (mm <= (now->tm_mon + 1)) { //if birthday month <= current month
    		if (dd <= (now->tm_mday)) { //if birthday date <= current date
    			cout << "Your age is: " << (1900 + now->tm_year) - yyyy << "years,"; //Already pass birthday
    			cout << " " << ((now->tm_mon + 1) - mm) << "months,"; //1 has added into years
    			cout << " and " << ((now->tm_mday) - dd) << "days" << endl; //and so the day
    		} else {
    			cout << "Your age is : " << (1900 + now->tm_year) - yyyy - 1 << "years,"; //Not yet pass birthday
    			cout << " " << ((now->tm_mon + 1)) << "months,"; //just print how many months
    			cout << " and " << ((now->tm_mday)) << "days" << endl; //and day you lived
    		}
    	} else {
    		cout << "Your age is : " << (1900 + now->tm_year) - yyyy - 1 << "years,"; //Not yet pass birthday
    		cout << " " << ((now->tm_mon + 1)) << "months,"; //just print how many months
    		cout << " and " << ((now->tm_mday)) << "days" << endl; //and day you lived
    	}
    }
    Regards,
    Milo.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look up the standard string class, create a std::string named (say) some_string, and then use std::getline(std::cin, some_string) to read a complete line of input. Check/parse the contents of some_string BEFORE trying to interpret it as a set of integers.


    Alternatively, you could also check cin.fail(). That function will return true if an error occurs for the streaming operator (e.g. a character entered that cannot be interpreted as an int). You will then need to clear the failbit (cin.clear()), and then gobble a character from cin before attempting to use the streaming operator again. Bear in mind there are other errors that may affect a stream (eg end of file or loss of integrity of the stream).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    12
    Quote Originally Posted by grumpy View Post
    Look up the standard string class, create a std::string named (say) some_string, and then use std::getline(std::cin, some_string) to read a complete line of input. Check/parse the contents of some_string BEFORE trying to interpret it as a set of integers.


    Alternatively, you could also check cin.fail(). That function will return true if an error occurs for the streaming operator (e.g. a character entered that cannot be interpreted as an int). You will then need to clear the failbit (cin.clear()), and then gobble a character from cin before attempting to use the streaming operator again. Bear in mind there are other errors that may affect a stream (eg end of file or loss of integrity of the stream).
    Hi, thanks for your reply!
    To be honest I still very fresh in C++ as I just started a few days ago, I'm sorry that I can't really understand what do you mean

    Can you provide me some example code and do some further explanation on that?

    Or is there any more simple way of doing the trick?

    Thanks!

    Regards,
    Milo.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No, I'm not going to provide sample code. You'll learn nothing that way. You'll learn more by reasoning out an answer for yourself (don't worry - it's not hard to pick up).

    Some hints on things to look up, in order to get you started.

    C++'s string class.
    The getline() function. This can be used to read data from a stream (like cin) into a string.

    Once you have a string, check its contents. (The documentation for the string class will tell you how to examine or change individual characters in the string).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 09-25-2012, 02:57 PM
  2. Check for validity of IP address
    By krishnampkkm in forum Linux Programming
    Replies: 1
    Last Post: 03-25-2010, 05:38 AM
  3. Help! with character validity check
    By Tdankert in forum C Programming
    Replies: 19
    Last Post: 07-30-2007, 08:41 PM
  4. Pointer validity check
    By Carlos in forum Windows Programming
    Replies: 6
    Last Post: 12-11-2003, 03:40 AM