+ 1

C++ iomanip count setw, extra newlines under Windows

I wrote some c++ code under Linux environment and the output to console via cout I formatted to look nice with iomanip using setw() and endl etc. I just compiled the same code under Windows and ran it and it's like there's an extra newline at every endl call. I think this is the \r\n vs \n newline situation. What are options for getting the same formatting for output across both platforms?

30th May 2024, 2:19 AM
Nathan Stanley
Nathan Stanley - avatar
4 Answers
+ 4
this is the only reason c++ has the std::endl. If you want your program set the correct new line for every OS you must use this
30th May 2024, 10:20 AM
john ds
john ds - avatar
+ 1
it doesn't seem to make a difference to the output if I change to using all endl for newlines. if (switchName == "-h" || switchName == "--help") { cout << left << endl << "Usage: " << argv[0] << " [-x] inputFilename.pdf" << endl << endl << "\tConverts PDF file data to different format based on configured template settings." << endl << "\tFor more information about configuring templates, see: https://github.com/Cabji/flip/README.md" << endl << endl << "\tSwitches:" << endl << endl << setw(25) << "\t -h --help" << setw(100) << "Displays help message" << endl << setw(25) << "\t -o outputFileName" << setw(100) << "Set output filename to use (./output.txt will be used if not provided)" << endl << setw(25) << "\t -t templateName" << setw(100) << "Set template to use" << endl << setw(25) << "\t -v --version" << setw(100) << "Display flip-cpp version" << endl << endl; return 0; } Outputs: Usage: main [-x] inputFilename.pdf Converts PDF file data to different format based on configured template settings. For more information about configuring templates, see: https://github.com/Cabji/flip/README.md Switches: -h --help Displays help message -o outputFileName Set output filename to use (./output.txt will be used if not provided) -t templateName Set template to use -v --version Display flip-cpp version Extra blank lines between each switch description line. Only shows like this under Windows, on Linux it doesn't have the extra blank lines.
1st Jun 2024, 10:58 AM
Nathan Stanley
Nathan Stanley - avatar
+ 1
if you still having issue, there are some things you can try to debug this issue. A good practice when you tried everything and still haven't identify the bug, is to keep a backup of source and then start removing rest code until you end up with the minimum code that still behaves weird. You will have to investigate a smaller source and at some point you will find it. In the past when i used options for printing like setw, my code behave weird sometimes. You can try print the new line in a different instruction like this: cout << setw(25) << "something"; cout << endl; Pretty sure it's due to setw(). Splitting your print to many parts, it may solve the problem
1st Jun 2024, 1:09 PM
john ds
john ds - avatar
0
@john ds ok thank you. i am using a combination of \n and endl in that portion of the code. so endl is the best way to get cross platform conformance!
1st Jun 2024, 10:52 AM
Nathan Stanley
Nathan Stanley - avatar