new to c++, working with vectors my question is can a string in a vector hold for example a string like " super mario bros" ( with the spaces)

to continue i am trying to to make a list that people can add to , and if user where to select add option and then decide to lets say want to add " super mario bros" then in another part of my code i have the option to list all things on list, but when i do this what comes out on the list is just "super" not all user inputs which was "super mario bros"

so i guess my question is, is my code not allowing this or is it just not possible for it to hold multi-words?

thanks alot

Recommended Answers

All 3 Replies

A string object can store strings including whitespaces. I would guess that your problem is that you only read the first word the user inputs and ignores the rest. If you could provide the relevant parts of your code it would be much easier to help you, especially the part that handles the input from the user.

In answer to your question about vectors, a vector of std::strings can hold any number of valid std::string objects. Whether or not they contain whitespace is completely irrelevant. As long as a string object is valid, the vector should be able to contain it!

Obviously it seems me that you think your vector is causing your problem, somehow only storing only the first word of each string your user enters. But in actual fact, you're almost certainly using an inappropriate method/function to get your string input from the user in the first place.

How strings are retrieved from the input stream and stored will depend on which function you use to get them from the user.

From what I've seen I'm assuming you're probably using std::cin or something similar which copies everything up to the first whitespace character, or the first return character (whichever is first) into the string object.

std::string str;
std::cout << "enter a string: ";
std::cin >> str;
std::cout << "You entered: " << str << std::endl;

Using the above code snippet, if you entered 'super mario bros', because cin only gets everything up to the first whitespace character, the string will only contain 'super'. So when you subsequently push your string into the vector, the only thing that goes into the vector is the string 'super'.

In other words, if you are using std::cin to get strings in your program, then you are getting exactly what you asked for, but perhaps not what you intended!

Whereas if you use std::getline it will get an entire line of text. In other words everything (including whitespace) up to the first return character.

std::string str;
std::cout << "Enter a string: ";
std::getline(std::cin, str);
std::cout << "You entered: " << str << std::endl;

Using the above code, if you entered 'super mario bros', the entire string would be stored in str, spaces and all! Pushing this string into your vector shouldn't be a problem either.

std::getline is part of the std::string library, so as long as you #include <string> you should be good to go!

When getting any kind of input from the user you need to use the most appropriate method. When it comes to strings, if you only want to get a single word, by all means use std::cin. But if you want to get an entire line of text, you should use std::getline.

Personally, in my console-mode applications I tend to get ALL user input as strings using std::getline (even if I want an int or a float value). I then parse the input to ensure it is in the expected format before using it. Once it's been validated, I can then convert it to a different type (using atoi or atof etc..) if required. It may seem a bit heavy-handed, but it reduces the risk that a user can spuriously enter something that may corrupt data or crash the program!

You might also want to look into stringstreams. They can sometimes be handy when dealing with input strings too! (For example, if you need to tokenise a long string containing whitespace.)

Cheers for now,

commented: Very nice explanation! It does some nice critical thinking of what you could possibly assume what the OP was requesting and revealing the steps to fix the entire logical problem instead of a simple code snippet. +2 if possible. +1
commented: Agreed. Good to see you posting again Jas! +7

JasonHippy thanks so much, so helpful exactly explained what was wondering and how fix. again thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.