Calculate Time

Dec 10, 2014 at 6:19pm
I am finishing up a program that calculates time but there is something about my code that isn't running right. I want the user to input several times and then my add function to add them. I keep getting a improper use of class Time error. Any suggestions or help would be great!

This is what I have so far

#include <iostream>

using namespace std;

class Time

{
private:
int hours;
int minutes;
int seconds;

void adjust_time(); //adjust time according to time

public:
Time();


void add_time(int h, int m, int s); //add amount of total time

void output(); //outputs the time


};

Time::Time() //constructor
{
//hours = h;
//minutes = m;
//seconds = s;

cout << "Plase enter athletes time: " << endl;
cin >> hours >> minutes >> seconds;

//adjust_time();
}

void Time::add_time(int h, int m, int s)
{
hours += h;
minutes += m;
seconds += s;

adjust_time();
}

void Time::adjust_time()
{
while (seconds >= 60)
{
seconds -= 60;
minutes += 1;
}

while (minutes >= 60)
{
minutes -= 60;
hours += 1;
}
}

void Time::output()
{
cout << hours << "hours: " << minutes << "minutes: " << seconds << "seconds: " << endl;
}


int main()
{

Time t;
t.Time();


}



Dec 10, 2014 at 6:46pm
t.Time();

What is that supposed to do? It is not valid syntax and doesn't make sense.
Dec 10, 2014 at 7:23pm
I was trying different things to try and call a function to do something but I now realize I was calling a constructor which doesn't really make sense.

Here is the code I had before I started trying different approaches to get user input.

#include <iostream>

using namespace std;

class Time
{
private:
int hours;
int minutes;
int seconds;

void adjust_time(); //adjust time according to time

public:
Time(int h, int m, int s);


void add_time(int h, int m, int s); //add amount of total time

void output(); //outputs the time


};
Time::Time(int h, int m, int s) //constructor
{
hours = h;
minutes = m;
seconds = s;



adjust_time();
}

void Time::add_time(int h, int m, int s)
{
hours += h;
minutes += m;
seconds += s;

cout << "Plase enter athletes time: " << endl;
cin >> hours >> minutes >> seconds;

adjust_time();
}

void Time::adjust_time()
{
while (seconds >= 60)
{
seconds -= 60;
minutes += 1;
}

while (minutes >= 60)
{
minutes -= 60;
hours += 1;
}
}

void Time::output()
{
cout << hours << "hours: " << minutes << "minutes: " << seconds << "seconds: " << endl;
}


int main()
{



}

Should I make another function to get the users input?

The goal is to get a user to enter the amount of time they spent studying throughout the week by day then add them and get a total time if that helps at all.



Dec 10, 2014 at 8:31pm
Your time class shouldn't know anything about the user and certainly should not be handling user input. You can do the user input in main, or you can do it in separate functions, but it should not be a part of what your time class does.
Topic archived. No new replies allowed.