简体   繁体   English

打印到std :: ostream的时间

[英]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. 我刚刚开始阅读C ++教科书,但在本章末尾我遇到了一个编码问题。 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: 然后main()函数应将这两个值传递给类型void函数,该函数以下面的示例运行中显示的格式显示两个值:

Enter the number of hours: 9 输入小时数:9
Enter the number of minutes: 28 输入分钟数:28
Time: 9:28 时间: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? 我现在在time(n, m)函数中做什么?

Thanks. 谢谢。

You can include <iomanip> and set field width and fill so that times like 9:01 are printed properly. 您可以包含<iomanip>并设置字段宽度填充,以便正确打印9:01类的时间。 And since the function time should just print the time, building and returning a std::string can be omitted. 由于函数time应该只打印时间,因此可以省略构建和返回std::string 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; 还要注意using namespace std;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; 如果你想避免使用std::前缀,请使用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函数,它以显示的格式显示两个值”,因此最简单和最正确的(因为它匹配所要求的)解决方案是:

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

Your main() function then needs to do nothing but... 你的main()函数然后只需要做什么......

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

  time(hour, min);

  return 0;
}

and then return. 然后回来。

First time() should return a std::string. 第一次()应该返回一个std :: string。 To format the string inside time() you could use std::ostringstream (header sstream). 要在time()中格式化字符串,可以使用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 . 你在main中的代码假设time是一个string方法,问题说明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. 鲍勃是某人的叔叔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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