Solved

Time class Addition

Posted on 2004-04-06
3
271 Views
Last Modified: 2010-04-01
Need help in the correction of this code for addition Time class.  When I try to compile, compiler states Time, time1, time2, sumTime undeclared.  Line 8-10 on add_time_3.cpp.  Looking for corrected working code.  Please advise.  Del

#ifndef TIME3_H
#define TIME3_H

class Time {

public:

   Time();
   void inputTime();
   void normTime();
   Time addTime(const Time &);
   void printTime();

private:

   int week;
   int day;
   int hour;
   int minute;

};

#endif

// time3.cpp

#include <iostream>

using namespace std;

#include "time3.h"

void Time::inputTime() {

                char temp[15];
            cout << "Enter time value: " << endl;
                cin >> week >> temp >> day >> temp >> hour >>temp >> minute >> temp;

}

void Time::normTime() {

                while(minute >= 60) {
                           hour ++;
               minute = minute - 60;

               }

                while(hour >= 24) {
                           day ++;
                hour = hour - 24;

                }

                while(day >= 7) {
                          week ++;
                day = day - 7;

                }

}

Time Time::addTime(const Time &time) {
            
            Time result;
                result.week = week + time.week;
                result.day = day + time.day;
                result.hour = hour + time.hour;
                result.minute = minute + time.minute;

            return result;

}

void Time::printTime() {

                cout << week << " Weeks " << day << " Days " << hour << " Hours " << minute
                       << " Minutes" << endl;

} // end of time3.cpp

// add_time_3.cpp

#include "time3.h"

int main()
{
      Time.time1, time2, sumTime;
      time1.inputTime();
      //time1.normTime();
      //time1.printTime();

      time2.inputTime();
      //time2.normTime();
      //time2.printTime();
      
      sumTime = time1.addTime(time2);
      sumTime.normTime();
      sumTime.printTime();
      
      return 0;

} // end of main

#endif






















































0
Comment
Question by:edelossantos
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 500 total points
ID: 10770498
>Time.time1, time2, sumTime;

Removed the . between Time and time1 like

Time time1, time2, sumTime;
0
 

Author Comment

by:edelossantos
ID: 10770708
I Removed the . between Time and time1 like

Time time1, time2, sumTime;

Now I am getting a user bin unresolved, Time::Time(void)
0
 
LVL 23

Expert Comment

by:chensu
ID: 10770805
You need to define the constructor in time3.cpp as follows.

Time::Time()
{
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now