By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

How to read string with space (help please)

P: 6
i have the following code :

string addrress;
cin >> address;
cout << address;

if I enter any string with space in between , it will save the first word only.

e.g. if i enter "this is a test"

address = this

how can I make address to have the rest of the string too. please help
Dec 4 '06 #1
Share this Question
Share on Google+
3 Replies

P: 5
use gets(address) instead of cin>>address

and puts(address) instead of cout<<address
Dec 4 '06 #2

P: 6
thanks it works. but i had to declare address as Char not string. so how can i get the size or length of the Char. is like (string.size or string.length). advise me please.

Dec 4 '06 #3

Expert 2.5K+
P: 3,652
string addrress;
cin >> address;
cout << address;
If you want to keep address as a string, use the getline function:

Expand|Select|Wrap|Line Numbers
  1. getline(istreamVar, stringVar);
where istreamVar would be cin, and stringVar is address. This will get every character in the input stream until a newline character is found. So, with your example, if you entered "this is a test", address would hold "this is a test".
Dec 4 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.