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
42
votes
2 answers

‘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-expression before ‘>’ token no matching function for…
Matt Munson
  • 2,903
  • 5
  • 33
  • 52
41
votes
7 answers

Press Enter to Continue

This doesn't work: string temp; cout << "Press Enter to Continue"; cin >> temp;
Elliot
  • 6,086
  • 11
  • 45
  • 57
37
votes
4 answers

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 if I do this while(std::cin >> value1) My code will be…
gigi
  • 669
  • 6
  • 11
34
votes
8 answers

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!!!"<
Narek
  • 38,779
  • 79
  • 233
  • 389
32
votes
3 answers

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

For example, #include int main() { unsigned n{}; std::cin >> n; std::cout << n << ' ' << (bool)std::cin << std::endl; } When input -1, clang 6.0.0 outputs 0 0 while gcc 7.2.0 outputs 4294967295 1. I'm wondering who is correct. Or…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
31
votes
7 answers

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) here is what I have right now if (cin >> gpa) { …
Bryce Hahn
  • 1,585
  • 4
  • 16
  • 26
29
votes
4 answers

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 cin, if the input is not a number, then are there…
chanwcom
  • 4,420
  • 8
  • 37
  • 49
23
votes
5 answers

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 years old!". "x" being cin >> age; I write the code like…
JimGordon
  • 385
  • 3
  • 8
23
votes
2 answers

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 instead of a number the console goes crazy. How…
user2630617
21
votes
3 answers

Hide user input on password prompt

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 cout, so that what I type into the console doesn't…
dom0
  • 7,356
  • 3
  • 28
  • 50
19
votes
2 answers

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

#include using namespace std; int main() { string s; cin >> s; cout << "Hello World!"; } This isn't working. Why?
Ivan
  • 1,801
  • 2
  • 23
  • 40
19
votes
6 answers

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 valid number cout << "Invalid Input! Please input a numerical…
Hristo
  • 45,559
  • 65
  • 163
  • 230
18
votes
7 answers

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?
Allan
  • 4,562
  • 8
  • 38
  • 59
18
votes
4 answers

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 data piece in its own cell in a data structure I…
user313
  • 681
  • 1
  • 8
  • 21
17
votes
2 answers

Reading piped input with C++

I am using the following code: #include using namespace std; int main(int argc, char **argv) { string lineInput = " "; while(lineInput.length()>0) { cin >> lineInput; cout << lineInput; } return 0; } With…
user181351