Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help doing this fragment of code in C++. The step asks to write std::istream& operator >>(std::istream& is, rgb& colour) by: Defining the operator
I need help doing this fragment of code in C++. The step asks to write std::istream& operator >>(std::istream& is, rgb& colour) by:
Defining the operator overload of >>, i.e., std::istream& operator >>(std::istream& is, rgb& colour), as follows:
- Read in an rgb structure into the colour parameter (i.e., the second parameter to this function).
- The values are read in as whitespace-delimited numbers between 0 and 255.
- The components will always appear in this order in the input stream: red, green, blue.
- Regardless of how this function executes, its return statement is always return is; (i.e., the first std::istream& argument is returned).
- If any component cannot be read in, then set the stream to "fail" with is.setstate(ios_base::failbit); and return from the function.
- If any component cannot be read in, then ensure you never alter/update any values in colour (i.e., the second parameter to this function).
- ADVICE: When reading in the components, declare them as local unsigned (not unsigned char) variables.
Code in C++: #include // for std::sqrt #include // for std::array #include // for std::vector #include // for std::numeric_limits #include // for std::string #include // for std::istream #include // for std::ostream #include // for std::cin, std::cout #include // for std::transform using namespace std; // place this after the #includes
struct rgb { unsigned char red,
unsigned char green;
unsigned char blue; };
//code goes here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started