473,320 Members | 2,111 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,320 software developers and data experts.

getline

what's wrong with this program:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string string1, string2;

cout << "Type a word: ";
cin >> string1;
cout << "You typed: " << string1 << endl;

cout << "now type a line: ";
getline( cin, string2 ); <-------- doesn't wait for me here
cout << "you typed: " << string2 << endl;

return 0;
}

it compiles but when run it doesn't wait for me to enter a sentence at the
getline command

output:
Type a word: lol
You typed: lol
now type a line: you typed:

platform: mandrake linux using g++ on i386
Jul 22 '05 #1
2 3045
slurper wrote in news:40*********************@news.skynet.be in
comp.lang.c++:
what's wrong with this program:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string string1, string2;

cout << "Type a word: ";
cin >> string1;
this reads in "lol" but *not* the final '\n'.
cout << "You typed: " << string1 << endl;

cout << "now type a line: ";
getline( cin, string2 ); <-------- doesn't wait for
this reads in the '\n' that wasn't read above.
me here cout << "you typed: " << string2 << endl;

return 0;
}

it compiles but when run it doesn't wait for me to enter a sentence at
the getline command

output:
Type a word: lol
You typed: lol
now type a line: you typed:

platform: mandrake linux using g++ on i386

Standatd C++.

I suggest you use getline for both inputs, always do this when
reading from the "console" as its line buffered anyway.

Use the exctactors (>>) for stringstream's, and files that
has been writen by programmes (other than notepad/emacs/vi/ed).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

"slurper" <sl*********@hotmail.com> wrote in message
news:40*********************@news.skynet.be...
what's wrong with this program:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string string1, string2;

cout << "Type a word: ";
cin >> string1;
cout << "You typed: " << string1 << endl;

cout << "now type a line: ";
getline( cin, string2 ); <-------- doesn't wait for me here cout << "you typed: " << string2 << endl;

return 0;
}

it compiles but when run it doesn't wait for me to enter a sentence at the
getline command

output:
Type a word: lol
You typed: lol
now type a line: you typed:

platform: mandrake linux using g++ on i386


cin >> string1 does not read a newline.
getline reads the newline that follows lol, therefore it doesn't wait.

Try typing "hello world" at the first prompt to see this more clearly.

john
Jul 22 '05 #3

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

Similar topics

2
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline...
5
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program...
1
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I...
10
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert...
14
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
2
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip...
5
by: allspamgoeshere3 | last post by:
Hi! I'm having trouble reading text lines from a file describing the levels for a simple game I'm creating. Inside the function that reads the content of the file there is a loop that basically...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
3
by: JackC | last post by:
Hi, How do i use stringstreams getline function to extract lines from an existing string? Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n"; I want to extract each line out into a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.