cout

Set Precision and Clip Trailing Zeros but Never Print Exponent

荒凉一梦 提交于 2019-11-28 12:41:38
I need to: Set precision so that floats are rounded to the hundredths place ( 0.111 prints as 0.11 ) Clip trailing zeros ( 1.0 prints as 1 ) Never print an exponent ( 1000.1 prints as 1000.1 ) printf( "%.2f\n", input ); // handles 1 and 3 but not 2 printf( "%.2g\n", input ); // handles 1 and 2 but not 3 cout << setprecision( 2 ) << input << endl; // handles 1 and 2 but not 3 Is there a printf or cout option that will let me handle all of these? The C11 standard says of %f and %F (7.21.6.1:8): A double argument representing a floating-point number is converted to decimal notation in the style [

cout slowest processor MPI

大憨熊 提交于 2019-11-28 11:45:14
I am writing a program using MPI. Each processor executes a for loop: int main(int argc, char** argv) { boost::mpi::environment env(argc, argv); for( int i=0; i<10; ++i ) { std::cout << "Index " << i << std::endl << std::flush; } } Is there a way to make the cout only happen on the last processor to hit index i? Or flag so a line is only executed on the last processor to get to it? It might look like trivial, but actually, what you ask here is extremely complex for distributed memory models such as MPI... In a shared memory environment, such as OpenMP for example, this would be trivially

C++ standard output format

十年热恋 提交于 2019-11-28 11:39:47
问题 I want to create a C++ console application that print some text to different parts of the console. For example in QBasic you can use: locate(8,5) print "hi" And hi would be printed in column 8 line 5. In C++ when I use cout it always prints on the next line, and begins printing in the first column. Is there any way I can do this? 回答1: C++ itself does not have this feature, it's I/O model is a fairly simple, sequential one. If you want to do fancy cursor positioning, you'll need to output (for

What is run first inside a cout statement? (C++17)

允我心安 提交于 2019-11-28 11:08:22
问题 Say for example I have a long statement like cout << findCurrent() << "," << findLowest() << "," << findHighest() << "," << findThird()<<"\n"; would findCurrent() be run before findLowest() like logic dictates? 回答1: Since C++17 the functions are guaranteed to be called left-to-right, i.e. findCurrent() is called first, then findLowest() and so on. C++17 Standard references: [expr.shift]/4 (referring to the expression E1 << E2 ): The expression E1 is sequenced before the expression E2 . [over

redirecting cout into file c++

别等时光非礼了梦想. 提交于 2019-11-28 10:17:16
问题 my problem is I have a couple of cout's in various files in the project. I would like all of them to be redirected and saved in .txt file, and what I achieved by now is that only one cout is saved in the file. I don't want to create separate .txt for each cout , for the sake of reading them at once. My code looks now like this: #include <fstream> #include <string> #include <iostream> int main() { std::ofstream out("out.txt"); std::cout.rdbuf(out.rdbuf()); std::cout << "get it3"; std::cout <<

Synchronizing STD cout output multi-thread

删除回忆录丶 提交于 2019-11-28 09:27:05
Latelly I've been working with multi-thread coding, after a while writing I realized that if I used std::cout in different boost::threads, the output would came without a logical order, the program that I'm testing is something like: #include <boost/thread/thread.hpp> #include <iostream> int my01( void ) { std::cout << "my01" << std::endl; return 0; } /* my02, my03 and my04 are the same with different outputs*/ [...] int main( void ) { boost::thread t1(&my01); boost::thread t2(&my02); boost::thread t3(&my03); boost::thread t4(&my04); while(!t1.joinable() || !t2.joinable() || !t3.joinable() ||

printf more than 5 times faster than std::cout?

ⅰ亾dé卋堺 提交于 2019-11-28 09:11:49
#include <iostream> #include <cstdlib> #include <cstdio> #include <ctime> int main(int argc, char* argv[]) { std::clock_t start; double duration; std::cout << "Starting std::cout test." << std::endl; start = std::clock(); for (int i = 0; i < 1000; i++) { std::cout << "Hello, World! (" << i << ")" << std::endl; } duration = (std::clock() - start) / (double) CLOCKS_PER_SEC; std::cout << "Ending std::cout test." << std::endl; std::cout << "Time taken: " << duration << std::endl; std::system("pause"); std::cout << "Starting std::printf test." << std::endl; start = std::clock(); for (int i = 0; i <

Linux下C++酒店管理系统

最后都变了- 提交于 2019-11-28 08:37:24
功能要求: ​ 相关源码:码云: 传送门 ,GitHub: 传送门 相关图片: 拆分版 make编译 ​ ./hotel运行 ​ 输入2,进入开房模块 ​ 相关源码: class.cpp 1 #include <fstream> 2 #include "tools.h" 3 #include "class.h" 4 5 using namespace std; 6 7 Customer* cust[30]; 8 Room* room[30]; 9 10 11 int live; // 被订房间数 12 13 // 获取room_num 14 short Room::get_room_num(void) 15 { 16 return room_num; 17 } 18 19 // 返回房间的下标 20 int Manage::room_index(short room_num) 21 { 22 short num = room_num; 23 for(int i=0; i<30; i++) 24 { 25 if(num == room[i]->get_room_num()) 26 { 27 return i; 28 } 29 } 30 return -1; 31 } 32 33 // 返回顾客的下标 34 int Manage::cust_index(short room_num)

C++: LPWSTR prints as an address in cout

青春壹個敷衍的年華 提交于 2019-11-28 08:02:52
问题 I have a variable of type LPTSTR , which I print to std::cout with << . In an ANSI system (don't know exactly where it is determined) it worked fine, it printed the string. Now in a Unicode system I get a hex address instead of the string. So, why does LPSTR (to which LPTSTR is resolved if UNICODE is not defined) act differently from LPWSTR (... if UNICODE is defined) and how do I print the string pointed by the latter one? 回答1: For Unicode strings you want wcout . You may be seeing hex

How to rollback lines from cout?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 07:02:44
I'm coding a task monitoring, which updates tasks' progress using cout. I'd like to display one task progress per line, therefore I have to rollback several lines of the console. I insist on "several" because \b does the job for one line, but does not erase \n between lines. I tried std::cout.seekp(std::cout.tellp() - str.length()); but tellp() returns -1 (failure). You can do cout << '\r'; to jump to the beginning of the current line, but moving upwards is system-specific. For Unix, see man termcap and man terminfo (and search for cursor_up ). On ANSI-compatible terminals (such as most modern