这份代码有什么问题#include <iostream> using namespace std; class time { private : int H; int F; int M; public : void sett(int, int, int); void print(); }; void time:: sett(int h, int f, int m) { H = h; F = f; M = m; } void time:: print() { printf("%2d-%2d-%2d", H, F, M); } int main() { int h, f, m; cin >> h >> f >> m; sett(h, f, m); return 0; }
时间: 2024-01-20 18:03:30 浏览: 27
这份代码有几个问题:
1. 在 main 函数中,调用了 `sett(h, f, m)`,但是 `sett` 函数并不是 main 函数的成员函数,因此应该先创建一个 time 类的对象,然后通过这个对象调用 `sett` 函数。
2. 在 `print` 函数中使用了 `printf` 函数,但是这个函数需要包含头文件 `stdio.h`,因此应该使用 `cout` 来代替。
3. 在 `print` 函数中使用了 `%2d` 的格式化字符串,但是应该使用 `%02d` 来确保输出的数字总是两位数。
4. 在 `print` 函数中没有输出分隔符,因此输出的时间格式不正确,应该在每两个数字之间加上一个分隔符,比如冒号。
下面是修改后的代码:
```
#include <iostream>
using namespace std;
class time {
private:
int H;
int F;
int M;
public:
void sett(int, int, int);
void print();
};
void time::sett(int h, int f, int m) {
H = h;
F = f;
M = m;
}
void time::print() {
cout << setw(2) << setfill('0') << H << ":" << setw(2) << setfill('0') << F << ":" << setw(2) << setfill('0') << M;
}
int main() {
int h, f, m;
cin >> h >> f >> m;
time t;
t.sett(h, f, m);
t.print();
return 0;
}
```
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)