istream & operator>>(istream &in, Rock &rockVal) {
in >> rockVal;
return in;
}
Im defining a variable like so: Rock sample; and then Im trying to use cin>> sample; to input BASALT or another rock type, and its not working. I'm getting a segmentation fault for some reason. I'm new to this so much help is appreciated! I basically want to input anytime and anyform like so: BASALT, basalt, Basalt, etc.. into my sample variable, and I want the operator to convert it for me.
You need to read a string, then convert the string to the appropriate value.
1 2 3 4 5 6 7 8 9 10 11
std::istream& operator>>( std::istream& is, Rock& rock ) {
std::string str;
is >> str;
if( strcasecmp( str.str(), "basalt" ) ) {
rock = BASALT;
return is;
} elseif( /* ... */ ) {
} else {
// Didn't read anything valid; set the fail bit on is and return.
}
}