tell cin to stop reading at newline
Use getline and istringstream:
#include <sstream>
/*....*/
vector<int> getclause() {
char c;
vector<int> cl;
std::string line;
std::getline(cin, line);
std::istringstream iss(line);
while ( iss >> c) {
cl.push_back(c);
}
return cl;
}
How to ignore line break after reading two ints with cin?
A good place to look for tips for common problems like this is your favorite reference:
When used immediately after whitespace-delimited input, e.g. after
int n; std::cin >> n;
,getline
consumes the endline character left on the input stream byoperator>>
, and returns immediately. A common solution is to ignore all leftover characters on the line of input withcin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
before switching to line-oriented input.
From here.
Why do cin and getline exhibit different reading behavior?
reading behavior of cin and getline.
cin
does not "read" anything. cin
is an input stream. cin
is getting read from. getline
reads from an input stream. The formatted extraction operator, >>
, reads from an input stream. What's doing the reading is >>
and std::getline
. std::cin
does no reading of its own. It's what's being read from.
first cin read up until the "\n". once it hit that "\n", it increments the
cursor to the next position
No it doesn't. The first >>
operator reads up until the \n
, but does not read it. \n
remains unread.
The second >>
operator starts reading with the newline character. The >>
operator skips all whitespace in the input stream before it extracts the expected value.
The detail that you're missing is that >>
skips whitespace (if there is any) before it extracts the value from the input stream, and not after.
Now, it is certainly possible that >>
finds no whitespace in the input stream before extracting the formatted value. If >>
is tasked with extracting an int
, and the input stream has just been opened and it's at the beginning of the file, and the first character in the file is a 1
, well, the >>
just doesn't skip any whitespace at all.
Finally, std::getline
does not skip any whitespace, it just reads from the input stream until it reads a \n
(or reaching the end of the input stream).
How can I take several integers as input until newline occurs?
Here is something that uses more C++ functions and data structures:
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::vector<int> database;
std::string text_line;
std::getline(std::cin, text_line);
std::istringstream input_stream(text_line);
int value;
while (input_stream >> value)
{
database.push_back(value);
}
// Edit 1: printing the vector
for (auto&& v:database)
{
std::cout << v << " ";
}
std::cout << "\n";
return EXIT_SUCCESS;
}
In the above program, a line of text (i.e. text until newline), is read into the string variable.
The string is then treated as an input stream and all the numbers in the string are placed into the vector.
The string helps to limit to only numbers input from the line of text.
Getline keeps on getting newline character. How can I avoid this?
Your cin >>N
stops at the first non-numeric character, which is the newline. This you have a getline
to read past it, that's good.
Each additional getline
after that reads the entire line, including the newline at the end. By putting in a second getline
you're skipping half your input.
new line character(enter key) in C++
If I understand your question and you want to trap the '\n'
character, then you need to use std::cin.get(letter)
instead of std::cin >> letter;
As explained in the comment, the >>
operator will discard leading whitespace, so the '\n'
left in stdin
is ignored on your next loop iteration.
std::cin.get()
is a raw read and will read each character in stdin
. See std::basic_istream::get For example:
#include <iostream>
int main (void) {
char letter;
while (std::cin.get(letter)) {
if (letter == '\n')
std::cout << "got newline\n";
}
}
Will generate a "got newline"
output after Enter is pressed each time.
Why does cin command leaves a '\n' in the buffer?
Because, when you say getline
you say you want to get a line... A line is string that ends with \n
, the ending is integral part of it.
When you say cin >> something
, you want to get precisely something
, nothing more. End-line marker is not part of it, so it's not consumed. Unless you would have a special type for line, but there is no such thing in standard library.
While without citations from standard this might be taken as opinion, but this is logic behind it. They have different semantics. There is also another difference getline
works as unformatted input, and operator>>
works as formatted input. I strongly suggest reading of those links to get the differences. This also says that they are semantically different.
Another answer, better or not is debatable, would be to quote standard, that I am sure says, how getline
behaves, and how operator>>
behaves for different types, and say, it works like this, because standard says so. This would be good, because the standard absolutely defines how things work and it can do so arbitrarily... And it rarely does explain motivation and logic behind the design.
Related Topics
Does the Gotw #101 "Solution" Actually Solve Anything
Why How to Not Brace Initialize a Struct Derived from Another Struct
Why Can't I Write to a String Literal While I *Can* Write to a String Object
Why Not Use Pointers for Everything in C++
How to Catch Python Stdout in C++ Code
Opencv 2.3 C - How to Isolate Object Inside Image
What Is the Datatype of String Literal in C++
C++: Is It Safe to Cast Pointer to Int and Later Back to Pointer Again
Destructor Being Called Twice When Being Explicitly Invoked
Unsequenced Value Computations (A.K.A Sequence Points)
Forward Declare a Standard Container
Why Is Initializing an Integer in C++ to 010 Different from Initializing It to 10
How to Append an Int to a String in C++
Fast N Choose K Mod P for Large N
Setupdigetdeviceproperty Usage Example