cin

Correct way to use cin.fail()

不问归期 提交于 2019-11-26 20:46:54
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 can I use cin.fail() to fix this? Or is there a better way? Shumail cin.fail() returns true if the last cin command failed, and false otherwise. An example: int main() { int i, j = 0; while (1) { i++; cin >> j; if (cin.fail()) return 0; cout << "Integer " << i << ": " << j << endl; } } Now suppose you have a text file - input.txt and it's contents are:

2019 Multi-University Training Contest 6

Deadly 提交于 2019-11-26 20:29:34
2019 Multi-University Training Contest 6 B.Nonsense Time 首先有这样一个结论:随机生成序列的期望 \(LIS\) 长度为 \(O(\sqrt{n})\) 。 然后就可以愉快的暴力了。 考虑逆序时间,即每次删去一个数,并回答询问。 因为限制 \(LIS\) 的长度为 \(\sqrt{n}\) ,那么期望删除 \(\sqrt{n}\) 次才会修改 \(LIS\) 长度,这个时候暴力更新 \(LIS\) 即可。 复杂度 \(O(nlogn\sqrt{n})\) ,求 \(LIS\) 并且得到 \(LIS\) 序列可用树状数组来搞,复杂度是 \(O(nlogn)\) 的。 Code #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int T, n; int a[N], b[N], c[N], d[N], nxt[N], pre[N]; bool used[N]; int f[N], g[N]; int lowbit(int x) { return x & (-x); } int query(int x) { int ans = 0, p = 0; for(; x; x -= lowbit(x)) { if(c[x] > ans) { ans =

Press Enter to Continue

半世苍凉 提交于 2019-11-26 20:07:19
问题 This doesn't work: string temp; cout << "Press Enter to Continue"; cin >> temp; 回答1: cout << "Press Enter to Continue"; cin.ignore(); or, better: #include <limits> cout << "Press Enter to Continue"; cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); 回答2: Try: char temp; cin.get(temp); or, better yet: char temp = 'x'; while (temp != '\n') cin.get(temp); I think the string input will wait until you enter real characters, not just a newline. 回答3: Replace your cin >> temp with: temp = cin

C++ Issue with cin and CTRL + Z

我们两清 提交于 2019-11-26 18:39:02
问题 I'm reading c++ primer 5th and I have a little problem with an exercise: Read a sequence of words from cin and store the values a vector. After you’ve read all the words, process the vector and change each word to uppercase. Print the transformed elements, eight words to a line. My code is this: #include <iostream> #include <vector> #include <string> #include <cctype> using std::vector; using std::string; using std::cin; using std::cout; using std::endl; int main(){ vector<string> words;

What's the difference between getline and std::istream::operator>>()?

我与影子孤独终老i 提交于 2019-11-26 17:06:17
问题 #include <iostream> #include <string> using namespace std; int main() { string username; cout<< "username" ; cin >> username; } So I was curious on what's the difference between these two codes, I heard it's the same thing but if it is then why two ways of doing it then? #include <iostream> #include <string> using namespace std; int main() { string username; cout << "username" ; getline (cin,username) ; } 回答1: The difference is that std::getline — as the name suggests — reads a line from the

Problem of using cin twice

爷,独闯天下 提交于 2019-11-26 16:33:35
问题 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 pauses for user input, therefore the second output is always empty. After spending some time on it, I realized after the first call "cin>>str", it seems '\n' is still stored in cin (using cin.peek() to check), which ends getline immediately. The solution will be adding one more line between the first usage and the second one: cin.ignore

Read binary data from std::cin

北城余情 提交于 2019-11-26 15:31:13
What is the easiest way to read binary (non-formated) data from std::cin into either a string or a stringstream ? std::cin is not opened with ios_binary. If you must use cin, then you need to reopen it, which isn't part of the standard. Some ideas here: http://compgroups.net/comp.unix.programmer/How-can-I-reopen-std-cin-and-std-cout-in-binary-mode . Once it's binary, you can use cin.read() to read bytes. If you know that in your system, there is no difference between text and binary (and you don't need to be portable), then you can just use read without worrying. For windows, you can use the

std::getline on std::cin

不羁岁月 提交于 2019-11-26 14:49:40
问题 Is there any good reason why: std::string input; std::getline(std::cin, input); the getline call won't wait for user input? Is the state of cin messed up somehow? 回答1: Most likely you are trying to read a string after reading some other data, say an int . consider the input: 11 is a prime if you use the following code: std::cin>>number; std::getline(std::cin,input) the getline will only read the newline after 11 and hence you will get the impression that it's not waiting for user input. The

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

帅比萌擦擦* 提交于 2019-11-26 14:39:11
When should std::cin.getline() be used? What does it differ from std::cin ? In case with char*, std::cin.getline getting line, instead of std::cin getting first word. MSalters Let's take std::cin.getline() apart. First, there's std:: . This is the namespace in which the standard library lives. It has hundreds of types, functions and objects. std::cin is such an object. It's the standard character input object, defined in <iostream> . It has some methods of its own, but you can also use it with many free functions. Most of these methods and functions are ways to get one or more characters from

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

廉价感情. 提交于 2019-11-26 14:20:50
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 using cin as a variable. But, I thought it was a function. Why can we use cin in this way when it is x that has whatever value we input into our keyboard? cin is an object of class istream that represents the standard input stream. It corresponds to the cstdio stream stdin . The operator >> overload for streams return a reference to the same stream.