Help! (updated code)Using Functions and bool to add hours and minutes

I have the following Function Prototypes:

1
2
3
4
5
6
bool validMins (/*not sure what goes here*/);
bool validHours (/*not sure what goes here*/);
const int MAX_MINS = 59;
const int MAX_HRS = 23;
const int MIN_MINS = 0;
const int MIN_HOURS = 0;


I HAVE to use the above prototypes to create function definitions. I just learned how to use functions and how to pass values or references. Now this input validation where I would probably need if else statements, along with bool flags is throwing me off.

Can anyone please show me how to do this given the Prototypes shown above? Thank you very much!
Last edited on
It seems that you want to determine if the value given is within a certain range. If that is the case then maybe something like this:

bool vaildMins(int Time); //passed in time in minutes
bool vaildHours(int Time); //passed in time in hours

then the routine would be to check to see if the passed in variable is less then the max time and more then the minimum time. if it is then return true, otherwise return false.
Thanks pogrady.

Now, how should the function definition look like? How do I incorporate the prototypes that have the constants?
Hi, try this:
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
#include <iostream> // cout, cin

// function prototypes
bool validMins (int mins);
bool validHours (int hours);

// constants
const int MAX_MINS = 59;
const int MAX_HRS = 23;
const int MIN_MINS = 0;
const int MIN_HRS = 0;

int main(void)
{
    int m,h;
    std::cout << "Enter mins:";
    std::cin >> m;
    std::cout << "Enter hours:";
    std::cin >> h;

    if ( validMins(m) && validHours(h) )
        std::cout << "Valid time\n";
    else
        std::cout << "Err: Invalid time!\n";

    return 0;
}

bool validMins(int mins)
{
    if (mins >= MIN_MINS && mins <= MAX_MINS)
        return true;

    return false;
}

bool validHours(int hours)
{
    if (hours >= MIN_HRS && hours <= MAX_HRS)
        return true;

    return false;
}
Last edited on
The functions can be written simpler

1
2
3
4
5
6
7
8
9
bool validMins( int mins )
{
    return (  MIN_MINS <= mins && mins <= MAX_MINS );
}

bool validHours( int hours )
{
    return (  MIN_HRS <= hours && hours <= MAX_HRS );
}

Thanks guys! I'm confused about constant int's above main. I didn't know how to incorporate them into the function definitions. So looking at your examples above, they are being passed in the function definitions.
So looking at your examples above, they are being passed in the function definitions.

No, they aren't. They are global, so each function can use them.
I will share what I have so far. Everything in main is given...I cannot alter or add anything in it. Same thing for the function prototypes...I cannot add or alter anything except I need to fill in the empty parameters of indicated prototypes. I'm just learning fucntions so this project is pretty confusing for me and I appreciate all the patience and help I've received from you folks.

Up until now I've only revealed fragments of my code, only because I want to figure things out on my own. Apparently I can't even get by with the examples provided. To avoid further confusion (definitely on my part!), I'm just gonna share what I have so far...

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
#include <iostream>
#include <string>

using namespace std;

void getHours (int &, const string &);
void getMinutes (int &, const string &);

void totalTime ( ); //This requires the calculation.  I need to fill in this parameter.

bool validMinutes ( ); //I need to fill in this parameter
bool validHours( ); //I need to fill in this parameter

const int MAX_MINS = 59;
const int MAX_HOURS = 23;
const int MIN_MINS = 0;
const int MIN_HOURS = 0;

int main ()
{
    int hours, minutes, addedHours, addedMinutes;
    getHours (hours, "Enter number of hours for starting time: ");
    getMinutes (minutes, "Enter number of minutes for starting time: ");
    getHours (addedHours, "Enter number of hours to be added to starting time: ");
    getMinutes (addedMinutes,  "Enter number of minutes to be added to starting time: ");
    TotalTime (hours, minutes, addedHours, addedMinutes);
	cout << "The total time is " << hours << " hours and " << minutes << " minutes." << endl;
    return 0;
}
void getHours (int &hours, const string &prompt)
{
	cout << prompt;
	cin >> hours;
}
void getMinutes (int &minutes, const string &prompt)
{
	cout << prompt;
	cin >> minutes;
}


As you folks can see, I'm stuck on calculation and bool portions.

Bambanos and Vlad, I will now study your examples further. Thank you all so much again!
Last edited on
Thanks again for all the help. Here's my code including the calculations and bool flags. I'm trying this now with a function for the calculation of the total time, therefore the addition of: void totalTime (int &, int &, int &, int &);. It compiles, but the calculation is not adding properly.

What is wrong with the totalTime parameters and the if statement structures? I bet there are more errors...please enlighten!

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
#include <iostream>
#include <string>

using namespace std;

void getHours (int &, const string &);
void getMinutes (int &, const string &);
void totalTime (int &, int &, int &, int &);
bool validMinutes (int mins); 
bool validHours(int hours); 
const int MAX_MINS = 59;
const int MAX_HOURS = 23;
const int MIN_MINS = 0;
const int MIN_HOURS = 0;

int main ()
{
    int hours, minutes, addedHours, addedMinutes;
    getHours (hours, "Enter number of hours for starting time: ");
    getMinutes (minutes, "Enter number of minutes for starting time: ");
    getHours (addedHours, "Enter number of hours to be added to starting time: ");
    getMinutes (addedMinutes,  "Enter number of minutes to be added to starting time: ");
    totalTime (hours, minutes, addedHours, addedMinutes);
   cout << "The total time is " << hours << " hours and " << minutes << " minutes." << endl;
    return 0;
}
void getHours (int &hours, const string &prompt)
{
   cout << prompt;
   cin >> hours;
}
void getMinutes (int &minutes, const string &prompt)
{
   cout << prompt;
   cin >> minutes;
}
void totalTime (int &hours, int &minutes, int &addedHours, int &addedMinutes)
{
	hours = hours + addedHours;
	minutes = minutes +addedMinutes;
}
bool validMins(int mins)
{
    if (mins >= MIN_MINS && mins <= MAX_MINS)
        return true;

    return false;
}
bool validHours(int hours)
{
    if (hours >= MIN_HOURS && hours <= MAX_HOURS)
        return true;

    return false;
}
Can anybody please show me the correct parameters for the totalTime prototype and the correct function bodies. I'm totally stumped. Please!!!
Before you move on to totalTime, you might want to actually validate the input. Once you've done that you might consider what happens in totalTime when you add 1 minute to 59 minutes.
Last edited on
If this means rearranging the order of the function calls, I cannot do so. This excercise does not allow alteration of anyything in main. It stays as it is given. Same thing with the prototypes (except their parameters).

LOL. I think I just need to see the final code for this thing and just go back to the books and go back to basics.
Last edited on
Topic archived. No new replies allowed.