user2087867
user2087867

Reputation: 105

strange output when I cout an array

I've never seen anything like this before and I can't figure out what could possibly be causing it. This is HW so you don't have to fix it for me just give me a clue whats going wrong. Here is my code.

void countChars(ifstream& inData, string filename, char x[])
{
for(int i=0; i < 58; i++)
    x[i] = 33+i;

cout << x << endl;
}

here is my output

output

Upvotes: 1

Views: 74

Answers (2)

RichardPlunkett
RichardPlunkett

Reputation: 2988

add a x[58] = 0; before the cout.

Upvotes: 1

ScarletAmaranth
ScarletAmaranth

Reputation: 5101

You forgot to null terminate your char[].

std::cout.operator<<(char*) uses \0 to tell where to stop.

This is a duplicate of: this

Upvotes: 4

Related Questions