Help with set/get functions!

Okay this is for an assignment that I have gotten a decent start on. I am confused in a few sections but mostly with "if" statements and get/ set functions. I have to make a class program that has hours minutes and seconds displayed all as two digit numbers. This is the instructions that I am most stuck on "

• Ensure that the hour value is in the range 0 – 23; if it is not, set the hour to 12.
• Ensure that the minute value is in the range 0 – 59; if it is not, set the minute to 0. • Ensure that the second value is in the range 0 – 59; if it is not, set the second to 0. Provide a set and get function for each data member."

I think my code while not perfect will work I am stuck on the IF statements that have no body in my code. If you have any advice for me let me know!

also confused on how to make the time be displayed as 00:00:00, all i can get is 0:0:0.

I appreciate any help you guys have for me thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 #include <iostream>

using namespace std;

class time
{
    private:
    int hr;
    int min;
    int sec;
    
    public:
    time(int h, int m, int s);
    
    
};

time::time(int h, int m, int s)
{
    hr = h;
    min = m;
    sec = s;
}
int main()
{
    cout << " Please enter each integer as two digits." << endl;
    
    
    cout << " Enter hours:";
    int h;
    cin>> h;
    
    cout << " Enter minutes";
    int m;
    cin>> m;
    
    cout << "Enter Seconds";
    int s;
    cin>> s;
    
    if((h >= 24 || h < 0))
   
    
    if((m >=59 || m <0))
   
    if ((s >= 59 || s <0))
    
    


 cout <<h << ":" << m << ":" << s << "." << endl;
    
    
    return 0;
    
}
Hello jaredv11,

Welcome to the forum.

The class definition is fine although I would choose a name other than "time" to avoid any possible conflices with "time" that is used in other header files.

In main you input the hours, minutes and seconds, but never create n instance of the class to hold these values.

A set function like:
1
2
3
4
time::SetHour(int hour)
{
    hr = hour;
}

Or you could adjust this to set all three variables. The "set" functions are mainly used after an instance of the class has been made when you need to either set or change a value.

The opposite is the "Get" function, not that hard, and can be written as simple as:
int time::getHour() { return hr; }.

The if statements may be useful at some point, but I think you could do without them.

Work something up and post the new code.

Hope that helps,

Andy

Hello jaredv11,

I forgot to mention. You have defined your variables as ints. These do not store leading zeros, so you will need to take care of this when you output to the screen.

Andy
Hi Handy Andy,

Thank you for your response! I am currently working on it, I have a question about storing my variables as ints. I don't think I've been taught any other way at this point. Im not sure if i know how to change the way i out put the time to save leading variables. If I hold the inputed values will then save leading 0s if they are apart of the input?


Thanks
Hello jaredv11,

"int"s are fine to store the parts of time. "int"s along with any other type of variable, e.g., short, unsigned int, float, best not to use, double, better than a float, or long do not store leading zeros. If you enter a leading zero that will work, but it will be dropped when stored. The only way to keep a leading zero is to input to a string, but if you need to do any type of calculation you will have to change it back to a numeric variable. That is more work that you need to do.

It looks like you would understand an if/else statement better.
1
2
3
4
if (h < 10)
	std::cout << '0' << timeClass.GetHour() << ':';
else
	std::cout << timeClass.GetHour() << ':';


The same format for minutes and seconds.

I defined my instance of the class as "timeClass" just to stay away form "time".

Hope that helps,

Andy
Andy,

Thank you so much for you help. I finally finished the project, I ended up using "while" statements to keep the variables within the parameters.

However, my directions wanted me to use a constructor to keep the variables in the usable range if someone could help me build a constructor or point me in the right direction that would be much appreciated.

Heres where I ended up.


#include <iostream>
#include <fstream>
using namespace std;

class Time
{
private:
int hr;
int min;
int sec;

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

};

Time::Time(int h, int m, int s)
{
hr = h;
min = m;
sec = s;
}
int main()
{



signed int h;
signed int s;
signed int m;





cout << " Please enter each integer as two digits." << endl;


cout << " Enter hours:";

cin>> h;

cout << " Enter minutes:";

cin>> m;

cout << "Enter Seconds:";

cin>> s;

while (h < 0 || h > 23)
{
cout << h << " are not valid hours.\n";
cout << "Please enter hours 0-23: ";
cin >> h;
}
while (m < 0 || m > 59)
{
cout << m << " are not valid minutes.\n";
cout << "Please enter minutes 0-59: ";
cin >> m;
}
while (s < 0 || s > 59)
{
cout << s << " are not valid seconds.\n";
cout << "Please enter seconds 0-59: ";
cin >> s;
}

if (h < 10)

std::cout << '0' << h << ':';
else
std::cout << h << ':';

if (m < 10)
std::cout << '0' << m << ':';
else
std::cout << m << ':';

if (s < 10)
std::cout << '0' << s << ':';
else
std::cout << s << ':';





return 0;

}


@ jaredv11, yes you had a great start but you seem to ignore some important aspect(s) of the program specification.

Your class lacks
Provide a set and get function for each data member.

and the controls
1
2
3
Ensure that the hour value is in the range 0 – 23; if it is not, set the hour to 12.
Ensure that the minute value is in the range 0 – 59; if it is not, set the minute to 0. 
Ensure that the second value is in the range 0 – 59; if it is not, set the second to 0


should be done inside the class.

Complete this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include <string> 

using namespace std;

class Time
{
private:
    int hr;
    int min;
    int sec;
    
public:
    Time(); // nice to have this too
    Time(const int &h, const int &m, const int &s);
    
