cin

How to stop, from another thread, std::cin from reading anymore input?

浪子不回头ぞ 提交于 2019-12-11 04:29:52
问题 I started two threads, thread t1 is waiting for input via cin . Can I put something like an EOF bit to cin from thread t2 to stop cin from reading? I tried '\n' and ios::eofbit . Both did not work. #include <thread> #include <iostream> #include <string> #include <condition_variable> std::condition_variable cv; void Foo() { std::string sFoo; std::cin >> sFoo; // Do stuff with sFoo } void Bar() { // Do stuff // If a condition is fullfilled I don't need the input in Foo anymore and so I want to

How to allow user to do input more than twice?

十年热恋 提交于 2019-12-11 04:13:39
问题 I am new to C++. I am trying to understand how to utilize the C++ common input (cin). I am trying to write a program that checks the amount of characters of the sentence and the amount of vowels in the input sentence. I have successfully done it, but a problem happens when I try to get the code to run through one more time. When it runs through one more time, it doesn't allow second input anymore. My simplified code is below. #include <iostream> #include <string> using namespace std; int main

How does one display * when typing a password in a C/C++ command-line tool? [duplicate]

寵の児 提交于 2019-12-11 03:27:56
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Read a password from std::cin Replace input with “ * ” C++ I'm looking for the C/C++ comparable way of doing <input type="password" /> for allowing the user to type in their password after a prompt in a command-line tool written in C. string username; string password; cout << "Username: "; getline(cin, username); cout << "Password: "; [<input type="password" />] getline(cin, password); 来源: https://stackoverflow

Mixing cin and getline under Linux and Windows

戏子无情 提交于 2019-12-11 02:41:35
问题 I am aware of the common problem with mixing cin and getline. I believe this is different. This is the program: #include <iostream> #include <cstdio> #include <string> using namespace std; int main() { int a; string line; cin >> a; printf("A is '%d'\n", a); getline(cin, line); printf("Line is '%s'\n", line.c_str()); cout << cin.fail() << cin.eof() << cin.bad() << endl; } I also have a version written using istream::getline. I believe the results are the same in all input cases given here. a

Does the failbit effect the call ignore on cin?

ぐ巨炮叔叔 提交于 2019-12-11 01:00:25
问题 After a failbit is set: When I first call cin.clear() and then cin.ignore(), the programe is right. And when I first call cin.ignore() and then cin.clear(), the ignore seems to not work, why? 回答1: cin.clear() clears the failbit, but cin.ignore() doesn't. it means that, if the stream is in an invalid state, calling clear() followed by ignore() will reset the state to good, then ignore the next character. On the other hand, calling ignore() followed by clear() means ignore() will fail, then

Read blank line C++

筅森魡賤 提交于 2019-12-10 21:16:22
问题 I am in a situation where i had a loop and everytime it reads a string but I dont know how to read blank input i.e if user enter nothing and hit enter, it remains there. I want to read that as string and move to next input below is the code int times = 4; while(times--) { string str; cin>>str; ---then some other code to play with the string--- } 回答1: You would need to read the entire line using getline(). Then you would need to tokenize the strings read. Here is a reference on using getline

Spaces cant be used in string? c++

半世苍凉 提交于 2019-12-10 20:46:08
问题 Basically I'm experimenting with polymorphism. I have 2 objects, a customer and an employee. a customer has a name and a complaint. An employee has a name and a salary. In a loop, I take in these parameters and create a new Person to add to an array. But here's my issue: If I put any spaces in the string, the loop races to the end. Person *persons[10]; for (int i = 0; i < sizeof persons;i++) { cout<<"Please type 1 for customer or 2 for Employee"<<endl; int q; cin>>q; string name; int salary;

C++ - Quitting a program

[亡魂溺海] 提交于 2019-12-10 19:58:50
问题 In the C++ Without Fear: A Beginner's Guide That Makes You Feel Smart book in chapter(8), part of a code trying to display a text file is the following: while(1) { for(int i=1; i <= 24 && !file_in.eof(); i++) { file_in.getline(input_line,80); std::cout<<input_line<<std::endl; } if(file_in.eof()) { break; } std::cout<<"More? (Press 'Q' and ENTER to quit.)"; std::cin.getline(input_line,80); c=input_line[0]; // <<<<<< if(c=='Q'||c=='q') { break; } } The part I'm not getting here is: c=input_line

How does cin.clear() clear the input buffer?

血红的双手。 提交于 2019-12-10 17:07:12
问题 From what I have read, cin.clear() resets the flags, but how does this clear the input buffer? 回答1: cin.clear() has no effect on the input buffer. As you correctly read, it resets the iostate flags (technically, replaces their current value with std::ios_base::goodbit ) 回答2: std::ios::clear() only resets the error flags, if possible. If there is, e.g., no stream buffer (i.e., stream.rdbuf() yield nullptr ) the std::ios_base::badbit still stays set. That is the only affect. In particular, std:

Read a very long console input in C++

☆樱花仙子☆ 提交于 2019-12-10 16:48:47
问题 I'm trying to read a list of numbers (space delimited) from the console using std::cin. When the input line is longer than 1023 characters, the first "cin >> list[i]" in the following small working example never returns: using namespace std; int main() { vector<int> list(200,0); for(int i=0;i<200;i++){ cin >> list[i]; cout << '-'; cin.clear(); if(list[i]==0) break; } return 0; } This code fails for the following input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28