我不斷收到這個奇怪的問題,這些字符出現在我的char數組中,名字爲day[3]
。我的目標是使用計算機時鐘並獲取日期和時間。我把它放到一個名爲dayHolder的字符串中,並且只想將它添加到名爲day
的char數組中。但是當我這樣做的時候會得到很多奇怪的字符。據我所知,該字符串應該用「\ 0」結束,但不能似乎得到day
只是顯示「星期五」 ......字符串輸出中的奇數字符C++
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
int main() {
// Functions
functions myFunc;
//Variables + installations
string dayHolder;
char day[3];
char whitespace = ' ';
time_t current = time(0);
dayHolder = ctime(¤t);
for (int i = 0; i < 3; i++)
{
day[i] = dayHolder[i];
}
cout << ctime(¤t) << endl;
cout << dayHolder << endl;
cout << day << endl;
return 0;
}
所以我能做些什麼來解決這個問題?我相信它的一些簡單的東西,我忽略了,所以任何意見或建議,將不勝感激謝謝。
你錯過了上串空終止。 – Barmar 2014-09-12 20:18:25
既然你明白這個字符串必須是null結尾的。那你爲什麼不'char day [4] ... day [3] ='\ 0';'? – 101010 2014-09-12 20:18:52