Questions tagged [cin]

std::cin is the global stream object provided by the C++ standard library for reading from the standard input stream.

For more general questions about std::istream use the or tags.

1859 questions
215
votes
8 answers

std::cin input with spaces?

#include std::string input; std::cin >> input; The user wants to enter "Hello World". But cin fails at the space between the two words. How can I make cin take in the whole of Hello World? I'm actually doing this with structs and…
dukevin
  • 22,384
  • 36
  • 82
  • 111
133
votes
14 answers

How do I flush the cin buffer?

How do I clear the cin buffer in C++?
Tal
120
votes
4 answers

Why would we call cin.clear() and cin.ignore() after reading input?

Google Code University's C++ tutorial used to have this code: // Description: Illustrate the use of cin to get input // and how to recover from errors. #include using namespace std; int main() { int input_var = 0; // Enter the do…
JacKeown
  • 2,780
  • 7
  • 26
  • 34
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
83
votes
6 answers

How do I deal with the max macro in windows.h colliding with max in std?

So I was trying to get valid integer input from cin, and used an answer to this question. It recommended: #include // includes WinDef.h which defines min() max() #include using std::cin; using std::cout; void Foo() { int…
Almo
  • 15,538
  • 13
  • 67
  • 95
64
votes
22 answers

How to cin values into a vector

I'm trying to ask the user to enter numbers that will be pushed into a vector, then using a function call to count these numbers. why is this not working? I'm only able to count the first number. template void write_vector(const…
Sean
  • 957
  • 2
  • 11
  • 15
62
votes
9 answers

if (cin >> x) - Why can you use that condition?

I have been using "Accelerated C++" to learn C++ over the summer, and there's a concept which I don't seem to understand properly. Why is int x; if (cin >> x){} equivalent to cin >> x; if (cin){} By looking at the code, it seems to me that we're…
Muhsin Ali
  • 621
  • 1
  • 6
  • 3
56
votes
2 answers

Why does stringstream >> change value of target on failure?

From Stroustrup's TC++PL, 3rd Edition, Section 21.3.3: If we try to read into a variable v and the operation fails, the value of v should be unchanged (it is unchanged if v is one of the types handled by istream or ostream member functions). The…
user1823664
  • 1,071
  • 9
  • 16
52
votes
4 answers

changing the delimiter for cin (c++)

I've redirected "cin" to read from a file stream cin.rdbug(inF.rdbug()) When I use the extraction operator it reads until it reaches a white space character. Is it possible to use another delimiter? I went through the api in cplusplus.com, but…
yotamoo
  • 5,334
  • 13
  • 49
  • 61
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
46
votes
3 answers

cin.ignore(numeric_limits::max(), '\n')

What does cin.ignore(numeric_limits::max(), '\n') mean in C++? Does it actually ignore the last input from the user?
Zyi
  • 473
  • 1
  • 5
  • 4
44
votes
3 answers

Multiple inputs on one line

I have looked to no avail, and I'm afraid that it might be such a simple question that nobody dares ask it. Can one input multiple things from standard input in one line? I mean this: float a, b; char c; // It is safe to assume a, b, c will be in…
Joshua
  • 4,270
  • 10
  • 42
  • 62
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
4 answers

C++: how do I check if the cin buffer is empty?

How do you check to see if the user didn't input anything at a cin command and simply pressed enter?
Matt Shindala
  • 423
  • 1
  • 4
  • 4
1
2 3
99 100