I have just started reading a C++ textbook and I am having trouble solving one of the coding problems at the end of the chapter. Here is the question:
Write a program that asks the user to enter an hour value and a minute value. The
main() function should then pass these two values to a type void function that displays
the two values in the format shown in the following sample run:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
my code so far is:
#include <iostream>
using namespace std;
void time(int h, int m);
int main()
{
int hour, min;
cout << "enter the number of hours: ";
cin >> hour;
cout << "enter the number of minutes: ";
cin >> min;
string temp = time(hour, min);
cout << temp;
return 0;
}
void time(int h, int m)
{
string clock;
clock =
}
What do I do now inside the time(n, m) function?
Thanks.
You can include
<iomanip>and set field width and fill so that times like9:01are printed properly. And since the functiontimeshould just print the time, building and returning astd::stringcan be omitted. Just print these values:Also note that writing
using namespace std;at the beginning of your files is considered bad practice since it causes some of user-defined names (of types, functions, etc.) to become ambiguous. If you want to avoid exhausting prefixing withstd::, useusing namespace std;within small scopes so that other functions and other files are not affected.