take 2 inputs if 2 words are type in one line

i was wondering if you can take in 2 different inputs separated by a space only if there are 2 words for example if i type "word 50"
it will take in word and the number 50
but if i type just"word" it will just take in word and ignore looking for the second input
You can if you are reading into a string with istream::getline.

http://www.cplusplus.com/reference/iostream/istream/getline/

But there are other methods as well. In what context are you talking? What are you trying to do? Have any code we can look at? What exactly is the problem or solution you are trying to solve/code for?
ill lay it out for you the actual code is confusing to just pick out what it is revolving around.

i have a class Command which has for example a help function and the function will only be called if "/help" is typed.
there are also "/addSTR" and etc function that require a number input
for example must be called like this in the console /addSTR 123

in the main
at the moment the program is asking "what class do you want to be?" you can only type in warrior; then it will show the current stats of the warrior.


i want to be able to type for example "warrior" to answer the question or type a command like /addSTR 123 but if i type just "warrior" it will only take the word warrior and not go looking for a second word or number

i have tried scanf("%s %d",&input,&value); but when i type in warrior the first time it looks for a second input
Last edited on
Yup, getline is your function then. You can read an entire line into a string then use string functions, or your own, to compare the string to your commands and execute the proper ones. Or if you want to use string objects you can use it's getline function. Here is the reference for that:

http://www.cplusplus.com/reference/string/getline/
Last edited on
how would i separate the words after i take it in ?
Read up on stringstreams.
Wait, are you doing this in C or C++, just noticed you used scanf which looks like C to me.
c++
scanf works in c++ also just that its not exactly what i want
Topic archived. No new replies allowed.