Inserting a space in C++

I

irsmartt

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.
 
V

Victor Bazarov

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
 
J

Jaspreet

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;
 
G

Gavin Deane

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-lite/how-to-post.html#faq-5.8

Gavin Deane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top