Question: I need help writing this fragment of code in C++: Write std::istream& operator >>(std::istream& is, rgb& colour) Define the operator overload of >>, i.e., std::istream&

I need help writing this fragment of code in C++:

Write std::istream& operator >>(std::istream& is, rgb& colour)

Define 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.

Write std::ostream& operator <<(std::ostream& os, rgb const& colour)

Define the operator overload of <<, i.e., std::ostream& operator <<(std::ostream& os, rgb const& colour), as follows:

  • Write out the contents of the provided rgb structure (i.e., colour --the second parameter to this function) to the output stream, os. Its components must be output in this order: red, green, blue. Also the components must be written out as unsigned (decimal) integers.
  • This function's return statement is always return os; (i.e., it returns the first parameter, os, to the function).
  • ADVICE:
    • Writing to os is just like writing values to std::cout, e.g., os << 34; would output the integer 34 to os.
    • It is legal and preferable when possible to write multiple values to os using more than one <<, e.g., os << 12 << ' ' << 24;
    • Notice that writing os << colour.red; will output a character --not an integer's numeric value. This is not desired --you are required to output an unsigned integer!
    • The C++ way to convert an unsigned char to an unsigned integer is to use static_cast, e.g., os << static_cast(colour.red);
      • NOTE: C-style casts are prohibited in this course as C++ has 4 special cast operators that should be used instead when needed.
Code: #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; };

istream& operator >>(istream& is, rgb& colour) { //code goes here }

ostream& operator <<(ostream& os, rgb const& colour) { //code goes here }

//You can now test your code, using a main() like this:

int main()

{

rgb value{}; // this declares an instance of the rgb structure

if (cin >> value)

cout << " Read successful. Writing: " << value << ' ';

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!