2016-08-11 8 views
0
#include<iostream> 
#include<ctime> 
#include<string> 
using namespace std; 

int main() 
{ 
    string NowTime; 
    time_t now; 

    struct tm nowLocal; 

    now=time(NULL); // get the time from the OS 

    nowLocal=*localtime(&now); 

    NowTime = nowLocal.tm_hour + ':' + nowLocal.tm_min + ':' + nowLocal.tm_sec; 
    cout<< NowTime; 
} 

プログラムを実行すると、何も表示されず、誰かが私を助けることができますか?私は全く新しいプログラミングです。システム時刻を文字列変数に追加する方法

+0

strftime、http://www.tutorialspoint.com/c_standard_library/c_function_strftime.htmを参照してください。 –

答えて

0

ライン:チャーが暗黙intに強制されるので

NowTime = nowLocal.tm_hour + ':' + nowLocal.tm_min + ':' + nowLocal.tm_sec; 

は、時間、分、および結腸シンボルの数値に第二を追加します。その値は文字列代入演算子のcharとして解釈されます。

代わりに、値を直接coutに直接出力することができます。それらはcoutのストリーム挿入演算子<<によって適切にフォーマットされます。

#include<iostream> 
#include<ctime> 
#include<string> 
using namespace std; 

int main() 
{ 
    string NowTime; 
    time_t now; 

    tm nowLocal; 

    now=time(NULL); // get the time from the OS 

    nowLocal=*localtime(&now); 

    cout << nowLocal.tm_hour << ':' << nowLocal.tm_min << ':' << nowLocal.tm_sec; 

    return 0; 
} 

文字列に格納する場合は、stringstreamsを読んでください。彼らはcoutと同じ構文を持ち、書式設定文字列をずっと簡単にすることができます。

0

代わりに、あなたがこのような出力を、変数に結果を置くことができたの:

:あなたは、変数を保持したい場合は、これを行う、また

cout << nowLocal.tm_hour << ':' << nowLocal.tm_min << ':' << nowLocal.tm_sec; 

Live Example

NowTime = std::to_string(nowLocal.tm_hour) + ':' + std::to_string(nowLocal.tm_min) + ':' + std::to_string(nowLocal.tm_sec); 
cout << NowTime; 
0

これを試してみてください:

#include<iostream> 
#include<ctime> 
#include<string> 
#include <sstream> 
using namespace std; 

int main() 
{ 
string NowTime; 
time_t now; 

struct tm nowLocal; 

now=time(NULL); // get the time from the OS 

nowLocal=*localtime(&now); 

stringstream s; 
s<<nowLocal.tm_hour; 
s<<":"; 
s<<nowLocal.tm_min; 
s<<":"; 
s<<nowLocal.tm_sec; 
NowTime = s.str(); 
cout<< NowTime; 
} 

intからstringに直接変換することはできず、ストリームに値を入れてから文字列に値を入れる必要があります。

1

あなたは

cout << nowLocal.tm_hour + ':' + nowLocal.tm_min + ':' + nowLocal.tm_sec; 

を試みる場合は、整数ではなく、時間に似たものが表示されます。
これは、5つの整数の合計です。文字は整数に昇格され、すべて加算されます。

最も簡単な修正は、個別部品を出力するが、すべての文字列を構築しないことです。

cout << nowLocal.tm_hour << ':' << nowLocal.tm_min << ':' << nowLocal.tm_sec; 

そうでない場合、あなたは文字列にこれらの数字を変換する必要があります。

NowTime = std::to_string(nowLocal.tm_hour) + ':' + std::to_string(nowLocal.tm_min) + ':' + std::to_string(nowLocal.tm_sec); 

か、あなたstd::coutと他のストリームと同じように機能しますが、std::stringに書き込むstd::ostringstreamを使用できます。

​​
std::ostringstream ss; 
ss << nowLocal.tm_hour << ':' << nowLocal.tm_min << ':' << nowLocal.tm_sec; 
NowTime = ss.str(); 
0

iostringstreamを使用して必要な文字列を作成するのはどうですか?

#include<iostream> 
#include<ctime> 
#include<string> 
#include <sstream> 

using namespace std; 

