Muffin createMuffin(istringstream& inStream) { Muffin muffin; string description; int size; bool hasChips; inStream >> description >> size >> boolalpha >> hasChips; muffin.setSize(size); muffin.setDescription(description); muffin.setHasChocolateChips(hasChips); return muffin; }
Muffin createMuffin(istringstream& stream) { Muffin muffin; // Assume data is properly formatted: // Description size chips string description; int size; bool hasChips; // Read all three values. Note that chips is represented // by the strings "true" and "false" stream >> description >> size >> boolalpha >> hasChips; if (stream) { // Reading was successful. muffin.setSize(size); muffin.setDescription(description); muffin.setHasChocolateChips(hasChips); } return muffin; }
int main() { cout << "First, let's create a muffin in code and output it." << endl; Muffin myMuffin; myMuffin.setDescription("Giant_Blueberry_Muffin"); myMuffin.setSize(42); myMuffin.setHasChocolateChips(false); printf("The muffin is "); myMuffin.output(); printf(" -- yummy!\n"); cout << "Now we'll create a muffin from a string stream" << endl; istringstream instream("My_Muffin 2 true"); Muffin m2 = createMuffin(instream); m2.output(); return 0; }