cout

Why does cout.setf(ios::fixed) change my floats to hexadecimal?

梦想的初衷 提交于 2019-12-31 01:11:10
问题 I experienced this weird issue recently having to do with cout.setf(ios::fixed). Took me quite a while to track down the cause and thought I'd ask here to learn more. The issue is this - all floating point numbers were printed as hexadecimal numbers when using cout.setf(ios::fixed). Why does this happen? The documentation of ios::base doesn't seem to imply that this will happen (at least to me). I am using g++ 5.3.0 and pasted below is a minimal example and the output. #include <iostream>

C++: 'cout << pointer << ++pointer' generates a compiler warning

萝らか妹 提交于 2019-12-30 18:27:13
问题 I have a C++ learning demo here: char c = 'M'; short s = 10; long l = 1002; char * cptr = &c; short * sptr = &s; long * lptr = &l; cout << "cptr:\t" << static_cast<void*>(cptr) << '\n'; cout << "cptr++:\t" << static_cast<void*>(++cptr) << '\n'; cout << "sptr:\t" << sptr << '\n'; cout << "sptr++:\t" << ++sptr << '\n'; cout << "lptr:\t" << lptr << '\n'; cout << "lptr++:\t" << ++lptr << '\n'; cout << c << '\t' << static_cast<void*>(cptr) << '\t' << static_cast<void*>(++cptr) << '\n'; cout << s <

C++: 'cout << pointer << ++pointer' generates a compiler warning

独自空忆成欢 提交于 2019-12-30 18:27:04
问题 I have a C++ learning demo here: char c = 'M'; short s = 10; long l = 1002; char * cptr = &c; short * sptr = &s; long * lptr = &l; cout << "cptr:\t" << static_cast<void*>(cptr) << '\n'; cout << "cptr++:\t" << static_cast<void*>(++cptr) << '\n'; cout << "sptr:\t" << sptr << '\n'; cout << "sptr++:\t" << ++sptr << '\n'; cout << "lptr:\t" << lptr << '\n'; cout << "lptr++:\t" << ++lptr << '\n'; cout << c << '\t' << static_cast<void*>(cptr) << '\t' << static_cast<void*>(++cptr) << '\n'; cout << s <

Cout long double issue

旧街凉风 提交于 2019-12-30 10:38:50
问题 So, I'm working on a C++ project. I have a var of long double type and assigned it a value like "1.02" Then, I try to use cout to print it and the result is: -0 I already tried to use setprecision and all I found googling the problem. What is the solution for this? Example code: #include <cstdlib> #include <iomanip> using namespace std; int main(int argc, char** argv) { cout.precision(15); long double var = 1.2; cout << var << endl; return 0; } OS: Windows 8.1 64 bits Compiler: minGW IDE:

Indenting Paragraph With cout

最后都变了- 提交于 2019-12-30 08:34:08
问题 Given a string of unknown length, how can you output it using cout so that the entire string displays as an indented block of text on the console? (so that even if the string wraps to a new line, the second line would have the same level of indentation) Example: cout << "This is a short string that isn't indented." << endl; cout << /* Indenting Magic */ << "This is a very long string that will wrap to the next line because it is a very long string that will wrap to the next line..." << endl;

Indenting Paragraph With cout

十年热恋 提交于 2019-12-30 08:34:06
问题 Given a string of unknown length, how can you output it using cout so that the entire string displays as an indented block of text on the console? (so that even if the string wraps to a new line, the second line would have the same level of indentation) Example: cout << "This is a short string that isn't indented." << endl; cout << /* Indenting Magic */ << "This is a very long string that will wrap to the next line because it is a very long string that will wrap to the next line..." << endl;

Custom C++ cout class - output to both console and log file

六月ゝ 毕业季﹏ 提交于 2019-12-30 04:37:07
问题 I'm working on a program that makes heavy use of "cout << strSomething;" to log information to the console. I need to modify the program so that all console output goes to both the console AND a file. Although I can modify the "cout <<" in our code, there are several large third party libraries that also use "cout <<"; those libraries cannot be modified due to their licenses - so modifying all references to "cout <<" is not a solution. Also, the use of "wtee.exe" isn't possible due to the

std::cout to print character N times

对着背影说爱祢 提交于 2019-12-30 00:52:06
问题 How can I print a character N number of times using std::cout without looping? Is there a way to move the text cursor back to nullify the effect of std::cout << std::endl; ? i.e. to move up a line (say we never printed anything after doing the std::cout << std::endl; operation). 回答1: std::cout << std::string(100, '*') << std::endl; To move a line up, you have to resort to terminal escapes (assuming that isatty() indicates that you are running on one). 回答2: std::cout << std::setfill(the_char)

C++异常注意事项

梦想的初衷 提交于 2019-12-29 09:10:29
C++里面catch对于类型转换,限制比参数传递时候要多: 不可以进行 标准算术转换 和 类的自定义转换 :在函数参数匹配的过程中,可以进行很多的类型转换。但是在异常匹配的过程中,转换的规则要严厉。 标准算术转换,指的是 short转成int 等等。异常catch的时候,不允许转换,指的是匹配的时候,就不会匹配上。比如下面: #include <iostream> #include <exception> #include <stack> using namespace std; int main() { std::cout << "Hello, World!" << std::endl; stack<int> stk; //stk.push(5); try { //stk.pop(); short s = 5; throw s; } /*catch(runtime_error exception1) { std::cout << exception1.what() << endl; }*/ catch(int &x) { std::cout << x << endl; } catch (...) { cout << "here catch" << endl; } return 0; } View Code 输出: here catch View Code 意味着

How to print '\n' instead of a newline?

浪尽此生 提交于 2019-12-29 07:42:08
问题 I am writing a program that uses prints a hex dump of its input. However, I'm running into problems when newlines, tabs, etc are passed in and destroying my output formatting. How can I use printf (or cout I guess) to print '\n' instead of printing an actual newline? Do I just need to do some manual parsing for this? EDIT: I'm receiving my data dynamically, it's not just the \n that I'm corned about, but rather all symbols. For example, this is my printf statement: printf("%c", theChar); How