I'm having some trouble to do with strings, with a function I've written.
First, at the top of the program, outside of main, I've declared the following global strings, to hold the time, and the date:
The variables are given data during this function:Code:char Date[11]; char Time[9];
The problem is, when Date is printed onto the console, the contents of time also appear after date too! I thought that the string should have been terminated by the \0 escape code.Code:/* Load time */ /* Loads the time and date settings into the global Time and Date variables */ int LoadTime() { cout << "Loading time and date...\n"; time_t TimeObject; tm * TimePointer; time(&TimeObject); TimePointer = gmtime(&TimeObject); char Symbols[3] = "-:\0"; string MyTimeString = asctime(TimePointer); Date[0] = MyTimeString[8]; Date[1] = MyTimeString[9]; Date[2] = Symbols[0]; Date[3] = MyTimeString[4]; Date[4] = MyTimeString[5]; Date[5] = MyTimeString[6]; Date[6] = Symbols[0]; Date[7] = MyTimeString[20]; Date[8] = MyTimeString[21]; Date[9] = MyTimeString[22]; Date[10] = MyTimeString[23]; Date[11] = Symbols[2]; Time[0] = MyTimeString[11]; Time[1] = MyTimeString[12]; Time[2] = MyTimeString[13]; Time[3] = MyTimeString[14]; Time[4] = MyTimeString[15]; Time[5] = MyTimeString[16]; Time[6] = MyTimeString[17]; Time[7] = MyTimeString[18]; Time[8] = MyTimeString[19]; Time[9] = Symbols[2]; cout << Date << "\n"; return 0; }
Does any kind people have some insight into this strange behaivour?



LinkBack URL
About LinkBacks



CornedBee