how can i manage to input a long sentences

May 20, 2014 at 12:13pm
Hi Im just going to ask how can I manage to input long words such as addresses in c++. Ive tried char but its no use. Can anyone help me?

1
2
3
4
5
  char address[100];
  
cout<<"Enter your address";
cin>>address;
May 20, 2014 at 1:27pm
In C++ use std::string rather than a char[] array. To get more then a single word, use getline().

1
2
3
string address;
cout << "Enter your address";
getline(cin, address);
May 20, 2014 at 1:33pm
You don't want to use a char array in any case; it is (practically) always too small or too big.

You should use std::string instead. However, you cannot use operator>>, for string takes only one word from it.

Therefore, read from stream with std::getline() into a string. (Note: if the user writes a multi-line address, then you have an additional challenge.)


Edit: too slow
Last edited on May 20, 2014 at 1:34pm
May 20, 2014 at 4:08pm
As the others said, the best way for that kind of inputs are string and getline method. another thing that you can use is make an array with the length of the address using new command, But i suggest you to use string as the others do that. and please when you get your answer, close the topic.
(really i think you should got your answer)

Have nice life.
Topic archived. No new replies allowed.