Questions tagged [getline]

A C function that reads an entire line from a stream. It was originally a GNU extension that was standardized in POSIX.1-2008.

1656 questions
1981
votes
10 answers

Why is reading lines from stdin much slower in C++ than Python?

I wanted to compare reading lines of string input from stdin using Python and C++ and was shocked to see my C++ code run an order of magnitude slower than the equivalent Python code. Since my C++ is rusty and I'm not yet an expert Pythonista, please…
JJC
  • 7,445
  • 6
  • 39
  • 51
75
votes
7 answers

When and why do I need to use cin.ignore() in C++?

I wrote a very basic program in C++ which asked the user to input a number and then a string. To my surprise, when running the program it never stopped to ask for the string. It just skipped over it. After doing some reading on StackOverflow, I…
Raddicus
  • 904
  • 1
  • 8
  • 9
66
votes
4 answers

Going through a text file line by line in C

I have been working on a small exercise for my CIS class and am very confused by the methods C uses to read from a file. All that I really need to do is read through a file line by line and use the information gathered from each line to do a few…
Dan Bradbury
  • 1,963
  • 2
  • 20
  • 26
63
votes
1 answer

reading a line from ifstream into a string variable

In the following code : #include #include #include using namespace std; int main() { string x = "This is C++."; ofstream of("d:/tester.txt"); of << x; of.close(); ifstream read("d:/tester.txt"); …
Suhail Gupta
  • 19,563
  • 57
  • 170
  • 298
52
votes
7 answers

Using getline() in C++

I have a problem using getline method to get a message that user types, I'm using something like: string messageVar; cout << "Type your message: "; getline(cin, messageVar); However, it's not stopping to get the output value, what's wrong with…
MGE
  • 802
  • 1
  • 7
  • 13
47
votes
13 answers

Using getline(cin, s) after cin

I need the following program to take the entire line of user input and put it into string names: cout << "Enter the number: "; int number; cin >> number; cout << "Enter names: "; string names; getline(cin, names); With the cin >> number command…
pauliwago
  • 5,359
  • 10
  • 36
  • 48
47
votes
4 answers

cin and getline skipping input

earlier i posted a question about cin skipping input, and I got results to flush, and use istringstream, but now I tried every possible solution but none of them work. here is my code: void createNewCustomer () { string name, address; cout…
hakuna matata
  • 2,831
  • 11
  • 46
  • 85
42
votes
2 answers

checking for eof in string::getline

How do I check for end-of-file using the std::getline function? If I use eof() it won't signal eof until I attempt to read beyond end-of-file.
assassin
  • 16,522
  • 10
  • 27
  • 41
37
votes
5 answers

std::cin.getline( ) vs. std::cin

When should std::cin.getline() be used? What does it differ from std::cin?
Simplicity
  • 41,347
  • 81
  • 231
  • 358
32
votes
8 answers

c++ getline() isn't waiting for input from console when called multiple times

I'm attempting to get a few user-input parameters from the console, two strings, two ints and a double. The relevant code I'm trying to use is this: #include #include using namespace std; // ... string inputString; unsigned int…
user754852
  • 413
  • 1
  • 4
  • 9
27
votes
6 answers

Why are there two different getline() functions (if indeed there are)?

Every time I do a quick snippet of C++ code line std::string s; cin >> s; I curse myself because I forgot it stops at the whitespace rather than getting an entire line. Then, on remembering getline, I invariably become confused as to the two…
paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
26
votes
3 answers

getline() does not work if used after some inputs

Possible Duplicate: Need help with getline() getline() is not working, if I use it after some inputs, i.e. #include using namespace std; main() { string date,time; char…
Muhammad Arslan Jamshaid
  • 1,246
  • 6
  • 23
  • 44
25
votes
4 answers

std::getline() returns

I have a loop that reads each line in a file using getline(): istream is; string line; while (!getline(is, line).eof()) { // ... } I noticed that calling getline() like this also seems to work: while (getline(is, line)) What's going on here?…
Ferruccio
  • 93,779
  • 37
  • 217
  • 294
23
votes
3 answers

getline not working properly ? What could be the reasons?

Possible Duplicate: getline not asking for input? There is some unique thing happening in my program. Here are some set of commands : cout << "Enter the full name of student: "; // cin name getline( cin , fullName ); cout << "\nAge: "; //…
Suhail Gupta
  • 19,563
  • 57
  • 170
  • 298
22
votes
4 answers

Can I use 2 or more delimiters in C++ function getline?

I would like to know how can I use 2 or more delimiters in the getline functon, that's my problem: The program reads a text file... each line is goning to be like: New Your, Paris, 100 CityA, CityB, 200 I am using getline(file, line), but I…
user6185425
1
2 3
99 100