so im creating a program on hours and minutes. and well my program basically works just that the output is a bit off.

#include <iostream>
#include <string> 
#include <iomanip>
using namespace std;

void getHours(int& input1, string& s);
void getMinutes(int& input2, string& s);
void calcTotalTime(int& input1, int& input2, int& input3, int& input4);
bool isValidMinutes(int);
bool isValidHours(int);
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, (string)("Enter the number of hours for the starting time: "));
	getMinutes(minutes, (string)("Enter the number of minutes for the starting time: "));
    getHours (addedHours, (string)("Enter the number of hours to be added to the starting time: "));
    getMinutes (addedMinutes, (string)("Enter the number of minutes to be added to the starting time: "));
	calcTotalTime(hours, minutes, addedHours, addedMinutes);
    cout << "\nThe total time is " << hours << " hours and "
            << minutes << " minutes." << endl;
	system("pause");
    return 0;
}



void getHours (int& input1, string& s)
{
	bool result;
	do {
	cout << s;
	cin >> input1;
	result = isValidHours(input1);
	} while (result == false);
}

void getMinutes (int& input2, string& s)
{
	bool result2;
	do {
	cout << s;
	cin >> input2;
	result2 = isValidMinutes(input2);
	} while (result2 == false);
}

 void calcTotalTime(int& input1, int& input2, int& input3, int& input4)
 {
	
	int num1 = (input1 + input3);
	int num2 = (input2 + input4);
	int num3 = (num2 - 60);

	
		if (num2 > 59)
		{
		cout << "\nThe total time is " << (num1 + 1) << " hours and "
				<< num3 << " minutes." << endl;

		}
		else
		{
			cout << "\nThe total time is " << num1 << " hours and "
				<< num2 << " minutes." << endl;

		}

}

 

bool isValidHours (int x)
{
	if ( x >= 0 && x <= 23)
			return true;

	else 
		cout <<"Hours must be at least 0, and no more than 23" << endl;
	return false;
	

}

bool isValidMinutes (int xx)
{
	if ( xx >= 0 && xx <=59)
		return true;

	else 
		cout <<"Minutes must be at least 0, and no more than 59" << endl;
		
		return false;
}

if you run youll see i get to outputs of the total hours and minutes , 1 being the correct outout and the other the wrong output. if you dont get me just run the program and youll see my problem.

Recommended Answers

All 4 Replies

This program doesn't compile for me due to errors like this in line 19:

invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'

You're saying you were able to compile and run this?

really? it compiles for me and runs hmm...

It doesn't work because calcTotalTime() isn't changing the values of the input parameters.

>>really? it compiles for me and runs hmm...
Yes -- didn't you try to compile it :S

A compile attempt (gcc):

main.cpp: In function `int main()':
main.cpp:19: error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'
main.cpp:6: error: in passing argument 2 of `void getHours(int&, std::string&)'
main.cpp:20: error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'
main.cpp:7: error: in passing argument 2 of `void getMinutes(int&, std::string&)'
main.cpp:21: error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'
main.cpp:6: error: in passing argument 2 of `void getHours(int&, std::string&)'
main.cpp:22: error: invalid initialization of non-const reference of type 'std::string&' from a temporary of type 'std::string'
main.cpp:7: error: in passing argument 2 of `void getMinutes(int&, std::string&)'
*** Errors occurred during this build ***

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.