Hello. I am supposed to program something like this, which prints the size and t
ID: 3540092 • Letter: H
Question
Hello. I am supposed to program something like this, which prints the size and three chosen flavors of a yogurt order:
----------------------------------------------------------
What size wold you like?
Please enter small, medium, or large: small //for example, choose small
Enter flavor 1: chocolate // choose
Enter flavor 2: DONE // When the second flavor is "DONE", the program should stop asking for the third flavor
** Order 1: small choc **
---------------------------------------------------------
But when I run it, what I got is like this:
------------------------------------------------------
What size wold you like?
Please enter small, medium, or large: small
Enter flavor 1: chocolate
Enter flavor 2: DONE
** Order 1: small choc **
** Order 1: small choc-DONE- **
---------------------------------------------------------
There is one extra line of the order description.
Here is my codes. Can someone please fix my problem?
void getYogurtFlavors(string& flavor1, string& flavor2, string& flavor3){
cout<<" ";
cout<<"Enter flavor 1: ";
cin.ignore();
getline(cin, flavor1);
cout<<"Enter flavor 2: ";
getline(cin, flavor2);
if(flavor2=="DONE"){
cout<<" ";
}
else{
cout<<"Enter Flavor 3: ";
getline(cin, flavor3);
cout<<" ";
}
}
void printOrder(const string& yogurtSize, const string& flavor1, const string& flavor2, const string& flavor3, int orderNumber){
if(flavor2=="DONE"){
cout<<"** Order "<<orderNumber<<": "<<yogurtSize+" "+flavor1.substr(0,4)<<" **"<<endl;
}
if(flavor3=="DONE"){
cout<<"** Order "<<orderNumber<<": "<<yogurtSize+" "+flavor1.substr(0,4)+"-"+flavor2.substr(0,4)<<" **"<<endl;
}
else{
cout<<"** Order "<<orderNumber<<": "<<yogurtSize+" "+flavor1.substr(0,4)+"-"+flavor2.substr(0,4)+"-"+flavor3.substr(0,4)<<" **"<<endl;
}
cout<<" ";
}
Explanation / Answer
#include #include // for std::string #include // for std::transform #include // for std::tolower const std::string flavors[] = { "Chocolate", "Vanilla", "Gumball", "Turkey", "Wild Nuts" }; const std::string sizes[] = { "Small", "Medium", "Large", "Super Gigantic" }; // a common method of getting the number of elements in an array (in // the scope in which the array is defined) is to take the overall // size of the array and divide it by the size of one of it's elements. const unsigned num_flavors = sizeof(flavors) / sizeof(flavors[0]) ; const unsigned num_sizes = sizeof(sizes) / sizeof(sizes[0]) ; // just to shorten things up a bit for posting: typedef const std::string const_string ; // returns true if to_check occurs in labels. bool check_against( const_string to_check, const_string labels[], unsigned elements ) ; // convenience function for displaying the list of flavors/sizes void display(std::ostream & out, const_string labels[], unsigned elements, const_string title, const_string prompt) ; int main() { std::string flavor ; do { display( std::cout, flavors, num_flavors, "Flavors are:", "What flavor would you like?" ) ; std::getline(std::cin, flavor); } while (!check_against(flavor, flavors, num_flavors)) ; std::string size ; do { display( std::cout, sizes, num_sizes, "Sizes are:", "What size would you like?") ; std::getline(std::cin, size) ; } while ( !check_against(size, sizes, num_sizes) ) ; std::coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.