Sign in to follow this  
iansane

space causeing problem in string vector

Recommended Posts

Hi, I made a program that lets the user enter a US State name. It compares the name to a vector list of all the states to make sure it is one of the United States before adding it to the users list. To check it, I use an iterator to go through the complete list and then compare it to the users state name. for (myiter = states.begin(); myiter != state.end(); ++myiter) if (*myiter == userState) cout << "yes that is a state"; userList.push_back(userState); This works for single word state names, but for the ones with space like "New York", it tells me this is not a state. (That part is next in the code after the above if statement) If I use an iterator to display the whole list, it displays "New York" like it should. It just doesn't find it when the above code searches for it. Can anyone tell me why the problem with spaces? Thanks

Share this post


Link to post
Share on other sites
Sorry, nevermind.

It was the same problem I had before for another issue.

needed to use getline (cin, userState);

instead of cin >> userState;

Sorry for wasting space on the forum.

Is there really any reason not to just always use getline()?

How to know when to use cin >> and when to use getline() instead?

This simple little thing is frustrating since 5 chapters in my book have used cin >> and never mentioned getline()

lol, I didn't know it existed till someone here said "have you tried getline()" for another issue I was having.

Share this post


Link to post
Share on other sites
Its pretty simple. Use getline() when you want a line, and stream extraction when you want whitespace as a data delimiter. You can use getline() for things that aren't newlines. For example, std::getline(in,string,',') will read comma separated values.

Likewise, there can be cases where you combine the above. Using std::getline to read an entire line, and then use std::stringstream to extract individual items from the string in memory.

Share this post


Link to post
Share on other sites
Thanks for the info on comma seperated. I actually have a planned project that creates .csv files. I hadn't started trying to figure it out yet but I'll remember that.

I had to go look up the word delimiter. Now that I know what it is, why would I ever want one? What's an example of a use where I want a data delimiter? Sorry to be so newb but neither my instructor or the book I'm useing explains stuff like this.

Share this post


Link to post
Share on other sites
There are many reasons. Having everything on a new line can look ugly when you want to edit the file in a text editor, so sometimes a newline is a poor choice.

Using a particular character could be problematic if the data is expected to contain text strings with arbitrary contents. Consider if the text contains the delimiter for some reason (accidental or malicious). Of course, there is the possibility that you are reading a foreign file format: you may not be able to make such a decision, it is made for you.

Consider C++, it uses the semicolon as a statement delimiter. Some languages don't, they use a newline, or some other character (for example, Prolog uses a full stop as a delimiter).

I hope this gives you a few ideas.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this