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

Inserting a space in C++

P: n/a
How can I allow a user (in a command prompt) to enter a string of
characters and put spaces in it? I use Dev C++. Example:

char[50] google;

cin>> google;
Whenever I try this and output the variable, the string cuts off at the
end of a space. The odd thing is that the alt code space works, but not
the spacebar.

Apr 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ir******@gmail.com wrote:
How can I allow a user (in a command prompt) to enter a string of
characters and put spaces in it? I use Dev C++. Example:

char[50] google;
Do you mean

char google[50];

? Never mind...
cin>> google;
Whenever I try this and output the variable, the string cuts off at
the end of a space. The odd thing is that the alt code space works,
but not the spacebar.


Use 'std::getline'. RTFM about it.

V
--
Please remove capital As from my address when replying by mail
Apr 12 '06 #2

P: n/a

ir******@gmail.com wrote:
How can I allow a user (in a command prompt) to enter a string of
characters and put spaces in it? I use Dev C++. Example:

char[50] google;
I doubt if any compiler accepts this. Try posting correct code snippet.
You probably meant
char google[50];
cin>> google;
Try:
char a[50];
std::cin.getline(a, 50);
std::cout<<a;


Whenever I try this and output the variable, the string cuts off at the
end of a space. The odd thing is that the alt code space works, but not
the spacebar.


Actually why not use string ?

std::string a;
std::getline(std::cin, a);
std::cout<<a;

Apr 12 '06 #3

P: n/a
A String also doesn't work

Apr 22 '06 #4

P: n/a

ir******@gmail.com wrote:
A String also doesn't work


Please quote some context in your message. To do so from Google Groups,
see this link
http://cfaj.freeshell.org/google/

Victor and Jaspreet both suggested reading into a std::string using
std::getline. That will do what you want. On the other hand, this

std::string s;
std::cin << s;

will have exactly the same problem as your original code (although it
is better than your original code for a different reason). If you can't
get std::getline to do what you want, this link explains how to get
help here.
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8

Gavin Deane

Apr 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.