    int getHour() const;
    int getMinutes() const;
    int getSeconds() const;
    
    void setHour(const int &hour);
    void setMinutes(const int &mins);
    void setSeconds(const int &secs);
    
    void showTime();    
};

Time::Time(const int &h, const int &m, const int &s)
{
   /* hr = h;
    min = m;
    sec = s;*/
    
    setHour(h);   // see setHour below. 
    setMinutes(m);
    setSeconds(s);
}

Time::Time(){};

// getters
int Time::getHour() const
{
    return hr;
}

// Do same for minutes and seconds


// Ensure that the hour value is in the range 0 – 23; if it is not, set the hour to 12.
void Time::setHour(const int &hour)
{
    if(hour >= 0 && hour <= 23){
        hr = hour;
    }
    else{
        hr = 12;
    }
}

//  Ensure that the minute value is in the range 0 – 59; if it is not, set the minute to 0
void Time::setMinutes(const int &mins)
{
    // do the control here
}

// Ensure that the second value is in the range 0 – 59; if it is not, set the second to 0
void Time::setSeconds(const int &secs)
{
   // do the control here
}

/*
 if hr, sec and min are < 10, append "0" to it otherwise use it as is
 see http://www.cplusplus.com/reference/string/to_string/ 
*/
void Time::showTime()
{
    string time;
    string h, m, s;

    if(hr < 10){
        h = "0" + to_string(hr);
    }else{
        h = to_string(hr);
    }
    if(min < 10){
        m = "0" + to_string(min);
    }else{
        m = to_string(min);
    }
    if(sec < 10){
        s = "0" + to_string(sec);
    }else{
        s = to_string(sec);
    }
    
   cout << "Time is : " << h << ":" << m << ":" << s << endl;
    
}

int main()
{
    // test show time
    Time t = Time(1,3,4);
    t.showTime();
    
    Time t1 = Time(12,9,34);
    t1.showTime();
    
    Time t2 = Time(-2, -2, 23); 
    t2.showTime();
    
    
    cout << " Please enter each integer as two digits." << endl;
    
    
    cout << " Enter hours: ";
    int h;
    cin>> h;
    
    cout << " Enter minutes: ";
    int m;
    cin>> m;
    
    cout << "Enter Seconds: ";
    int s;
    cin>> s;
    
    Time t3 = Time(h,m,s);
    t3.showTime();
    
    // or using your setters
    Time t4; // this will call Time::Time(){};
    t4.setHour(h);
    t4.setMinutes(m);
    t4.setSeconds(s);
    t4.showTime();
       
    return 0;
 }


If you do all right, your output should be like this


Time is : 01:03:04
Time is : 12:09:34
Time is : 12:00:23
 Please enter each integer as two digits.
 Enter hours: 12
 Enter minutes: -1
Enter Seconds: 12
Time is : 12:00:12
Time is : 12:00:12


Hope that helps
Last edited on
Hello jaredv11,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Watch your use of blank lines. To many makes the code hard to follow.

Here is your code a little easier to follow with some comments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <fstream>

//using namespace std;  // <--- Best not to use.

class Time
{
private:
	int hr;
	int min;
	int sec;

public:
	Time(int h, int m, int s);
        //  <--- Needs Set and Get functions here.
};

Time::Time(int h, int m, int s)
{
	hr = h;
	min = m;
	sec = s;
}

//  <--- Needs Set and Get functions definitions here.

int main()
{
	int h;  // <--- signed is not need. "int" is already a signed type.
	int s;
	int m;

	std::cout << " Please enter each integer as two digits." << std::endl;

	std::cout << " Enter hours:";
	std::cin >> h;

	std::cout << " Enter minutes:";
	std::cin >> m;

	std::cout << "Enter Seconds:";
	std::cin >> s;

	// <--- These while loops are  good start, but in the wrong place. Need to be in the ctor or set functions of
	//      the class. As is each while loop should follow each corresponding "cin".
	while (h < 0 || h > 23)
	{
		std::cout << h << " are not valid hours.\n";
		std::cout << "Please enter hours 0-23: ";
		std::cin >> h;
	}
	while (m < 0 || m > 59)
	{
		std::cout << m << " are not valid minutes.\n";
		std::cout << "Please enter minutes 0-59: ";
		std::cin >> m;
	}
	while (s < 0 || s > 59)
	{
		std::cout << s << " are not valid seconds.\n";
		std::cout << "Please enter seconds 0-59: ";
		std::cin >> s;
	}

	//  <--- These if statements are fine, but you should not be printing from the variables used for input, but the
	//       class variables. This is where the get functions of the class would be used.
	if (h < 10)
		std::std::cout << '0' << h << ':';
	else
		std::std::cout << h << ':';

	if (m < 10)
		std::std::cout << '0' << m << ':';
	else
		std::std::cout << m << ':';

	if (s < 10)
		std::std::cout << '0' << s << ':';
	else
		std::std::cout << s << ':';

	return 0;

}


blongho has a good example that will help you.

Hope that helps,

Andy
You can simplify your "display" by using std::setfill() and std::setw().

1
2
3
4
5
6
7
8
9
void Time::showTime(std::ostream& fout)
{
    fout << std::setfill('0');
    fout << "Time is : " 
           << std::setw(2) << hour << ":"
           << std::setw(2) << minutes << ":"
           << std::setw(2) << seconds << '\n';
    fout << std::setfill(' ');  // Be sure to reset the fill character before returning.
}
Last edited on
Topic archived. No new replies allowed.