Trouble with boolean and int functions

New to programming. Been searching for help and I'm sure the answer's there but I can't see it because I'm a newbie. Trying to write a program for school. The main function and the prototype names must remain unchanged. My task is to write the functions for the prototypes and have them in the correct order. I really don't know what I'm doing so would love some help.
Not concerned about how the braces show on here, I know they're incorrect.
#include <cstdio>

const int MaxHours = 23;
const int MaxMins = 59;
const int ErrVal = -1;

int getInRange(int minVal, int maxVal);
bool getTime(int &hours, int &minutes);
void displayTime(int hours, int minutes);

int main()
{
int hh = ErrVal;
int mm = ErrVal;
if (getTime(hh, mm)) {
displayTime(hh, mm);
} else {
printf("An invalid time was provided\n");
}
return 0;
}

bool getTime(int &hours, int &minutes){

printf("Please enter the hours between 0 and %d\n", MaxHours);
scanf("%d", &hours);
if (hours < MaxHours){
return hours;
}
else{
return ErrVal;
}

printf("Please enter the minutes between 0 and %d\n", MaxMins);
scanf("%d", &minutes);

if (minutes < MaxMins){
return minutes;
}
else{
return ErrVal;
}
}

int getInRange(int minVal, int maxVal)
{

float userInput;
scanf("%f", &userInput);
if(userInput < minVal){
printf("Error: the value %f is too small\n", userInput);
return ErrVal;
}

if(userInput > maxVal){
printf("Error: the value %f is too big\n", userInput);
return ErrVal;
}
return userInput;
}


void displayTime(int hours, int minutes){

printf("The time you provided was %d : %d.\n", hours, minutes);

}
Last edited on
closed account (48T7M4Gy)
Please use code tags.

Not concerned about how the braces show on here, I know they're incorrect.
Yeah I don't give a sh!t about them either.

Have you run this program?

In general the order doesn't matter.
Last edited on
Do you have a debugger? If you are able to step thru your code in a debugger, it can help you understand how your code is interpretted by the language.

What C++ environment are you using?

1
2
3
4
5
6
7
8
9
10
11
12
13
bool getTime(int &hours, int &minutes)
{
	printf("Please enter the hours between 0 and %d\n", MaxHours);
	scanf("%d", &hours);

	if (hours < MaxHours)
	{
		return hours;
	}
	else
	{
		return ErrVal;
	}
You're trying to validate the value input by the user. It's ok if it's in the range 0 - 23.

A bool function returns true or false, so we can use that return value to return if the function was successful or not, true for ok and false for error. So we could rewrite the check to reflect that.
1
2
3
4
5
6
7
8
9
bool getTime(int &hours, int &minutes)
{
	printf("Please enter the hours between 0 and %d\n", MaxHours);
	scanf("%d", &hours);

	if (hours < 0 || hours > MaxHours)
	{
		return false;
	}


Let's look at the next one, it has a different problem.
1
2
3
4
5
6
7
8
9
10
int getInRange(int minVal, int maxVal)
{
	float userInput;
	scanf("%f", &userInput);

	if(userInput < minVal)
	{
		printf("Error: the value %f is too small\n", userInput);
		return ErrVal;
	}
We want to read a number in the range [minVal, maxVal] and return that number. The number is of type int.

So we need to read an int and validate it. You've read a float, but we don't want a float, we want an int. We can loop until the user gets it right (which can be annoying)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int getInRange(int minVal, int maxVal)
{
	int userInput;

	do
	{
		scanf("%d", &userInput);

		if(userInput < minVal)
		{
			printf("Error: the value %d is too small. We need a value between %d and %d\n", userInput, minVal, maxVal);
		}
	}
	while (userInput < minVal && userInput > maxVal);

	return userInput;
}
Last edited on
That's awesome, I like the way you've explained it, super helpful. Wish I could work on it right away but have class soon. Thank you so much, I'll be sure to report back!
Hey there, thanks again for your help. Got it figured out, thumbs up to your good self.
Topic archived. No new replies allowed.