cin

C++ Checking for an integer.

淺唱寂寞╮ 提交于 2019-12-18 05:02:24
问题 New to C++. Having issues correctly looping while handling errors. I am trying to check if user input is an integer, and is positive. do{ cout << "Please enter an integer."; cin >> n; if (cin.good()) { if (n < 0) {cout << "Negative.";} else {cout << "Positive.";} } else { cout << "Not an integer."; cin.clear(); cin.ignore(); } }while (!cin.good() || n < 0); cout << "\ndone."; When a non-integer is entered, the loop breaks. I feel like I am misunderstanding the inherent usage of cin.clear()

Using the console in a GUI app in windows, only if its run from a console

白昼怎懂夜的黑 提交于 2019-12-17 19:31:01
问题 My application is a GUI app that has helpful (though optional) information through the terminal (via cout). In Windows I either have a console appear (by compiling as a console app, or allocating it dynamically) or I don't. My intention is to make use of the console IF it is being run from the console, but ignore the console completely if it was not. (Essentially what happens in Linux and OS X). I do not wish to redirect to a file (and in the case of using cin, this is not a viable solution

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

亡梦爱人 提交于 2019-12-17 18:55:18
问题 This question already has answers here : Closed 7 years ago . 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? Also, how would an error be handled? At the moment this is all I have: if(cin >> radius){}else{} I using `try{}catch(){}, but I don't think that would the right solution for this issue. Any

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

做~自己de王妃 提交于 2019-12-17 18:33:25
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . #include <iostream> using namespace std; int main() { string s; cin >> s; cout << "Hello World!"; } This isn't working. Why? 回答1:

Is it possible to use cin with Qt?

我的梦境 提交于 2019-12-17 16:46:08
问题 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. 回答1: I tested out Kaleb Pederson's answer, and found a more consise way than the solution he presented (though I have to thank him for pointing me to the right direction): QTextStream qtin(stdin); QString line = qtin.readLine(); // This is how you read the entire line QString word; qtin >> word; // This is how you read a word (separated by space) at a time. In other

Reading piped input with C++

耗尽温柔 提交于 2019-12-17 16:29:03
问题 I am using the following code: #include <iostream> using namespace std; int main(int argc, char **argv) { string lineInput = " "; while(lineInput.length()>0) { cin >> lineInput; cout << lineInput; } return 0; } With the following command: echo "Hello" | test.exe Thes result is an infinate loop printing "Hello". How can I make it read and print a single "Hello"? 回答1: string lineInput; while (cin >> lineInput) { cout << lineInput; } If you really want full lines, use: string lineInput; while

cin.getline() is skipping an input in C++

本小妞迷上赌 提交于 2019-12-17 09:36:25
问题 If I use the following code, getline doesn't take the last input(for last iteration of "for" loop, it simply skips it) - int main() { int n; map<string, set<string> > lst; string c,s,c2; cin>>n; for(int i=0;i<n;i++) { getline(cin,c); // here it skips the input for last iteration stringstream ss; ss<<c; bool f=1; while(ss>>s) { if(f) { c2=s; f=0; } else lst[c2].insert(s); } } for (map<string, set<string> >::const_iterator ci = lst.begin(); ci != lst.end(); ++ci) { cout<< (*ci).first <<" "<< (

cin for an int inputing a char causes Loop that is supposed to check input to go wild

ⅰ亾dé卋堺 提交于 2019-12-17 06:49:06
问题 This is a function of my game it will ask for input and cin into "iAuswahl"! Then the while loop is checks if it is one of the Values i want 1-9 if not it activates and is supposed to ask for new input. Witch it does for int. But if i input a char like r it will go crazy and just keep giving me back my cout and skip the cin! My questions are why does it do it and how do i stop it? void zug(string sSpieler, int iDran){ int iAuswahl; char cXO = 'O'; if (iDran == 1) { cXO = 'X'; } cout <<

Hide user input on password prompt [duplicate]

老子叫甜甜 提交于 2019-12-17 06:41:28
问题 This question already has answers here : Closed 8 years ago . 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 appear directly in it again? I need this for letting the user typing a password and neither me nor the user normally wants his password appearing in plaintext on the screen. I tried using

How to read cin with whitespace up until a newline character?

会有一股神秘感。 提交于 2019-12-17 06:17:06
问题 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 given: (cd /my/dir; doSometing)\n The variable result only gets: (cd I would think I should be able to use stream manipulators to accomplish this, but the skipws was not quite right in that it throws carriage returns in with spaces and tabs, plus it