![]() |
| | #1 |
| Registered User Join Date: Jan 2007
Posts: 6
| String problems - strings seem to join? First, at the top of the program, outside of main, I've declared the following global strings, to hold the time, and the date: Code: char Date[11]; char Time[9]; 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? |
| Metalixia is offline | |
| | #2 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,492
| You didn't actually provide space for the '\0' terminator. Date contains space for 11 characters (indices 0-10), but you assign to the twelth (index 11). As it happens on your platform, Date and Time are directly adjacent in memory, so Time[0] is the same memory location as (the invalid) Date[11]. In other words, the assignment to Time[0] overwrites the terminator you gave Date.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #3 |
| Registered User Join Date: Jan 2007
Posts: 6
| Ah, excelent! Such a simple problem, sometimes it takes another pair of eyes to spot these sort of things. Thankyou so much. |
| Metalixia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Inheritance Hierarchy] User Input on program with constructors. How ? | chandreu | C++ Programming | 8 | 04-25-2008 02:45 PM |
| Inheritance Hierarchy for a Package class | twickre | C++ Programming | 7 | 12-08-2007 04:13 PM |
| Custom String class gives problem with another prog. | I BLcK I | C++ Programming | 1 | 12-18-2006 03:40 AM |
| RicBot | John_ | C++ Programming | 8 | 06-13-2006 06:52 PM |
| problems with string manipulation | Unregistered | C++ Programming | 0 | 04-24-2002 04:45 PM |