C++ question

Discussion in 'OT Technology' started by NEp8ntballer, Apr 22, 2007.

  1. NEp8ntballer

    NEp8ntballer New Member

    Joined:
    Mar 22, 2006
    Messages:
    2,347
    Likes Received:
    0
    How would I do data validation to see if a usere's input was lacking a charachter. In this case it is to see if there is a colon between the hours and minutes. What a user does is they enter the hours then the minutes separated by a colon. how the hell am I to make sure that they include the colon?

    here's the code if it'll help:

    #include <iostream>
    using namespace std;
    #include "Time.h"

    void display ( Time object, char format );
    void addminute ( Time &object );

    int main ()
    {
    Time clock, alarm;
    int hr, mn;
    char colon;
    int minutes;
    char format;

    cout << "Enter the clock time in HH:MM > " << flush;
    cin >> hr >> colon >> mn;
    clock.setTime (hr, min, 0);

    return 0;
    }

    I apologize if this is breaking the rules. I am not asking for a person to do my homework seeing as how there are other problems still needing to be addressed at this time. All I am asking is for a person to outline how to accomplish what I need to do.
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    welp if you wanna use some libraries youre going to have to read it in as a char* and then theres plenty of stuff to use in the string.h library. you can loop through the array first off makes sure its only 5 characters long and then make sure that the first two are integers (theres a function for that like isInt or something I forget look at the documentation) then make sure taht the 3 character is a ":" and then the second two are integers. parsing in C++/C is generally viewed as a bitch at first but i write stuff to do it all the time and its pretty quick once you get used to it
     
  3. Lexical Unit

    Lexical Unit New Member

    Joined:
    Jul 9, 2005
    Messages:
    6
    Likes Received:
    0
    Never leave home without it: http://cppreference.com

    If you're on *nix, use regex.
    Code:
    #include <iostream>
    #include <sstream>
    #include <regex.h>
    
    using namespace std;
    
    int main ()
    {
        regex_t re;
        regcomp (&re, "^[0-9]{2}:[0-9]{2}$", REG_EXTENDED);
    
        string userin;
        do {
            cout << "Enter the clock time in HH:MM > " << flush;
            getline (cin, userin);
        } while (regexec (&re, userin.c_str (), size_t (0), NULL, 0));
        
        stringstream ss;
        userin.replace (2, 1, " ");
        ss << userin;
        int hr, mn;
        ss >> hr >> mn;
    
        cout << "Got " << hr << " hours and " << mn << " minutes." << endl;
    
        return 0;
    }
    
    Or better yet since you are using C++, If you've got boost installed, see Boost.Regex. It should be similar to the regex.h library.

    If you want to do it the hard way (ie: hand roll it), do something like this:
    Code:
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    /*
        Matcher takes a pattern where # represents a single character integer
        and anything else is just represents what it actually is.  We only
        match if the input string is the same size as the pattern and they
        match exactly.
    */
    class Matcher
    {
        public:
            Matcher(const string pattern);
            const bool operator()(const string in) const;
    
        protected:
            string _pattern;
    };
    
    Matcher::Matcher(const string pattern) : _pattern(pattern) { }
    const bool Matcher::operator()(const string in) const {
        static const string number = "0123456789";
    
        if (_pattern.length () != in.length ()) {
            return false;
        }
    
        for (int i = 0; i < in.length (); ++i) {
            if (_pattern.at (i) == '#') {
                if (number.find (in.at (i)) == string::npos) {
                    return false;
                }
            } else {
                if (in.at (i) != _pattern.at (i)) {
                    return false;
                }
            }
        }
       
        return true;
    }
    
    int main ()
    {
    
        string userin;
        Matcher m ("##:##");
        do {
            cout << "Enter the clock time in HH:MM > " << flush;
            getline (cin, userin);
        } while (!m (userin));
       
        stringstream ss;
        userin.replace (2, 1, " ");
        ss << userin;
        int hr, mn;
        ss >> hr >> mn;
    
        cout << "Got " << hr << " hours and " << mn << " minutes." << endl;
    
        return 0;
    }
    
    Enjoy and be merry.
     
  4. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    It would be better to provide an interface that automatically inserts the colon. That way the user doesn't need to service the program on the program's terms.
     

Share This Page