Values return as zero--values from function calls

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.

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

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;
	    }
        else if (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.

Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 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);
   
}


Last edited on
> 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.)

Something like this, perhaps:

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>

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()
{
    const int hr = get_hour() ; // store the value returned by the function in the variable hr
    const int min = get_minutes() ;
    const int sec = get_seconds() ;

    std::cout << hr << ':' << min << ':' << sec << '\n' ;

    // printTime( hr, min, sec ) ;
}

Thank you everyone! Finally got it working--the entire part about passing as a reference really helped me out!
Topic archived. No new replies allowed.