Questions tagged [cin]

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

Filter by
Sorted by
Tagged with
193votes
8answers
640kviews

std::cin input with spaces?

#include <string> 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 ...
user avatar
  • 20.9k
125votes
13answers
351kviews

How do I flush the cin buffer?

How do I clear the cin buffer in C++?
user avatar
105votes
4answers
250kviews

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 <iostream> using namespace std; ...
user avatar
  • 2,480
83votes
7answers
265kviews

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. ...
user avatar
  • 984
77votes
5answers
36kviews

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 <Windows.h> // includes WinDef.h which defines min() max() #include <...
user avatar
  • 15.1k
58votes
9answers
61kviews

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; ...
user avatar
57votes
21answers
254kviews

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. ...
user avatar
  • 877
55votes
2answers
3kviews

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 ...
user avatar
  • 1,031
49votes
4answers
46kviews

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? ...
user avatar
  • 5,124
48votes
4answers
138kviews

cin and getline skipping input [duplicate]

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 ...
user avatar
48votes
13answers
88kviews

Using getline(cin, s) after cin [duplicate]

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: "; ...
user avatar
  • 5,785
42votes
4answers
84kviews

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?
user avatar
40votes
7answers
103kviews

Press Enter to Continue

This doesn't work: string temp; cout << "Press Enter to Continue"; cin >> temp;
user avatar
  • 5,928
40votes
5answers
66kviews

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

When should std::cin.getline() be used? What does it differ from std::cin?
user avatar
  • 44.1k
40votes
2answers
167kviews

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; ...
user avatar
  • 4,140
40votes
3answers
53kviews

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

What does cin.ignore(numeric_limits<streamsize>::max(), '\n') mean in C++? Does it actually ignore the last input from the user?
user avatar
  • 413
38votes
2answers
46kviews

‘numeric_limits’ was not declared in this scope, no matching function for call to ‘max()’

I compiled this code at home on my mac w/ xcode and there was no provblem. I compile it at school with g++ on linux and I get these errors: numeric_limits’ is not a member of std expected primary-...
user avatar
  • 2,823
36votes
4answers
5kviews

How can std::cin return a bool and itself at the same time?

I'm reading a book on C++ that says that if I use the >> operator it returns the object at the left side of the operator so in this example std::cin >> value1; the code returns std::cin. But ...
user avatar
  • 659
32votes
3answers
1kviews

Should reading negative into unsigned fail via std::cin (gcc, clang disagree)?

For example, #include <iostream> int main() { unsigned n{}; std::cin >> n; std::cout << n << ' ' << (bool)std::cin << std::endl; } When input -1, clang 6.0....
user avatar
  • 14k
31votes
8answers
144kviews

How to cin Space in c++?

