iomanip

How do you limit the maximum amount of characters in user input in C++?

不羁的心 提交于 2019-12-24 02:18:57
问题 I want it so that when the user inputs more than 5 characters, something will happen, instead of just skipping the rest. In this code, if you type in more than 5 characters, it will only show the first 5 characters. I want to put an "if" statement here that if the user inputs more than 5 characters, it will show an error or something, and not just show the first 5 characters. #include <iostream> #include <iomanip> int main() { using namespace std; string nationname; int maxchar = 5; cout <<

Does put_money hold its argument by value or reference?

不想你离开。 提交于 2019-12-22 05:07:54
问题 Does the following invoke undefined behavior? #include <iostream> #include <iomanip> #include <algorithm> #include <experimental/iterator> int main() { long double values[] = {1, 2, 3}; std::transform( std::begin(values), std::end(values), std::experimental::make_ostream_joiner(std::cout, ", "), [](long double v) { return std::put_money(v + 1); } ); return 0; } My worry is that return std::put_money(v + 1) returns a reference to the temporary v + 1 . 回答1: The standard ([ext.manip]/6) only

What's the opposite of `fixed` in cout?

北慕城南 提交于 2019-12-18 20:13:25
问题 When using cout , what is the default formatter defined in the <iomanip> header? In other words, once I've set my formatter to fixed using cout << fixed << setPrecision(2) , how do I change it back? Or, what am I changing it back to ? 回答1: The opposite of std::fixed is std::scientific . (You find a nice list of manipulators in this great answer.) 回答2: The answer is std::defaultfloat in C++11. To achieve this in C++03 you can do cout.unsetf(std::ios_base::floatfield); See Really, what's the

What's the opposite of `fixed` in cout?

折月煮酒 提交于 2019-12-18 20:13:09
问题 When using cout , what is the default formatter defined in the <iomanip> header? In other words, once I've set my formatter to fixed using cout << fixed << setPrecision(2) , how do I change it back? Or, what am I changing it back to ? 回答1: The opposite of std::fixed is std::scientific . (You find a nice list of manipulators in this great answer.) 回答2: The answer is std::defaultfloat in C++11. To achieve this in C++03 you can do cout.unsetf(std::ios_base::floatfield); See Really, what's the

How does “std::cout << std::endl;” compile?

旧巷老猫 提交于 2019-12-17 20:17:40
问题 Most IO stream manipulators are regular functions with the following signature: std::ios_base& func( std::ios_base& str ); However some manipulators (including the most frequently used ones - std::endl and std::flush) are templates of the following form: template< class CharT, class Traits > std::basic_ostream<CharT, Traits>& func(std::basic_ostream<CharT, Traits>& os); Then, how does the compilation of std::cout << std::endl; succeed given that the following example fails: $ cat main.cpp

Effective use of C++ iomanip library

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 19:23:07
问题 I created a Vector class in C++ and it works great for my problems. I am now cleaning it up, and I ran into the following piece of code: std::ostream& operator<<(std::ostream &output, const Vector &v){ output<<"[" <<std::setiosflags(std::ios::right | std::ios::scientific) <<std::setw(23) <<std::setprecision(16) <<v._x<<", " <<std::setiosflags(std::ios::right | std::ios::scientific) <<std::setw(23) <<std::setprecision(16) <<v._y<<", " <<std::setiosflags(std::ios::right | std::ios::scientific)

Reading a double from an istream in Hex

徘徊边缘 提交于 2019-12-12 19:14:52
问题 Given double foo I can assign it from a hex format string using sscanf like this: sscanf("0XD", "%lg", &foo) But I cannot seem to get an istringstream to behave the same way. All of these just write 0 to foo : istringstream("0XD") >> foo istringstream("0XD") >> hex >> foo istringstream("D") >> hex >> foo This is particularly concerning when I read here that the double istream extraction operator should: The check is made whether the char obtained from the previous steps is allowed in the

Truncating with setw

浪子不回头ぞ 提交于 2019-12-12 04:49:07
问题 Is there a way that I can force setw to truncate? Say that I want to get the output: blah blah blee le bu blah blah blee Is there a way to make this work: string foo{"bu blah blah blee le"}; cout << setw(foo.size() - 3) << foo.data() + 3 << setw(foo.size() - 3) << foo << endl; 回答1: No, not really. You can switch to unformatted output for this example, though: assert(foo.size() > 3); cout.write(&foo[3], foo.size() - 3); cout.write(&foo[0], foo.size() - 3); 回答2: Not directly. In the printf

How can I Format Width in puttime?

主宰稳场 提交于 2019-12-12 04:31:05
问题 Let's say that I want to print something simple like this table: January 1 February 2 March 3 April 4 May 5 June 6 July 7 August 8 September 9 October 10 November 11 December 12 I'd like to accomplish this like: for(tm i{ 0, 0, 0, 1, 0 }; i.tm_mon < 12; ++i.tm_mon) cout << put_time(&i, "%-9B") << i.tm_mon + 1 << endl; Unfortunately puttime doesn't seem to allow me to use field flags in it's format fields. Additionally this puttime doesn't seem to play nice with setw . Is my only option to do

C++ iomanip Alignment

随声附和 提交于 2019-12-11 11:44:53
问题 I'm trying to align my output but for some reason I can't get it to how I want it, and it's really frustrating. The title won't align right. I don't know if I'm using setw() properly. #include <iostream> using std::cout; using std::endl; #include <string> using std::string; #include <assert.h> #include <iomanip> using std::setw; using std::right; using std::left; //Movie Struct to hold movie data struct MOVIES { string Title; //Name of movie int CriticRating; //Critic rating 1-100 int