0

I need an explanation please, I want to insert a sentence, and then do all kinds of actions on it. My problem is that when I try to print the sentence I wrote, he prints only the first word. I do not want to use the string, except in the form I wrote.

int size = 10;
char* str = new char[size];

for (int i = 0; i < size; i++){
    str[i] = NULL;
}

cin >> str;

for (int i = 0; i < size; i++){
    cout << str[i];
}

input:

abcdef abc

output:

abcdef  

When I set the array of char * how it looks, like that?

char* str = new char[10]; >>> [pointer?][garbage?][Something else?][][][][][][][]

thank's.

liran
  • 19
  • 2
  • 3
    " My problem is that when I try to print the sentence I wrote, he prints only the first word." That's because `>>` breaks at whitespace. – Sergey Kalinichenko Mar 27 '17 at 16:15
  • 3
    10 is awfully small for user input. You're almost certainly writing off the end of what you've allocated, invoking undefined behavior. It would be a much better idea to use `std::string`. – Fred Larson Mar 27 '17 at 16:15

0 Answers0