2014-10-28 45 views
-3

我應該使它成爲string?或者有一個簡單的方法來與int如何修復時間演示從23:7:42到23:07:42

class TimeFixer { 
private: 
    int seconds=0; 
    int minutes=0; 
    int hours=0; 


public: 
    TimeFixer() { 
     cout << "Enter hours,minutes,seconds: " ; 
     cin >> hours; 
     cin >> minutes; 
     cin >> seconds; 
    } 
    void fixTime() { 
     while (seconds >= 60) { 
      seconds -= 60; 
      minutes++; 
     } 
     while (minutes >= 60) { 
      minutes -= 60; 
      hours++; 
     } 
     while (hours >= 24) { 
      hours -= 24; 
     } 
    } 
    void displayTime() { 
     cout << hours << ":" << minutes << ":" << seconds << endl; 
    } 
}; 
int main() 
{ 
    int value = 0; 
    cout << "Enter value: " ; 
    cin >> value; 
    TimeFixer *fix1; 
    fix1 = new TimeFixer[value](); 

    for (int i = 0 ; i < value ; i++) { 
     fix1[i].fixTime(); 
     fix1[i].displayTime(); 
    } 
    return 0; 
} 
+2

'運輸及工務局局長(2)'和'setfill( '0')'?更多的代碼示例可能會有所幫助。 – Niall 2014-10-28 11:45:39

+2

這是有點不清楚你在問什麼。字符串「23:7:42」從哪裏來?也許你應該向我們展示你的代碼。 – user763305 2014-10-28 11:47:16

+0

即時獲得用戶的時間INT小時分鐘等 – TheNsn666 2014-10-28 11:57:23

回答

2

您也可以使用setfill()和setw()函數來顯示數據,按照Niall的建議。

即。您可以使用以下方式顯示時間。

void displayTime() { 
    cout << setfill ('0') << setw (2)<< hours << ":" << setw (2)<< minutes << ":" << setw (2)<< seconds << endl ; 
} 

例如圖:http://codepad.org/NNL930Im

+0

謝謝你的工作很棒! – TheNsn666 2014-10-28 12:31:16

+0

我不認爲你需要在每個整數輸出之前使用'setfill('0')'。只要在開始時就足夠了。參見[http://codepad.org/FqHrTOxm](http://codepad.org/FqHrTOxm)。 – 2014-10-28 13:19:42

相關問題