473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Stroustrup chapter 3 - 3.6

i have this code. it works but its behaviour is strange:
------------------------------------------------------------
#include <iostream>
#include <string>

int main() {
std::string s1;
std::string s2;

std::cout << "Please enter your name: ";
std::cin >s1;
std::cout << "hello " << s1 << "\n";

std::cout << "now using GETLINE(): ";

getline(std::cin, s2);
std::cout << "hi babe " << s2 << " ;-) \n";
}

OUTPUT (with one word as input):

[arnuld@localhost cpp]$ ./a.out

Please enter your name: arnuld
hello arnuld
now using GETLINE(): hi babe ;-)

OUTPUT (with 2 words as input):

Please enter your name: arnuld fraser
hello arnuld
now using GETLINE(): hi babe fraser ;-)
[arnuld@localhost cpp]$

-----------------------------------------------------------------
i expected:

1.) if i enter 1 word then it will print it /hello word/ & it will ask
me for another when it will hit /getline/ but it doesn't.

2.) with 2 words i just expect /hello 1st word/ & then after hitting
/getline/ it will ask for another input.

but behaviour is not like this. can somebody explain?

i am using "g++ version 4.1.1" on "BLAG Linux 50002".

thanks
-- arnuld
http://arnuld.blogspot.com

Nov 5 '06 #1
4 1584
arnuld wrote:
i have this code. it works but its behaviour is strange:
------------------------------------------------------------
#include <iostream>
#include <string>

int main() {
std::string s1;
std::string s2;

std::cout << "Please enter your name: ";
std::cin >s1;

The above statement reads the FIRST word from 'cin'.
If more than one word is pending on 'cin', the the call
to getline() below will read the remaining word(s).
If 'arnuld fraser' is entered in reply to the '...name:'
prompt above, then 's1' will contain 'arnuld' and
'fraser' will still be pending on 'cin' - and will
be read by getline() below.

std::cout << "hello " << s1 << "\n";

std::cout << "now using GETLINE(): ";

getline(std::cin, s2);
std::cout << "hi babe " << s2 << " ;-) \n";
}

OUTPUT (with one word as input):

[arnuld@localhost cpp]$ ./a.out

Please enter your name: arnuld
hello arnuld
now using GETLINE(): hi babe ;-)

OUTPUT (with 2 words as input):

Please enter your name: arnuld fraser
hello arnuld
now using GETLINE(): hi babe fraser ;-)
[arnuld@localhost cpp]$

-----------------------------------------------------------------
i expected:

1.) if i enter 1 word then it will print it /hello word/ & it will ask
me for another when it will hit /getline/ but it doesn't.

2.) with 2 words i just expect /hello 1st word/ & then after hitting
/getline/ it will ask for another input.

but behaviour is not like this. can somebody explain?

i am using "g++ version 4.1.1" on "BLAG Linux 50002".

thanks
-- arnuld
http://arnuld.blogspot.com
Nov 5 '06 #2
The above statement reads the FIRST word from 'cin'.
If more than one word is pending on 'cin', the the call
to getline() below will read the remaining word(s).
If 'arnuld fraser' is entered in reply to the '...name:'
prompt above, then 's1' will contain 'arnuld' and
'fraser' will still be pending on 'cin' - and will
be read by getline() below.
ok, i got it but why it does not call /getline/ when i input one word

-- arnuld
http://arnuld.blogspot.com

Nov 5 '06 #3
arnuld wrote:
>The above statement reads the FIRST word from 'cin'.
If more than one word is pending on 'cin', the the call
to getline() below will read the remaining word(s).
If 'arnuld fraser' is entered in reply to the '...name:'
prompt above, then 's1' will contain 'arnuld' and
'fraser' will still be pending on 'cin' - and will
be read by getline() below.

ok, i got it but why it does not call /getline/ when i input one word

-- arnuld
http://arnuld.blogspot.com
It does call getline() when only one word was entered in
reply to the '...name:' prompt, but all that is left for
getline() to read is the newline that followed the single
word (i.e. getline() reads an empty line). For example,
if you entered 'arnuld', then the input buffer for 'cin'
contains "arnuld\n". the statement:

std::cin >s1;

will read "arnuld" from the input and place it into 's1'.
Now the input buffer for 'cin' contains only "\n".
getline() reads up thru the next newline ("\n"), so
getline() reads the "\n" from the 'cin' input buffer as
a blank line.
Nov 5 '06 #4
Larry Smith wrote:
It does call getline() when only one word was entered in
reply to the '...name:' prompt, but all that is left for
getline() to read is the newline that followed the single
word (i.e. getline() reads an empty line). For example,
if you entered 'arnuld', then the input buffer for 'cin'
contains "arnuld\n". the statement:

std::cin >s1;

will read "arnuld" from the input and place it into 's1'.
Now the input buffer for 'cin' contains only "\n".
getline() reads up thru the next newline ("\n"), so
getline() reads the "\n" from the 'cin' input buffer as
a blank line.
i did not get that when i read it first. then i tried what you told me
on the Emacs /eshell/ with one more /getline/ & i saw it asking for
input. that way i got it "larry\n" gives us

1.) larry
2.) \n

& hence 3rd /getline/ does not find anything & it asks for input.

hey thanks Larry. now i understood it.

-- arnuld
http://arnuld.blogspot.com

Nov 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
11
by: Steven T. Hatton | last post by:
In TC++PL(SE) Stroustrup asserts there is a companion reference called _The Annotated C++ Language Standard_ by Koenig & Stroustrup. It doesn't show up on a google. What gives here? I know there...
1
by: Mike | last post by:
Hi, I'm auctioning the book "The C++ Programming Language" 3rd Ed. by Bjarne Stroustrup on ebay, details as follows or see eBay item number 250030480853. Softback. Condition : Good. Pub....
11
by: arnuld | last post by:
this is the code which runs without any trouble: ----------------------------------------------------- #include <iostream> #include <string> #include <vector> struct Entry { std::string...
14
by: arnuld | last post by:
Stroustrup starts chapter 6 with a programme for desk-calculator: here is a grammer for the langugae accepted by the calcualtor: program: END // END is end-of-input...
14
by: arnuld | last post by:
i have 2 problems: 1.) in section 4.2 he uses: bool is_open(File*) i want to know why he uses the pointer, instead of these 2: bool is_open(File) or bool is_open(File&)
0
by: arnuld | last post by:
Stroustrup has a table in section 4.9 of declarations and definitions. he asks to write a similar table but in opposite sense: char ch; // declaration with definition he asks to do the...
0
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 //...
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
1
by: blangela | last post by:
Bjarne Stroustrup has a new text coming out called "Programming: Principles and Practice Using C++" (ISBN: 0321543726), due to be published in December of this year. Some of the features of this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.