0

I keep getting this odd problem where these characters appear in my char array called day[3]. My goal is to use the computers clock and get the date and time. I put that into a string called dayHolder and want to just add the day it to a char array called day. But when I do this it gets a lot of odd characters. I understand that the string should end with '\0' but cant seem to get day to just display "Fri"....

#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(&current);


    for (int i = 0; i < 3; i++)
    {
        day[i] = dayHolder[i];
    }



    cout << ctime(&current) << endl;
    cout << dayHolder << endl;
    cout << day << endl;


    return 0;
}

enter image description here

So what can I do to fix this problem? I am sure its something simple that I am overlooking so any advise or suggestions would be appreciated thanks.

2
  • 4
    You're missing the null terminator on the string. Commented Sep 12, 2014 at 20:18
  • 2
    Since you understand that string must be null terminated. Then why you don't char day[4] ... day[3] = '\0';? Commented Sep 12, 2014 at 20:18

5 Answers 5

3

char day[3] is not zero-terminated.

Try

char day[4];

....

for (int i = 0; i < 3; i++)
{
    day[i] = dayHolder[i];
}
day[3] = 0; // or '\0' as @DeepBlackDwarf suggested

Or, alternatively

string day = dayHolder.substr(0, 3);
Sign up to request clarification or add additional context in comments.

2 Comments

Hey, wow that worked! I am going to look it up after I post this but I thought that char arrays start at base 0. So in the case of Fri 0 = F, 1 = r, 2 = i, 3 = null i.e \0. Is how does making the memory of the array 4 fix this?
There is no 3. day[3] declares an array or three elements, with indices 0, 1, 2. In addition, array is not initialized by default (but see the answer of fbrereto).
2

You have to add an extra '\0' character at the end of the string, ie declare a char day[4] and assign the last char with day[3] = '\0'.

Comments

2

You need to null-terminate, as has been mentioned. First you'll need to add another byte for the null. Another option is to zero out the buffer initially:

char day[4] = { 0 };

As long as you don't overwrite day[3] that value will stay 0 and the string will stay a valid c-string.

2 Comments

Except that they write 3 characters in, so unless the buffer is larger you still won't have a terminating null character.
One more for the null terminator: char day[4] = { 0 }
1

That happens because cout keeps on writing to stdout until it meets a null terminator, hence the garbage values. Since you are using a C-style string, you should always remember to include an additional space in order to null terminate your string (e.g. you want to store 3 chars, so buffer should have size of 4). Others already mentioned that you need a bigger buffer and put there a \0 at the end.

Comments

1

Why don't you use std::string since you've already included string header:

time_t current = time(0);
string dayHolder = ctime(&current);
string day(dayHolder.begin(), dayHolder.begin() + 3);

LIVE DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.