Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > fetching two numbers separated by a whitespace (noob question)

Reply
Thread Tools

fetching two numbers separated by a whitespace (noob question)

 
 
johnmcmadeup@gmail.com
Guest
Posts: n/a
 
      04-05-2008
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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      04-05-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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;

--
Ian Collins.
 
Reply With Quote
 
 
 
 
johnmcmadeup@gmail.com
Guest
Posts: n/a
 
      04-05-2008
On Apr 5, 11:32*pm, Ian Collins <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > 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;
>
> --
> Ian Collins.


Thanks a lot. I always though that that would force the user to hit
enter between the 2 numbers.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-06-2008
(E-Mail Removed) wrote:
> On Apr 5, 11:32 pm, Ian Collins <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> 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;

>
> 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.


--
Jim Langston
(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Splitting text at whitespace but keeping the whitespace in thereturned list MRAB Python 3 01-26-2010 11:36 PM
Structure using whitespace vs logical whitespace cmdrrickhunter@yaho.com Python 10 12-16-2008 03:51 PM
Re: Write two separated objects? Peter Nilsson C Programming 3 04-14-2008 03:52 AM
newbie regular expression question numbers/strings separated by : djbitchpimp@snowboard.com Perl Misc 6 07-04-2005 11:05 PM
Whitespace where I don't want whitespace! Oli Filth HTML 9 01-17-2005 08:47 PM



Advertisments