Question
I am confused about why am getting this error, here is my code: main.cpp: int main(int argc, char *argv[]) { string prompt = Please enter
I am confused about why am getting this error, here is my code:
main.cpp:
int main(int argc, char *argv[])
{
string prompt = "Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., \"255 255 255\")(CTRL-D to exit) : ";
/*
Color color;
cout << "Color values after declaration : " << color.to_str() << endl;
cout << prompt;
for (int r, g, b ; ; ) {
while (!(cin >> r >> g >> b)) {
if (cin.bad() || cin.eof()) {
return 1;
} else if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits
}
cout << "Invalid input." << endl << endl << prompt;
cin >> r >> g >> b;
}
color.set_R(r); color.set_G(g); color.set_B(b);
cout << "Color : " << color.to_str() << endl << endl;
cout << prompt;
}
*/
// Part 2
cout << prompt;
for (int r, g, b ; ; ) {
while (!(cin >> r >> g >> b)) {
if (cin.bad() || cin.eof()) {
return 1;
} else if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits
}
cout << "Invalid input." << endl << endl << prompt;
cin >> r >> g >> b;
}
Color color(r, g, b);
cout << "Color : " << color.to_str() << endl << endl;
cout << prompt;
}
return 0;
}
color.cpp:
////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // CLASS CONSTRUCTORS LAB // // COLOR.CPP // // // // Date ...: 18/MAR/2017 // ////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////// // Includes // ////////////////////////////////////////////////////////////////////////////// #include "Color.h"
////////////////////////////////////////////////////////////////////////////// // Color Class Member Definitions // ////////////////////////////////////////////////////////////////////////////// Color::Color() { this->R = 255; this->G = 255; this->B = 255;
} Color::Color(int R, int G, int B) { if(R>255 || R<0)throw Color_error{}; this->R = R; if(G>255 || G<0)throw Color_error{}; this->G = G; if(B>255 || B<0)throw Color_error{}; this->B = B;
} std::string Color::to_str() { std::stringstream ss; ss << '(' << R << ',' << G << ',' << B << ')'; return ss.str(); }
void Color::set_R(int r) { if (!is_valid_val(r)) throw Color_error{}; R = r; } void Color::set_G(int g) { if (!is_valid_val(g)) throw Color_error{}; G = g; } void Color::set_B(int b) { if (!is_valid_val(b)) throw Color_error{}; B = b; } bool Color::is_valid_val(int v) { if (v < 0 || v > 255) return false; else return true; }
color.h
////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// // CLASS CONSTRUCTORS LAB // // COLOR.H // // // // Date ...: 18/MAR/2017 // ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// #ifndef COLOR_H #define COLOR_H
////////////////////////////////////////////////////////////////////////////// // Dependencies // ////////////////////////////////////////////////////////////////////////////// #include
////////////////////////////////////////////////////////////////////////////// // Color Class Definition // ////////////////////////////////////////////////////////////////////////////// class Color_error {}; class Color { public: Color(); Color(int, int, int); std::string to_str(); bool is_valid_val(int); void set_R(int); void set_G(int); void set_B(int); private: int R; int G; int B; }; ////////////////////////////////////////////////////////////////////////////// #endif
here is my error:
YOUR CODE'S OUTPUT
Color values after declaration : (255,255,255) Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Color : (123,234,98) Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Invalid input. Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Invalid input. Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Invalid input. Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Invalid input. Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Invalid input. Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) :
`UNIX DIFF` OF CORRECT OUTPUT AND YOUR OUTPUT
2c2,3 < Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Color : (123,234,98) --- > Please enter the r, g, and b integer values of a color, > with each separated by a space (e.x., "255 255 255"): Color : (123,234,98) 4c5,6 < Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Invalid input. --- > Do another? (y or Y) Please enter the r, g, and b integer values of a color, > with each separated by a space (e.x., "255 255 255"): Invalid input. 6c8,9 < Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Invalid input. --- > Do another? (y or Y) Please enter the r, g, and b integer values of a color, > with each separated by a space (e.x., "255 255 255"): Invalid input. 8c11,12 < Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Invalid input. --- > Please enter the r, g, and b integer values of a color, > with each separated by a space (e.x., "255 255 255"): Invalid input. 10c14,15 < Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Invalid input. --- > Please enter the r, g, and b integer values of a color, > with each separated by a space (e.x., "255 255 255"): Color : (50,150,250) 12,14c17 < Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : Invalid input. < < Please enter the r, g, and b integer values of a color, with each separated by a space (e.x., "255 255 255")(CTRL-D to exit) : \ No newline at end of file --- > Do another? (y or Y) Goodbye!
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