iostream

When using cout and cin, what are the “<<” and “>>” operators doing and why do we use them?

一笑奈何 提交于 2021-02-05 07:35:21
问题 For example: int age; cin >> age; cout << "You are " << age << " years old!" << endl; Why do we use the "<<" and ">>" operators here? What are they doing? I somewhat understand bit-shifting, but I don't get how that works here. 回答1: They are called the stream insertion operator ( << ) and the stream extraction operator ( >> ). These are the same operators as the left and right bit shift operators (even though they have different names). The bit shift operators are overloaded, so that when the

How do I use an uint8_t with I/O streams while avoiding the char behavior?

喜你入骨 提交于 2021-02-05 05:59:26
问题 Consider this simple C++ program: #include <cstdint> #include <iostream> int main() { uint8_t var; std::cin >> var; std::cout << "var = " << var << '\n'; if (var == 1) { std::cout << "var is 1\n"; } else { std::cout << "var is not 1\n"; } } Running it shows some surprising behavior. When the input is 1 , the output is var = 1 var is not 1 which is clearly absurd! After quite a few varied tests, I realized what is happening—it's reading and writing a char! Still, it's not the behavior I want—I

cin infinite loop when reading in a non-numeric value

烂漫一生 提交于 2021-02-02 08:39:22
问题 I had a strange behavior in a program and I spent long time trying to deduce why. it was an infinite loop with no sense. Testing these lines of code(under suspicion) i got the same result. Every time I type in a non-numeric value such a symbol, the program runs through an infinite loop printing zeros, which i guess is how cout represents the wrong value entered. I'd like to know why is that weird behavior from cin, printing all those zeros instead of stopping when it finds a wrong reading.

cin infinite loop when reading in a non-numeric value

那年仲夏 提交于 2021-02-02 08:38:15
问题 I had a strange behavior in a program and I spent long time trying to deduce why. it was an infinite loop with no sense. Testing these lines of code(under suspicion) i got the same result. Every time I type in a non-numeric value such a symbol, the program runs through an infinite loop printing zeros, which i guess is how cout represents the wrong value entered. I'd like to know why is that weird behavior from cin, printing all those zeros instead of stopping when it finds a wrong reading.

cin assigns beginning of string to int, only fails in second call

北城余情 提交于 2021-01-27 12:01:52
问题 In the snippet for(;;) { std::cout << "Please enter an integer: "; if(std::cin >> nInput) { return nInput; } else { std::cout << "Invalid input! Please enter an integer!\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } } cin will assign the beginning of a string to nInput , provided at least the first character is numeric. For instance, here's some of my console output: Please enter an integer: 10 Please enter an integer: foo Invalid input! Please

Reading a specific number of characters from C++ stream into std::string

痴心易碎 提交于 2021-01-27 04:56:17
问题 I'm pretty familiar with most of C++ but one area I've avoided has been IO streams, mainly because I've been using it on embedded systems where they're not appropriate. Recently I've had to become familiar with them, however, and I'm struggling to figure out something that I feel should be simple. What I'm looking for a relatively efficient way to read a fixed number of characters from a C++ stream into a std::string . I could easily read into a temporary char array with the read() method and

Full precision display of floating point numbers in C++?

萝らか妹 提交于 2021-01-20 04:43:12
问题 I have read several topics about the display of floating point numbers display in C++ and I couldn't find a satisfying answer. My question is: how to display all the significant digits of a floating point numbers in C++ in a scientific format (mantissa/exponent) ? The problem is that all numbers do not have the same number of significant digits in base 10. For example a double has 15 to 17 significant decimal digits precision, but std::numeric_limits<double>::digits10 returns 15 and

C++ - Send data in one ostream to another ostream

荒凉一梦 提交于 2020-12-12 03:39:49
问题 I don't understand what this ostream function declaration means: ostream& operator<< (ostream& (*pf)(ostream&)); (specifically, the (*pf)(ostream&) part). I want to do something like: void print(ostream& os){ cout << os; } but I get the error: Invalid operands to binary expression ('ostream' . . . and 'ostream') 回答1: I don't understand what this ostream function declaration means: ostream& operator<< (ostream& (*pf)(ostream&)); Have you seen functions like std::endl , std::flush , std::hex ,

C++ - Send data in one ostream to another ostream

我只是一个虾纸丫 提交于 2020-12-12 03:38:04
问题 I don't understand what this ostream function declaration means: ostream& operator<< (ostream& (*pf)(ostream&)); (specifically, the (*pf)(ostream&) part). I want to do something like: void print(ostream& os){ cout << os; } but I get the error: Invalid operands to binary expression ('ostream' . . . and 'ostream') 回答1: I don't understand what this ostream function declaration means: ostream& operator<< (ostream& (*pf)(ostream&)); Have you seen functions like std::endl , std::flush , std::hex ,

C++ - Send data in one ostream to another ostream

ぐ巨炮叔叔 提交于 2020-12-12 03:37:51
问题 I don't understand what this ostream function declaration means: ostream& operator<< (ostream& (*pf)(ostream&)); (specifically, the (*pf)(ostream&) part). I want to do something like: void print(ostream& os){ cout << os; } but I get the error: Invalid operands to binary expression ('ostream' . . . and 'ostream') 回答1: I don't understand what this ostream function declaration means: ostream& operator<< (ostream& (*pf)(ostream&)); Have you seen functions like std::endl , std::flush , std::hex ,