fetching two numbers separated by a whitespace (noob question)

J

johnmcmadeup

How can I make it so that, if the user inputs 2 numbers with a space
in between , I can get both numbers(instead of just the first number,
which happens when I try cin>>var;)?
 
I

Ian Collins

How can I make it so that, if the user inputs 2 numbers with a space
in between , I can get both numbers(instead of just the first number,
which happens when I try cin>>var;)?

std::cin >> var1 >> var2;
 
J

Jim Langston

Thanks a lot. I always though that that would force the user to hit
enter between the 2 numbers.

Any whitespace will do, space, tab, enter. In fact if it's a number any non
number will cause the first input to stop. This may cause the var2 to not
get a value if it's not a number. I.E. If the user enters
1x2
var2 is undefined.

Also, if you want all the numeric digits in one variable you could use a
std::string and getline. I.E.

std::string Line;
std::getline( std::cin, Line );

That will accept all characters types into Line until the user presses
enter. But then it's a string, not a number.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top