cin

C++ iostream: Using cin >> var and getline(cin, var) input errors

旧街凉风 提交于 2019-12-01 11:00:35
I'm creating a simple console application in C++ that gets string and char inputs from the user. To make things simple, I would like to use the string and char data types to pass input from cin to. To get string inputs, I'm using the getline method: string var; cin.ignore(); //I used ignore() because it prevents skipping a line after using cin >> var getline(cin, var); To get char inputs, I'm using the cin >> var method: char var; cin >> var; This works fine for the most part. However, when I enter a string using getline , it ignores the first character of my string. Is it possible to use

Read an array from standard input, ignoring brackets and commas

↘锁芯ラ 提交于 2019-12-01 10:23:42
问题 The sample input to my code is: { 1, 2, 3, 4 } I wish to ignore the curly brackets and commas, and read the numbers into an array. How can I do that? 回答1: Hmmm, this might work: // Ignore all characters up to and including the open curly bracket cin.ignore(100000, '{'); // Read the numbers into an array int my_array[4]; unsigned int array_index = 0; cin >> my_array[array_index]; array_index++; cin >> my_array[array_index]; array_index++; cin >> my_array[array_index]; array_index++; cin >> my

C++ iostream: Using cin >> var and getline(cin, var) input errors

邮差的信 提交于 2019-12-01 09:33:56
问题 I'm creating a simple console application in C++ that gets string and char inputs from the user. To make things simple, I would like to use the string and char data types to pass input from cin to. To get string inputs, I'm using the getline method: string var; cin.ignore(); //I used ignore() because it prevents skipping a line after using cin >> var getline(cin, var); To get char inputs, I'm using the cin >> var method: char var; cin >> var; This works fine for the most part. However, when I

Reuse std::cin after eating an EOF

痞子三分冷 提交于 2019-12-01 07:39:44
问题 The unix command wc has this functionality: $ wc - - - aaa bbb ccc<EOF> 0 3 11 - aaa bbb ccc<EOF> 0 3 11 - aaa bbb ccc<EOF> 0 3 11 - 0 9 33 total Each <EOF> indicates a <C-d> key sequence that enters an EOF into stdin. wc is then able to pick up this EOF . I'm trying to implement this in C++. A common suggestion is combination of clear() and ignore() . char c; while (std::cin >> c) { ... } std::cin.clear(); std::cin.ignore(); while (std::cin >> c) { /* never executed */ } I've also tried std:

How Can I avoid char input for an int variable?

回眸只為那壹抹淺笑 提交于 2019-12-01 04:43:01
The program below shows a 'int' value being entered and being output at the same time. However, when I entered a character, it goes into an infinite loop displaying the previous 'int' value entered. How can I avoid a character being entered? #include<iostream> using namespace std; int main(){ int n; while(n!=0){ cin>>n; cout<<n<<endl; } return 0; } Reason for Infinite loop: cin goes into a failed state and that makes it ignore further calls to it, till the error flag and buffer are reset. cin.clear(); cin.ignore(100, '\n'); //100 --> asks cin to discard 100 characters from the input stream.

Is it possible to “prepare” input from cin?

老子叫甜甜 提交于 2019-12-01 04:14:31
问题 In his answer, specifically in the linked Ideone example, @Nawaz shows how you can change the buffer object of cout to write to something else. This made me think of utilizing that to prepare input from cin , by filling its streambuf : #include <iostream> #include <sstream> using namespace std; int main(){ streambuf *coutbuf = cout.rdbuf(cin.rdbuf()); cout << "this goes to the input stream" << endl; string s; cin >> s; cout.rdbuf(coutbuf); cout << "after cour.rdbuf : " << s; return 0; } But

How Can I avoid char input for an int variable?

只谈情不闲聊 提交于 2019-12-01 02:14:00
问题 The program below shows a 'int' value being entered and being output at the same time. However, when I entered a character, it goes into an infinite loop displaying the previous 'int' value entered. How can I avoid a character being entered? #include<iostream> using namespace std; int main(){ int n; while(n!=0){ cin>>n; cout<<n<<endl; } return 0; } 回答1: Reason for Infinite loop: cin goes into a failed state and that makes it ignore further calls to it, till the error flag and buffer are reset

char* and cin in C++

那年仲夏 提交于 2019-12-01 01:23:12
I would like to input a string of indefinite length to a char * variable using cin; I can do this: char * tmp = "My string"; cout << tmp << endl; system("pause"); It works perfectly. But I fail to do this: char * tmp cin >> tmp; Could you give me a hing what's wrong" Well, you havn't created an object for the char* to point to. char* tmp = new char[MAX_LENGTH]; should make it work better (you have to define MAX_LENGTH). Another way to do this is: std::string strtmp; cin >> strtmp; const char* tmp = strtmp.c_str(); This method would mean that you need not use new . Couple of issues: char * tmp

Passing input as a function argument using cin

帅比萌擦擦* 提交于 2019-11-30 21:42:17
My program: class test { int k; public: void changeval(int i){k=i;} }; int main() { test obj; int i; cin>>i; obj.changeval(i); return 0; } Is there any way, by which i can directly pass input from the user as an argument to the function changeval(int), without even initializing value to i?? I mean, i don't want to declare a variable just to pass value to a function. Is there any way i can avoid it? If yes, can I use it for constructors also? Thanks. Nope. Now, you could put this into a function: int readInt(std::istream& stream) { int i; stream >> i; // Cross your fingers this doesn't fail