in strings, what should i use? cin.getline or cin or getline(cin, var)?
right now i'm using cin to take in 3 variables and then rearranging them. however if only 2 variables are filled in, then it should execute my instructions. however cin waits for the third variable to be inputted before proceeding. which i understand. what can i use to take in 3 variables but if 2 are given, then it should proceed?
i was thinking of searching for a "null" string somehow and then use a conditional if statement.
should i use getline to take the whole line as a string and then divide it accordingly?
Use getline(cin, var) if var is a C++ string. Use an empty string instead of null unless an empty string is valid input. If you have three getlines, and the user just hits enter on the last one, then the 3rd string variable will be empty.
i can use the str.empty() function to test to see if a variable is empty. so that part is easy.
however, i want to take in 3 variables in one line of input: first middle last. however i want to test the middle and last to see if either one is empty.
is there any variation of cin or get or even getline that i can use?
Did you read the FAQ on getting a line of input from the user?
*shakes magic 8-ball"
"My sources say no."
take a look at getline's prototype: http://www.cppreference.com/cppstring/getline.html
if that's too confusing, they break it down further in the other getline's prototype
it's probably better to explicitly ask the user for their first, middle (optional), and last name seperately though... people do stupid things when you give them the freedom to.
You don't need a variation. Just call getline and then check the string to see if its empty.
The user will have to hit enter for each one, even if they don't type anything. If you don't want the user to have to hit enter for the second and third ones if they don't want to, then that is much more difficult and involves platform specific stuff.
no... if I understand correctly, what he wants is the user to enter:
<firstname> <middlename> <lastname>
on the same line before hitting enter - and he wants to be able to differentiate between the two inputs.
and the whole not hitting enter for the last one if they don't want to thing, there's nothing platform-specific about it... just whenever they enter an empty string, don't ask them for input anymore.
Gotcha. And you're right, it's probably better to ask for each separately.
I'd use getline to read the line and use a stringstream to parse each word in the line.
yes. thanks daved. that's exactly what i'm thinking of.
let's say that i have one whole line of strings. how can i separate strings from a line of strings?
Sorry to just give code without explanation, but here you go:
std::string line("This is a line of text.");
while (istrm >> word)
std::cout << word << std::endl;
std::cout << "---Done---" << std::endl;
is there another way to do it besides streaming? i've done it using characters but i guess it's best to do it using strings.
is there a way to convert chars to strings?
There are other ways to do it, but IMO that way is the easiest. You'd probably want to give more specifics on what you are working with, like example code, so we can help you with alternative solutions.
so far my program accomplishes the task. whereas it takes in FIRST MIDDLE LAST names and outputs LAST, FIRST M.
i'm taking the whole line via getline and then searching for spaces. then using the spaces as markers to output from one marker to the other.
right now my line is declared as "char line" but i would like to do it via strings "string first, middle, last".
Whichever way you are doing it now with C style strings you can also do it the same way with C++ strings. However, the stringstream might be easier.
Look at the example. A stringstream works just like a file stream or cin. Just load in the first name by calling stream >> firstname, then the middle name by stream >> middlename, etc. You can also check the return value of those operations to tell if the user didn't type enough words.
to be honest with you...we haven't covered it in class so i don't know if i'm allowed to stream it. although you did open my mind up to other possibilities...