continuing to work on my time program. Compiles just fine--handles exceptions just fine, however the print output is not correct. I've left of the minutes entry and seconds entry for code brevity.
int hourEntry(int hours)
{
bool running=false;
hours = 0;
do
{
try
{
cout << "Please enter the hours (1-12): ";
cin >> hours;
cout << endl;
if (hours > 12 || hours < 0)
{
throw invalidHr();
}
else
{
running = true;
}
}
catch (invalidHr invHr)
{
cout <<"Runtime Error: " <<invHr.what()<<endl;
}
}
while (!running);
return hours;
};
void printTime(int hours, int minutes, int seconds)
{
int amPM = 0;
cout << "Is the time to be entered in the Morning(1) or Evening(2)? Enter 1 or 2: ";
cin >> amPM;
if(amPM==1)
{
if (hours == 12)
cout << hours-12 << ":" << minutes << "." << seconds << endl;
}
elseif (amPM==2)
{
if(hours==12)
cout << hours<<":"<<minutes<<"."<<seconds<<endl;
else
cout <<hours+12<<":"<<minutes<<"."<<seconds<<endl;
}
};
int main()
{
int hours = 0;
int minutes = 0;
int seconds = 0;
cout << "This Program Will Convert 12HR Time formats to 24HR time formats"<<endl;
cout << endl;
hourEntry(hours);
minuteEntry(minutes);
secondsEntry(seconds);
cout << "Your Time Converted to 24 hr time is: " << endl;
printTime(hours, minutes, seconds);
return 0;
}
would I be correct to assume that although I'm calling hourEntry, mintueEntry and secondsEntry, they aren't returning their values (hours, minutes, seconds) to main? The program is returning "12:0.0" when I run it regardless of the user input.
would I be correct to assume that although I'm calling hourEntry, mintueEntry and secondsEntry, they aren't returning their values (hours, minutes, seconds) to main?
Not exactly.
Those functions do return a value. However main() just ignores it.
There are two ways to do this. The first, and perhaps most usual, is to assign the value returned from the function to a variable. See hourEntry() below.
The second, useful when more than one value is being returned, pass the parameter(s) by reference. See minuteEntry() below. Notice the & in the parameter list.
// function returns an integer result
int hourEntry()
{
int hours = 7;
return hours;
};
// function modifies the parameter passed by reference
void minuteEntry(int & minutes)
{
minutes = 23;
}
int main()
{
// receive value returned from function
int hours = hourEntry();
// pass parameter by reference
int minutes = 0;
minuteEntry(minutes);
}
> would I be correct to assume that although I'm calling hourEntry, mintueEntry and secondsEntry,
> they aren't returning their values (hours, minutes, seconds) to main?
hourEntry, and presumably the other two functions are returning values to main()
But main() ignores the values that are returned.
(They also modify the copy of the integer that is passed to them; but since the modifications are made on a copy, the variables in main() are unaffected.)
#include <iostream>
int get_int( int minv, int maxv )
{
std::cout << "enter a value in [ " << minv << '-' << maxv << "]: " ;
int value ;
// we'll assume for now that he user does not enter invalid characters eg. abcd
std::cin >> value ;
if( value >= minv && value <= maxv ) return value ; // ok, return value within range
std::cout << "invalid input. try again\n" ;
return get_int( minv, maxv ) ; // try again
}
int get_hour()
{
std::cout << "hour? " ;
return get_int( 1, 12 ) ;
}
int get_minutes()
{
std::cout << "minutes? " ;
return get_int( 0, 59 ) ;
}
int get_seconds()
{
std::cout << "seconds? " ;
return get_int( 0, 59 ) ;
}
int main()
{
constint hr = get_hour() ; // store the value returned by the function in the variable hr
constint min = get_minutes() ;
constint sec = get_seconds() ;
std::cout << hr << ':' << min << ':' << sec << '\n' ;
// printTime( hr, min, sec ) ;
}