Hey Guys,

I'm trying to create a timer program that is based on the MVC model and I'm having trouble with updating the views via notify() function as they should display the number of seconds or minutes and seconds elapsed. Could anyone help me with this problem?

//KeyboardController.h
class KeyboardController: public Subject {
public:
    void start(){
        Timer timer(0, true);
        MinSecView minSecView(&timer);
        SecView secView(&timer);
        TickView tickView(&timer);

        while(std::cin >> cmd_){
            if(cmd_ == "start"){
                notify();
                timer.start();
            }
            else if(cmd_ == "stop"){
                notify();
                timer.stop();
            }
            else if(cmd_ == "reset"){
                notify();
                timer.reset();
            }
            else if (cmd_ == "exit"){
                exit(1);
            }
        }
    }
    std::string getCommand() const {
        return cmd_;
    }
private:
    std::string cmd_;
};



//Subject.cpp
void Subject::subscribe(Observer* obs){
    observers_.push_back(obs);
}
void Subject::unsubscribe(Observer* obs){
    list<Observer *>::iterator it;
    for(it = observers_.begin(); it != observers_.end(); ++it){
        if(*it == obs){
            observers_.erase(it);
        }
    }
}
void Subject::notify(){
    list<Observer *>::iterator it;
    for(it = observers_.begin(); it != observers_.end(); ++it){
        (*it)->update(this);
    }
}



//TimerView.cpp (public Observer)
TimerView::TimerView(Timer *timer){
    timer_ = timer;
    timer_->subscribe(this);
}
void TimerView::update(Subject *s){
    if(s == timer_){
        display(cout);
    }
}
void TimerView::display(ostream& os) const {
    os << timer_ << endl;
}
This article has been dead for over six months. Start a new discussion instead.