Using return value of cin to take unknown number of inputs in C++ Last Updated : 23 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Consider a problem where we need to take an unknown number of integer inputs. A typical solution is to run a loop and stop when a user enters a particular value. How to do it if we are not allowed to use if-else, switch-case, and conditional statements? The idea is to use the fact that 'cin >> input' is false if the non-numeric value is given. Note that the above approach holds true only when the input value's data type is int (integer). Important Point: cin is an object of std::istream. In C++11 and later, std::istream has a conversion function explicit bool() const;, meaning that there is a valid conversion from std::istream to bool, but only where explicitly requested. An if or while counts as explicitly requesting conversion to bool. [Source StackOVerflow] Before C++ 11, std::istream had a conversion to operator void*() const; C++ // C++ program to take unknown number // of integers from user. #include <iostream> using namespace std; int main() { int input; int count = 0; cout << "To stop enter anything except integer"; cout << "\nEnter Your Input::"; // cin returns false when anything // is entered except integer while (cin >> input) count++; cout << "\nTotal number of inputs entered: " << count; return 0; } //This code is updated by Susobhan Akhuli Output: To stop enter any character Enter Your Input 1 2 3 s Total number of inputs entered: 3 Time Complexity: O(count)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article A creative C++ program to Zoom digits of an integer A AdityaRakhecha Improve Article Tags : Misc C/C++ Puzzles C++ cpp-input-output cpp-puzzle +1 More Practice Tags : CPPMisc Similar Reads How to Read and Print an Integer value in C++ The given task is to take an integer as input from the user and print that integer in C++ language. In this article, we will learn how to read and print an integer value. In the below program, the syntax and procedures to take the integer as input from the user is shown in C++ language. Standard Inp 2 min read How to use getline() in C++ when there are blank lines in input? In C++, if we need to read a few sentences from a stream, the generally preferred way is to use the getline() function as it can read string streams till it encounters a newline or sees a delimiter provided by the user. Also, it uses <string.h> header file to be fully functional. Here is a sam 2 min read Print a character n times without using loop, recursion or goto in C++ Given a character c and a number n, print the character c, n times. We are not allowed to use loop, recursion, and goto. Examples : Input : n = 10, c = 'a'Output : aaaaaaaaaa Input : n = 6, character = '@'Output : @@@@@@ In C++, there is a way to initialize a string with a value. It can be used to p 2 min read How to Take Input in Array in C++? Arrays in C++ are derived data types that can contain multiple elements of the same data type. They are generally used when we want to store multiple elements of a particular data type under the same name. We can access different array elements using their index as they are stored sequentially in th 3 min read A creative C++ program to Zoom digits of an integer Write a C (or C++) program to ZOOM (magnify) the digits of an integer. It should take an integer from the user and display each digit of the integer in magnified form using some pattern. Examples: Input : 123 Output : @ @@ @ @ @@@@@ ------------------------------- @@@@ @ @ @ @ @@@@ ----------------- 4 min read transform_inclusive_scan() function in C++ transform_inclusive_scan() is inbuilt function in C++ and its same as inclusive_scan(), except a unary function which is first applied to each input item.Its functionality is to transform each and every element between first and last with unary_op then computes an inclusive prefix sum operation by t 3 min read Like