cin

char* and cin in C++

邮差的信 提交于 2019-12-19 04:15:10
问题 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" 回答1: 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* and cin in C++

我只是一个虾纸丫 提交于 2019-12-19 04:15:07
问题 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" 回答1: 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

Passing input as a function argument using cin

元气小坏坏 提交于 2019-12-19 03:11:09
问题 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. 回答1: Nope. Now, you could put this into a

istream and cin.get()

﹥>﹥吖頭↗ 提交于 2019-12-18 18:43:01
问题 I have a question about the difference between these two pieces of code: char buffer5[5]; cin.get(buffer5, 5); cout << buffer5; cin.get(buffer5, 5); cout << buffer5; and char buffer4; while (cin.get(buffer4)) { cout << buffer4; } In the first piece of code, the code gets 5 characters and puts it in buffer5. However, because you press enter, a newline character isn't put into the stream when calling get(), so the program will terminate and will not ask you for another round of 5 characters. In

istream and cin.get()

淺唱寂寞╮ 提交于 2019-12-18 18:41:15
问题 I have a question about the difference between these two pieces of code: char buffer5[5]; cin.get(buffer5, 5); cout << buffer5; cin.get(buffer5, 5); cout << buffer5; and char buffer4; while (cin.get(buffer4)) { cout << buffer4; } In the first piece of code, the code gets 5 characters and puts it in buffer5. However, because you press enter, a newline character isn't put into the stream when calling get(), so the program will terminate and will not ask you for another round of 5 characters. In

C++: how do I check if the cin buffer is empty?

孤者浪人 提交于 2019-12-18 10:42:13
问题 How do you check to see if the user didn't input anything at a cin command and simply pressed enter? 回答1: When reading from std::cin, it's preferable not to use the stream extraction operator >> as this can have all sorts of nasty side effects. For example, if you have this code: std::string name; std::cin >> name; And I enter John Doe , then the line to read from cin will just hold the value John , leaving Doe behind to be read by some future read operation. Similarly, if I were to write:

Wait until user presses enter in C++?

倾然丶 夕夏残阳落幕 提交于 2019-12-18 09:09:43
问题 waitForEnter() { char enter; do { cin.get(enter); } while ( enter != '\n' ); } It works, but not always. It doesn't work when an enter is pressed just before the function is called. 回答1: You can use getline to make the program wait for any newline-terminated input: #include <string> #include <iostream> #include <limits> void wait_once() { std::string s; std::getline(std::cin, s); } In general, you cannot simply "clear" the entire input buffer and ensure that this call will always block. If

Best way to read binary file c++ though input redirection

此生再无相见时 提交于 2019-12-18 07:00:19
问题 I am trying to read a large binary file thought input redirection ( stdin ) at runtime, and stdin is mandatory. ./a.out < input.bin So far I have used fgets. But fgets skips blanks and newline. I want to include both. My currentBuffersize could dynamically vary. FILE * inputFileStream = stdin; int currentPos = INIT_BUFFER_SIZE; int currentBufferSize = 24; // opt unsigned short int count = 0; // As Max number of packets 30,000/65,536 while (!feof(inputFileStream)) { char buf[INIT_BUFFER_SIZE];

C++ cin char read symbol-by-symbol

吃可爱长大的小学妹 提交于 2019-12-18 05:53:26
问题 I need to read symbol-by-symbol. But I don't know how to read until end of input. As exemple test system will cin>>somecharvariable m times. I have to read symbol-by-symbol all characters. Only m times. How I can do it? 回答1: If you want formatted input character-by-character, do this: char c; while (infile >> c) { // process character c } If you want to read raw bytes, do this: char b; while (infile.get(b)) // while(infile.read(&b, 1) // alternative, compare and profile { // process byte b }

Meaning of cin.fail() in C++?

戏子无情 提交于 2019-12-18 05:16:53
问题 while (!correct) { cout << "Please enter an angle value => "; cin >> value; //request user to input a value if(cin.fail()) // LINE 7 { cin.clear(); // LINE 9 while(cin.get() != '\n'); // LINE 10 textcolor(WHITE); cout << "Please enter a valid value. "<< endl; correct = false; } else { cin.ignore(); // LINE 18 correct =true; } } Hi, this is part of the code that I have written. The purpose of this code is to restrict users to input numbers like 10,10.00 etc, if they input values like (abc,!$@