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.

1801 questions
2159
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
  • 9,547
  • 8
  • 48
  • 53
96
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
  • 1,114
  • 1
  • 9
  • 9
71
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
  • 2,071
  • 2
  • 21
  • 27
68
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
  • 22,386
  • 64
  • 200
  • 328
54
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
  • 872
  • 1
  • 8
  • 14
52
votes
3 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
  • 19,899
  • 10
  • 30
  • 43
50
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
  • 3,243
  • 13
  • 56
  • 93
49
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
  • 6,373
  • 11
  • 42
  • 52
43
votes
5 answers

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

When should std::cin.getline() be used? What does it differ from std::cin?
Simplicity
  • 47,404
  • 98
  • 256
  • 385
42
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,077
  • 8
  • 27
  • 48
41
votes
9 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
  • 503
  • 1
  • 4
  • 10
31
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
  • 854,327
  • 234
  • 1,573
  • 1,953
31
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
28
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
  • 22,386
  • 64
  • 200
  • 328
26
votes
4 answers

c++ Read from .csv file

I have this code which is supposed to cout in console the information from the .csv file; while(file.good()) { getline(file, ID, ','); cout << "ID: " << ID << " " ; getline(file, nome, ',') ; cout << "User: " << nome << " " ; …
Mr. Phil
  • 1,099
  • 2
  • 14
  • 31
1
2 3
99 100