1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Complete Data Validation

Discussion in 'C' started by Suzanne, Feb 6, 2010.

  1. Suzanne

    Suzanne New Member

    Joined:
    Feb 6, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Asia
    I have a major problem. I need to do complete data validation for the given code & I dun know how to. Could someone help me validate?I would be really grateful. Tks.
    Code:
    #include <iostream> 
    #include <iomanip> 
    #include <fstream> 
    #include <sstream> 
    #include <ctime> 
    #include <vector> 
    #include <algorithm> 
    #include <cstdlib> 
     
    using namespace std; 
     
    class Movie 
    { 
        friend istream& operator>>(istream&, const Movie&); 
        friend ostream& operator<<(ostream&, const Movie&); 
         
        public: 
            void setVector(string&, string&, string&, string&, string&, string&, int&); 
            void setInfo(string, string, string, string, string, string, int); 
            string getTitle(const Movie&); 
            string getDirector(const Movie&); 
            string getRelDate(const Movie&); 
            string getGenre(const Movie&); 
            string getCast(const Movie&); 
            string getSyn(const Movie&); 
            int getRuntime(const Movie&); 
        //private: 
            string title; 
            string director; 
            string reldate; 
            string genre; 
            string cast; 
            string synopsis; 
            int runtime; 
    }; 
     
    ostream& operator<<(ostream& out, const Movie& mv) 
    { 
        out << mv.title << "|" << mv.director << "|" << mv.reldate << "|" 
            << mv.genre << "|" << mv.cast << "|" << mv.synopsis << "|" << mv.runtime; 
         
        return out;     
    } 
     
    void Movie::setVector(string& t, string& d, string& rdate, string& g, string& c, string& s, int& run) 
    { 
        title = t; 
        director = d; 
        reldate = rdate; 
        genre = g; 
        cast = c; 
        synopsis = s; 
        runtime = run; 
    } 
     
    void Movie::setInfo(string t, string d, string rdate, string g, string c, string s, int run) 
    { 
        title = t; 
        director = d; 
        reldate = rdate; 
        genre = g; 
        cast = c; 
        synopsis = s; 
        runtime = run; 
    } 
     
    string Movie::getTitle(const Movie& rhs) 
    { 
        return title; 
    } 
    string Movie::getDirector(const Movie& rhs) 
    { 
        return director; 
    } 
    string Movie::getRelDate(const Movie& rhs) 
    { 
        return reldate; 
    } 
    string Movie::getGenre(const Movie& rhs) 
    { 
        return genre; 
    } 
    string Movie::getCast(const Movie& rhs) 
    { 
        return cast; 
    } 
    string Movie::getSyn(const Movie& rhs) 
    { 
        return synopsis; 
    } 
    int Movie::getRuntime(const Movie& rhs) 
    { 
        return rhs.runtime; 
    } 
         
    class Dbase 
    { 
        public: 
            void add(); 
            void remove(); 
            void search(); 
            void summary(); 
            void infile(); 
             
        private: 
            bool compare(const Movie&); 
            void outfile(); 
            Movie mov; 
            vector<Movie> m; 
            vector<Movie>::iterator it; 
    }; 
     
    bool Dbase::compare(const Movie& rhs) 
    { 
        it = m.begin(); 
        cout << "Checking movie in database.." << endl; 
         
        for (int i = 0; i < m.size(); i++) 
        { 
            //cout << m[i].title << " " << m[i].reldate << endl; 
            //cout << rhs.title << " " << rhs.reldate << endl << endl; 
             
            if ((m[i].title == rhs.title) && (m[i].reldate == rhs.reldate)) 
            { 
                cout << "Movie data found!!" << endl; 
                return true; 
            } 
            it++; 
        } 
    } 
     
    void Dbase::infile() 
    { 
        ifstream fin; 
        string s, z; 
        string temp[7]; 
        int i, rt; 
        fin.open("movdb.txt"); 
        if (!fin.good()) 
            cout << "Input file corrupted!!" << endl; 
        else 
        { 
            while (getline(fin, s)) 
            { 
                stringstream ss(s); 
                i = 0; 
                while (getline(ss, z, '|')) 
                { 
                    temp[i] = z; 
                    ++i; 
                } 
                rt = atoi( temp[6].c_str() ); 
                mov.setVector(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],rt); 
                m.push_back(mov); 
            } 
        }     
    } 
     
    void Dbase::outfile() 
    { 
        ofstream fout; 
        fout.open("movdb.txt"); 
         
        if (!fout.good()) 
            cout << "Output file corrupted!!" << endl; 
        else 
            for (int i = 0; i < m.size(); i++) 
                fout << m[i] << endl; 
         
        fout.close(); 
    } 
         
    void Dbase::add() 
    { 
         
        if (compare(mov) == true) 
        { 
           cout << "Duplicate data was not added.." << endl;  
        } 
        else 
        { 
            m.push_back(mov); 
            cout << "A movie data has been added successfully!!" << endl; 
        } 
        outfile(); 
    } 
     
    void Dbase::remove() 
    { 
        char rmv; 
        string a; 
         
        cout << "Please enter movie data data that you want to remove.." << endl; 
        cin.ignore(1000, '\n'); 
        cout << "Title: "; 
        getline(cin, mov.title); 
        //system("pause"); 
        //cin.ignore(1000, '\n'); 
        cout << "Release Date: "; 
        getline(cin, mov.reldate); 
         
        if (compare(mov) == true) 
        { 
            while ((rmv != 'Y') && (rmv !='y') && (rmv != 'N') && (rmv != 'n')) 
            { 
                cout << "Confirm to remove movie data?? (Y/N): "; 
                cin >> rmv; 
            } 
            if (rmv == 'Y' || rmv == 'y') 
            { 
                m.erase(it); 
            } 
        } 
        else 
            cout << "Movie not found!!" << endl; 
    } 
     
    void Dbase::search() 
    {
       int valueM; string store;
       cout << "Please choose a search option 1.Title 2.Director 3.Release 4.Casts 5.Genre 6.Runtime";
       cin >> valueM;
      while(valueM > 0 && valueM < 7)
      {
       switch(valueM)
        {
         case1:  it = search(m[valueM].begin(),m[valueM].end()) 
                 cout << "Please provide movie title: " <<endl;
                 cin >> store;
                 while(it != m[valueM].end())
                 if(store == 
                 it++;
                 cout << endl; 
    
        
    } 
     
    void Dbase::summary() 
    { 
        cout << setw(30) << left << "Title" << setw(25) << "Director" << setw(20) << "Release Date" 
             << setw(30) << "Casts" << setw(15) << "Genre" << setw(10) << right << "Runtime" << endl; 
         
        for (int i = 0; i < m.size(); ++i) 
            cout << m[i] << endl; 
    } 
     
    class MovieManager 
    { 
        public: 
            MovieManager(); 
            void mainMenu(); 
            void optionCase(int); 
             
        private: 
            int choice; 
            Dbase db; 
    }; 
     
    MovieManager::MovieManager() 
    { 
        while (choice != 5)  
            mainMenu(); 
    } 
     
    void MovieManager::mainMenu() 
    { 
        cout << "===========================" << endl; 
        cout << "     Main Menu option:     " << endl; 
        cout << "---------------------------" << endl; 
        cout << "1. Add" << endl; 
        cout << "2. Remove" << endl; 
        cout << "3. Search" << endl; 
        cout << "4. Summary" << endl; 
        cout << "5. Quit" << endl << endl;   
        cout << "Enter your selection: "; 
         
        db.infile(); 
         
        cin >> choice; 
        optionCase(choice); 
     
    } 
     
    void MovieManager::optionCase(int choice) 
    { 
        switch (choice) 
        { 
            case 1: 
                db.add(); 
                break; 
                 
            case 2: 
                db.remove(); 
                break; 
                 
            case 3: 
                db.search(); 
                break; 
                 
            case 4: 
                db.summary(); 
                break; 
            case 5: 
                //exit(0); 
                break; 
        } 
    } 
     
    int main() 
    {    
        MovieManager i; 
        cout << "I'm done" << endl; 
        system("pause"); 
        return 0; 
    }
     
    Last edited by a moderator: Feb 7, 2010
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Validating means checking for errors and, optionally, not allowing the user to continue if the data is not correct. So if the user has to enter a positive integer between 5 and 10, and they enter -27, or "hello", then both those are wrong. How do you think you might do that? (Hint: what are the "decision making" statements?)
     

Share This Page