int main() 
{ 
    ostringstream NowTime; 
    time_t now; 

    struct tm nowLocal; 

    now=time(NULL); // get the time from the OS 

    nowLocal=*localtime(&now); 

    NowTime << nowLocal.tm_hour << ":" << nowLocal.tm_min << ":" << nowLocal.tm_sec; 
    cout<< NowTime.str() << endl; 
} 

また、プログラムの目的のために、出力ストリームであるstd :: coutを簡単に使用できます。

cout << nowLocal.tm_hour << ":" << nowLocal.tm_min << ":" << nowLocal.tm_sec << endl; 
0

あなたは事前にC++ 11だとstd::to_stringを使用することはできませんように見えますので。ここでは、あなたが現在使っているインクルードに固執するCのようなやり方があります。

#include<iostream> 
#include<ctime> 
#include<string> 

using namespace std; 

#define STR_LEN 128 

int main() 
{ 
    string nowTime; 

    time_t now; 

    struct tm nowLocal; 

    now = time(NULL); // get the time from the OS 

    nowLocal = *localtime(&now); 

    char hour[ STR_LEN ], min[ STR_LEN ], sec[ STR_LEN ]; 

    sprintf(hour, "%d", nowLocal.tm_hour); 

    sprintf(min, "%d", nowLocal.tm_min); 

    sprintf(sec, "%d", nowLocal.tm_sec); 

    nowTime = string(hour) + ':' + string(min) + ':' + string(sec); 

    cout << nowTime << endl; 
} 
0

どのようにシステム時間を文字列に追加しますか?

私の答えは、便利な機能を構築することです。

時間、分、秒だけが本当に必要な場合は、比較的遅いlocaltime()を使用する必要はありません。 (一方で、もっと必要があれば、変換のためにlocaltime_r()を使うべきだと思います)。

組み込みシステムの場合、いくつかの契約が戻ってくるので、私はこの変換が比較的遅い関数であることを発見し、それを回避することを選択しました。うるう日や世紀などを処理するためのアルゴリズムは、簡単に見えます。私は、変換を1秒間に何回もやろうとしていたそのアプリケーションで必要以上に計算しているので、単純に遅く思ったと思う。

モジュラ演算を含むより単純な(おそらくさらに高速な)アプローチが存在します。それは同じ - 時間(0)で始まります(そして、私はここで何をしているのかはlocaltime_r()関数では隠されていると思います)。サイドノート1 - 私の古いデルのUbuntu 15.10を実行している時、time(0)はウォールクロックへの最速のアクセスで、典型的な期間は約6または7 nsです。サイドノート2 - time_tはいつか変更されるかもしれません。 "time_t Wikipediaの記事でこれについていくつかのことを明らかにしています。結論として、time_tのタイプはC仕様で保証されていないということです。"

私は現在、便利なタイム・スタンプ列を生成するために使用するコード:静的項目

std::string hhCmmCssGet(time_t tt = 0) // tt has default value 
{ 
    time_t now = (tt ? tt : time(0)); 

    static time_t  prev = 0; 
    static char hhmmss[] = "hh:mm:ss"; 

    if (prev != now) 
    { 
     prev = now; 

     const int32_t CST = -6; 
     int64_t hr = ((now/3600) % 24) + CST; // hr of day, CST offset 
     if (hr < 0) hr += 24; 
     uint64_t min = ((now/60) % 60); // min of day 
     uint64_t sec = (now % 60);   // sec of day 

     std::stringstream ss; 
     ss << std::dec 
     << std::setfill('0') << std::setw(2) << hr << ":" 
     << std::setfill('0') << std::setw(2) << min << ":" 
     << std::setfill('0') << std::setw(2) << sec; 

     for (size_t i=0; i<8; i++) // transfer new value 
     hhmmss[i] = ss.str()[i]; // into static hhmmss 
    } 
    std::string retVal(hhmmss); 
    return(retVal);   
} 

と句「(PREV =今!)場合」、この関数は、毎秒何千回も呼び出すことができるようにします...労力を大幅に削減しました。結局のところ、1秒あたり1秒しか更新されません。また、std :: stringstreamとモジュラ算術演算は1秒あたり1秒しか実行しないことに注意してください。

+0

したがって、 "string NowTime(hhCmmCssGet());"あなたのために働くはずです。 –

関連する問題