Say we have a code: int main() { char a[10]; for(int i = 0; i < 10; i++) { cin>>a[i]; if(a[i] == ' ') cout<<"It is a space!!!"<<endl; } ...
user avatar
  • 36.7k
28votes
4answers
32kviews

Infinite loop with cin when typing string while a number is expected

In the following loop, if we type characters as the cin input instead of numbers which are expected, then it goes into infinite loop. Could anyone please explain to me why this occurs? When we use ...
user avatar
  • 3,892
23votes
5answers
2kviews

How do I use cin between strings in C++?

I'm fairly new to C++ and have been learning things like this: cout << "My age is: "; cin >> age; Messing around with cin, I have come across a roadblock. Say I wanted to write "I am x ...
user avatar
  • 385
22votes
2answers
86kviews

Correct way to use cin.fail()

What is the correct way to use cin.fail();? I am making a program where you need to input something. It is not very clear if you need to input a number or character. When a user inputs a character ...
user avatar
19votes
2answers
47kviews

Why isn't cin >> string working with Visual C++ 2010? [closed]

#include <iostream> using namespace std; int main() { string s; cin >> s; cout << "Hello World!"; } This isn't working. Why?
user avatar
  • 1,721
19votes
7answers
111kviews

C++ round a double up to 2 decimal places

I am having trouble rounding a GPA double to 2 decimal places. (ex of a GPA needed to be rounded: 3.67924) I am currently using ceil to round up, but it currently outputs it as a whole number (368) ...
user avatar
  • 1,255
19votes
3answers
41kviews

Hide user input on password prompt [duplicate]

Possible Duplicate: Read a password from std::cin I don't work normally with the console, so my question is maybe very easy to answer or impossible to do . Is it possible to "decouple" cin and ...
user avatar
  • 7,118
18votes
6answers
31kviews

how do I validate user input as a double in C++?

How would I check if the input is really a double? double x; while (1) { cout << '>'; if (cin >> x) { // valid number break; } else { // not a ...
user avatar
  • 43.7k
17votes
2answers
25kviews

Reading piped input with C++

I am using the following code: #include <iostream> using namespace std; int main(int argc, char **argv) { string lineInput = " "; while(lineInput.length()>0) { cin >> ...
user avatar
17votes
7answers
21kviews

Read binary data from std::cin

What is the easiest way to read binary (non-formated) data from std::cin into either a string or a stringstream?
user avatar
  • 4,380
16votes
8answers
174kviews

Checking cin input stream produces an integer

I was typing this and it asks the user to input two integers which will then become variables. From there it will carry out simple operations. How do I get the computer to check if what is entered ...
user avatar
16votes
1answer
18kviews

How to read cin with whitespace up until a newline character?

I wish to read from cin in C++ from the current position up until a newline character into a string. The characters to be read may include spaces. My first pass fails because it stops on the first ...
user avatar
  • 38.3k
15votes
4answers
9kviews

Can a variable be initialized with an istream on the same line it is declared? [duplicate]

Can the following two lines be condensed into one? int foo; std::cin >> foo;
user avatar
  • 892
15votes
4answers
53kviews

Reading getline from cin into a stringstream (C++)

So I'm trying to read input like this from the standard input (using cin): Adam English 85 Charlie Math 76 Erica History 82 Richard Science 90 My goal is to eventually store each ...
user avatar
14votes
5answers
56kviews

difference between cin.get() and cin.getline()

I am new to programming, and I have some questions on get() and getline() functions in C++. My understanding for the two functions: The getline() function reads a whole line, and using the newline ...
user avatar
  • 163
14votes
11answers
48kviews

How to signify no more input for string ss in the loop while (cin >> ss)

I used "cin" to read words from input stream, which like int main( ){ string word; while (cin >> word){ //do sth on the input word } // perform some other ...
user avatar
14votes
3answers
47kviews

C++ cin char read symbol-by-symbol

I need to read symbol-by-symbol. But I don't know how to read until end of input. As exemple test system will cin>>somecharvariable m times. I have to read symbol-by-symbol all characters. Only m ...
user avatar
  • 396
13votes
3answers
46kviews

c++, how to verify is the data input is of the correct datatype [duplicate]

Possible Duplicate: how do I validate user input as a double in C++? I am new to C++, and I have a function in which I am wanting the user to input a double value. How would I go about insuring ...
user avatar
  • 2,015
13votes
8answers
37kviews

How to check if a string is in a list of strings?

In Python, doing if a in b is really easy, and I'm wondering if there's an equivalent in C++. Specifically, I want to make a list of strings and check if an input is in that list. std::string myinput; ...
user avatar
  • 131
13votes
3answers
69kviews

Difference between cin and cin.get() for char array

I have these 2 codes: char a[256]; cin>>a; cout<<a; and char a[256]; cin.get(a,256);cin.get(); cout<<a; and maybe, relative to the second one without cin.get(); char a[256]; cin....
user avatar
  • 181
12votes
3answers
23kviews

Is it possible to use cin with Qt?

Is it possible to use cin in Qt? I can use cout but cannot find examples of how to use cin within a Qt console application.
user avatar
  • 121
12votes
5answers
16kviews

C++ having cin read a return character

I was wondering how to use cin so that if the user does not enter in any value and just pushes ENTER that cin will recognize this as valid input.
user avatar
12votes
2answers
27kviews

cin and boolean input

I am new to C++ and I was wondering how the function cin in case of a boolean data works. Let's say for instance : bool a; cin >> a; I understand that if I give 0 or 1, my data a will be either ...
user avatar
  • 1,616
11votes
3answers
55kviews

cin >> "no operator matches these operands"

I've been working on a C++ project in visual studio 2012 console mode and I keep getting this strange persistent error with the cin function. Under the >> I get a red line and the program tells ...
user avatar
11votes
2answers
23kviews

Getting arrow keys from cin

I am sure this must have been asked before, but a quick search found nothing. How can I get the arrow/direction keys with cin in c++?
user avatar
  • 19k
11votes
4answers
11kviews

Using cin in QtCreator

For school, we use C++ as the language of choice. I am currently using QtCreator as an IDE, and for its GUI library, it is wonderful. The school is using Visual Studio. However, most of the programs ...
user avatar
10votes
2answers
560views

Does "cin" reset variable to some default value if input type differs from destination type? [duplicate]

I have an issue with behavior of "cin" (I do not understand). My IDE is Netbeans under Windows OS (with Cygwin). Here's a code example: int main() { int temp = -1; std::cin >> temp; ...
user avatar
10votes
4answers
41kviews

How Can I avoid char input for an int variable?

The program below shows a 'int' value being entered and being output at the same time. However, when I entered a character, it goes into an infinite loop displaying the previous 'int' value entered. ...
user avatar
  • 121
10votes
3answers
940views

Trying to push an unknown number of strings into a vector, but my cin loop doesn't terminate

I am trying to take strings as input from cin, and then push the string into a vector each time. However, my loop doesn't terminate even when I put a '\' at the end of all my input. int main(void) { ...
user avatar
10votes
6answers
10kviews

Problem of using cin twice

Here is the code: string str; cin>>str; cout<<"first input:"<<str<<endl; getline(cin, str); cout<<"line input:"<<str<<endl; The result is that getline never ...
user avatar
  • 395
9votes
3answers
37kviews

C++ - pointer being freed was not allocated error

malloc: *** error for object 0x10ee008c0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug Abort trap: 6 Or I get this when I try and print everything ...
user avatar

15 30 50 per page
1
2 3 4 5
36