简体   繁体   中英

printing time to std::ostream

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 like 9:01 are printed properly. And since the function time should just print the time, building and returning a std::string can be omitted. Just print these values:

void time(int hour, int min)
{
    using namespace std;
    cout << "Time: " << hour << ':' << setfill('0') << setw (2) << min << endl;
}

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 with std:: , use using namespace std; within small scopes so that other functions and other files are not affected.

The question requests "a type void function that displays the two values in the format shown" so the simplest and most correct (because it matches what was asked) solution is:

void time(int h, int m)
{
  cout << "Time: " << h << ":" << m << endl;
}

Your main() function then needs to do nothing but...

  // ... prompt for values as before, then:

  time(hour, min);

  return 0;
}

and then return.

First time() should return a std::string. To format the string inside time() you could use std::ostringstream (header sstream).

Eg:

std::string time(int hour, int minutes)
{
   std::ostringstream oss;
   oss << hour << ":" << minutes;
   return oss.str();
}

Edit: Of course, you could also print hour and minutes directly inside the time(..) function. Or you could pass the time(..) function also a stream argument to let time(..) print it out on that stream.

Your code in main is assuming that time is a string method, the question states void . Your code should be:

#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; 

    // Now pass to your time method.
    time(hour, min); 

    return 0; 
} 

void time(int h, int m)     
{     
    cout << "Time: " << h << ':' << m << endl;     
}

and Bob is someone's uncle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM