cin

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

不想你离开。 提交于 2019-11-26 03:44:32
问题 When should std::cin.getline() be used? What does it differ from std::cin ? 回答1: In case with char*, std::cin.getline getting line, instead of std::cin getting first word. 回答2: 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

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

我的未来我决定 提交于 2019-11-26 03:37:49
问题 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? 回答1: cin is an object of class istream that represents the standard input stream. It

How to cin Space in c++?

强颜欢笑 提交于 2019-11-26 02:21:48
问题 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!!!\"<<endl; } return 0; } How to cin a Space symbol from standard input? If you write space, program ignores! :( Is there any combination of symbols (e.g. \'\\s\' or something like this) that means \"Space\" that I can use from standard input for my code? 回答1: It skips all whitespace (spaces, tabs, new lines, etc.) by default. You can either change its behavior, or use

how do I validate user input as a double in C++?

穿精又带淫゛_ 提交于 2019-11-26 02:12:00
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 value." << endl; } } //do other stuff... The above code infinitely outputs the Invalid Input! statement, so its not prompting for another input. I want to prompt for the input, check if it is legitimate... if its a double, go on... if it is NOT a double, prompt again. Any ideas? Try this: while (1) { if (cin >> x) { // valid number break; } else { // not a valid number cout << "Invalid Input!

Why does stringstream >> change value of target on failure?

萝らか妹 提交于 2019-11-26 02:10:31
问题 From Stroustrup\'s TC++PL, 3rd Edition, Section 21.3.3: If we try to read into a variable v and the operation fails, the value of v should be unchanged (it is unchanged if v is one of the types handled by istream or ostream member functions). The following example appears to contradict the above quote. Based on the above quote, I was expecting the value of v to remain unchanged -- but it gets zeroed. What\'s the explanation for this apparent contradictory behaviour? #include <iostream>

getline not asking for input? [duplicate]

笑着哭i 提交于 2019-11-26 02:01:49
问题 This question already has answers here : Need help with getline() [duplicate] (7 answers) Closed 4 years ago . This is probably a very simple problem but forgive me as I am new. Here is my code: #include <iostream> #include <string> #include <sstream> using namespace std; int main () { string name; int i; string mystr; float price = 0; cout << \"Hello World!\" << endl; cout << \"What is your name? \"; cin >> name; cout << \"Hello \" << name << endl; cout << \"How old are you? \"; cin >> i;

changing the delimiter for cin (c++)

◇◆丶佛笑我妖孽 提交于 2019-11-26 01:15:54
问题 I\'ve redirected \"cin\" to read from a file stream cin.rdbug(inF.rdbug()) When I use the extraction operator it reads until it reaches a white space character. Is it possible to use another delimiter? I went through the api in cplusplus.com, but didn\'t find anything. 回答1: It is possible to change the inter-word delimiter for cin or any other std::istream, using std::ios_base::imbue to add a custom ctype facet. If you are reading a file in the style of /etc/passwd, the following program will

When and why do I need to use cin.ignore() in C++?

孤者浪人 提交于 2019-11-26 01:13:08
问题 I wrote a very basic program in C++ which asked the user to input a number and then a string. To my surprise, when running the program it never stopped to ask for the string. It just skipped over it. After doing some reading on StackOverflow, I found out that I needed to add a line that said: cin.ignore(256, \'\\n\'); before the line that gets the string input. Adding that fixed the problem and made the program work. My question is why does C++ need this cin.ignore() line and how can I

how do I validate user input as a double in C++?

北城余情 提交于 2019-11-26 01:09:09
问题 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 value.\" << endl; } } //do other stuff... The above code infinitely outputs the Invalid Input! statement, so its not prompting for another input. I want to prompt for the input, check if it is legitimate... if its a double, go on... if it is NOT a double, prompt again. Any ideas? 回答1: Try

How do I flush the cin buffer?

眉间皱痕 提交于 2019-11-25 22:57:53
问题 How do I clear the cin buffer in C++? 回答1: Possibly: std::cin.ignore(INT_MAX); This would read in and ignore everything until EOF . (you can also supply a second argument which is the character to read until (ex: '\n' to ignore a single line). Also: You probably want to do a: std::cin.clear(); before this too to reset the stream state. 回答2: I would prefer the C++ size constraints over the C versions: // Ignore to the end of file cin.ignore(std::numeric_limits<std::streamsize>::max()) //