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
17
votes
8 answers

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 is an integer or not? And if not, ask the user to…
user2766546
  • 185
  • 1
  • 1
  • 7
17
votes
4 answers

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

Can the following two lines be condensed into one? int foo; std::cin >> foo;
ayane_m
  • 962
  • 2
  • 10
  • 26
16
votes
1 answer

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 space: string result; cin >> result; If cin is…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
15
votes
3 answers

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 times. How I can do it?
tucnak
  • 406
  • 1
  • 3
  • 10
15
votes
5 answers

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 character transmitted by the Enter key to mark the…
Yu Zhou
  • 173
  • 1
  • 2
  • 10
14
votes
11 answers

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 operations } The code structure is something like the above one.…
user630983
  • 947
  • 2
  • 9
  • 16
14
votes
3 answers

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

I have these 2 codes: char a[256]; cin>>a; cout<
Ruben P
  • 191
  • 1
  • 1
  • 9
14
votes
3 answers

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 me no operator matches these operands. I have…
user3163612
  • 151
  • 1
  • 1
  • 4
14
votes
8 answers

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; std::string mylist[] = {"a", "b", "c"}; std::cin…
Sumyjkl
  • 141
  • 1
  • 1
  • 4
14
votes
3 answers

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

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 that the value input was of the correct datatype?…
Brook Julias
  • 2,085
  • 9
  • 29
  • 44
13
votes
2 answers

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++?
Baruch
  • 20,590
  • 28
  • 126
  • 201
13
votes
5 answers

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.
Tomek
12
votes
2 answers

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 true or false. But what happens if I give another…
Xema
  • 1,796
  • 1
  • 19
  • 28
12
votes
3 answers

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.
grant
  • 121
  • 1
  • 1
  • 3
12
votes
5 answers

Why getline() skipping input, even after cin.clear()?

So I have a function that keeps skipping over the first getline and straight to the second one. I tried to clear the buffer but still no luck, what's going on? void getData(char* strA, char* strB) { cout << "Enter String 1: "; //…
Derp
  • 921
  • 3
  • 14
  • 22