cin

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

末鹿安然 提交于 2019-11-27 08:09:31
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 <<" "<< (*ci).second.size() <<endl; } } To get rid of it, I put cin.ignore() after getline. Now its taking all

C++ cin.fail() question

爱⌒轻易说出口 提交于 2019-11-27 07:24:01
问题 When running the following code and enter a number, it works fine. But when entering a letter, the program enters an infinite loop, displaying "Enter a number (0 to exit): cin failed." My intent was to handle the cin fail case and prompt the user again. int number; do{ cout << "Enter a number (0 to exit): "; cin >> number; if(cin.fail()){ cout << "cin failed." << endl; cin.clear(); }else{ cout << "cin succeeded, " << number << " entered." << endl; } }while(number != 0); 回答1: You need to clear

How do I prevent a runaway input loop when I request a number but the user enters a non-number?

主宰稳场 提交于 2019-11-27 07:11:06
问题 I need to know how to make my cin statement not appear to 'remove' itself if you input the wrong type. The code is here: int mathOperator() { using namespace std; int Input; do { cout << "Choose: "; el(); cout << "1) Addition"; el(); cout << "2) Subtraction"; el(); cout << "3) Multiplication"; el(); cout << "4) Division"; el(); el(); cin >> Input; } while (Input != 1 && Input != 2 && Input!=3 && Input!=4); return Input; } Execute, enter, for example, a character, and it loops nonstop acting

How is “std::cin>>value” evaluated in a while loop?

霸气de小男生 提交于 2019-11-27 07:01:19
问题 Currently I'm self-learning C++ Primer 5th. Here comes something I'm not sure. (I couldn't find the exact relevant question on F.A.Q). Consider this while loop: while(std::cin>>value){...} \\value here was defined as int. The text book says: That expression reads the next number from the standard input and stores that number in value. The input operator (§ 1.2, p. 8) returns its left operand, which in this case is std::cin. This condition, therefore, tests std::cin.When we use an istream as a

Skipping expected characters like scanf() with cin

坚强是说给别人听的谎言 提交于 2019-11-27 06:16:07
问题 How to achieve scanf("%d # %d",&a,&b); sort of effect with cin in C++ ? 回答1: You could create your own stream manipulator. It is fairly easy. #include <ios> #include <iostream> using namespace std; // skips the number of characters equal to the length of given text // does not check whether the skipped characters are the same as it struct skip { const char * text; skip(const char * text) : text(text) {} }; std::istream & operator >> (std::istream & stream, const skip & x) { ios_base::fmtflags

Multiple inputs on one line

落爺英雄遲暮 提交于 2019-11-27 05:39:34
问题 I have looked to no avail, and I'm afraid that it might be such a simple question that nobody dares ask it. Can one input multiple things from standard input in one line? I mean this: float a, b; char c; // It is safe to assume a, b, c will be in float, float, char form? cin >> a >> b >> c; 回答1: Yes, you can input multiple items from cin , using exactly the syntax you describe. The result is essentially identical to: cin >> a; cin >> b; cin >> c; This is due to a technique called "operator

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

风流意气都作罢 提交于 2019-11-27 05:24:32
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 sounds like that is for leading whitespace to be skipped. Perhaps I need to use streambuf something like

Hide user input on password prompt [duplicate]

↘锁芯ラ 提交于 2019-11-27 04:25:19
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 std::cin.tie on a stringstream , but everything I type is still mirrored in the console. From How to Hide Text : Windows #include <iostream> #include <string> #include

cin input (input is an int) when I input a letter, instead of printing back incorrect once, it prints correct once then inc for the rest of the loop

萝らか妹 提交于 2019-11-26 23:45:57
问题 I'm making a multiplication practice program. As my title says, if I enter a letter into the console instead of a number, it will run off saying correct on the first one, but incorrect on the rest. Even if you're not touching the keyboard, it'll still spit out incorrect. ans = table * i; std::cout << table << " * " << i << " =" << std::endl; std::cin >> input; if(input == ans) { std::cout << "Correct! " << ans << std::endl; } else { std::cout << "Incorrect, the answer was " << ans << std:

How do I deal with the max macro in windows.h colliding with max in std?

烂漫一生 提交于 2019-11-26 20:59:42
So I was trying to get valid integer input from cin, and used an answer to this question . It recommended: #include <Windows.h> // includes WinDef.h which defines min() max() #include <iostream> using std::cin; using std::cout; void Foo() { int delay = 0; do { if(cin.fail()) { cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } cout << "Enter number of seconds between submissions: "; } while(!(cin >> delay) || delay == 0); } Which gives me an error on Windows, saying that the max macro doesn't take that many arguments. Which means I have to do this do { if(cin.fail())