我试图弄清楚如何同时显示军事和标准时间。
用户输入时间,然后以标准格式显示。
它主要在那里,但标准时间部分给了我一些问题。
#ifndef TIME_H
#define TIME_H
class Time
{
public:
Time(int = 0, int = 0, int = 0);
~Time();
int hour; // valid values are 0 to 23
int minute; // valid values are 0 to 59
int second; // valid values are 0 to 59
void setTime(int, int, int); // function that checks if inputs are valid
void printUniversal(); // prints in HH:MM:SS format
void printStandard(); // prints in HH:MM:SS AM/PM format
static int count; //counter
};
#endif
#include "stdafx.h"
#include "Time.h" //header file that contains the Time class file
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int Time::count = 12;
Time::Time(int hr, int min, int sec)
{
hour = hr; minute = min; second = sec;
count++;
}
Time::~Time()
{
count--;
}
void Time::setTime(int hr, int min, int sec)
{
hour = (hr >= 0 && hr < 24) ? hr : 0; // checks if hour input is valid
minute = (min >= 0 && min < 60) ? min : 0; // checks if minute input is valid
second = (sec >= 0 && sec < 60) ? sec : 0; // checks if seconds input is valid
}
void Time :: printUniversal()
{
cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second;
}
void Time::printStandard()
{
cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << setfill('0') << setw(2) << minute << ":" << setw(2) << second << (hour < 12 ? " AM" : " PM");
}
//(Where I implement the functions - main.cpp)
#include "Time.h"
#include <iostream>
using namespace std;
int main()
{
int hour, minute, second;
Time t; //t is the time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
//test is also a time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
//Time *tp = new Time;
//Time *tarray = new Time[5];
cout << "Enter hour in military time ";
cin >> hour;
cout << "Enter minute ";
cin >> minute;
cout << "Enter second ";
cin >> second;
cout << "\nThe standard time is ";
t.printStandard(); //(PROBLEM!!!- I have the number 12 appearing right after AM and i can't get rid of it. )
cout << "\nThe universal time is ";
t.printUniversal();
cout << endl;
return 0;
} // end main
更新:由于发布的建议,错误暂时消失。
然而,现在当我运行它时,我得到了这个......好吧,因为我无法发布图像......
我输入13,45,05并且几乎像旧的录像机一样,我不能在12:00:00标准或00:00:00通用
无论我输入所有相同的输出。
答案 0 :(得分:0)
Time t(); // This declares a function named t which takes
// no arguments and returns a Time object
Time test(); // This declares a function named test which
// takes no arguments and returns a Time object
Time t; // this is a default constructed Time object named t
Time t( 12, 7, 5 ); // this is a Time object with all
// parameters passed to the constructor
在printStandard中,你有
<< sizeof(Time)
这是12来自哪里。 Time类类型的对象在内存中占用12个字节。
我强烈建议你读一本关于C ++的书,因为所有这些都将在那里解释。