Mark E wrote:
> Below is the code but I'm not sure I understand the use of getline in this
> case. From what I read, it may be storing all 3 values in to a single string
> and it needs to be split. If that's the case, what's the best way to handle
> this? And will the 3 if statements work correctly at that point?
>
Personally, I would refactor this code slightly, even considering this
is a basic programming assignment. First, I would use "else" statements
so the user is not flooded with questions: the program would prompt one
at a time. Ideally I would refactor the decision logic into separate
classes and use polymorphism... but that is the sledgehammer approach
and probably not what the professor is looking for :-)
If you did want to prompt multiple things at a time, I would recommend
specifying how to separate the input: spaces? Commas? Three separate
lines? The program is ambiguous in this regard, currently, it accepts
any input for any variable, but only one input.
Also, thank you for being explicit about it being homework: you asked
the right questions in the right way, instead of saying "do my work
plzzzzzzz lol" like most people do.
> #include <iostream>
> #include <string>
> using namespace std;
> int main()
> {
> string icecream = "", topping = "", sprinkles = "";
> string order;
> do
> {
> if (icecream == "")
> cout << "Do you want chocolate, vanilla, or twist?"<<endl;
> if (topping == "")
> cout << "Hot fudge, chocolate, or strawberry sauce?"<<endl;
> if (sprinkles =="")
> cout << "Do you want sprinkles (yes/no)?"<< endl;
> getline(cin, order);
> if ((order == "chocolate") || (order == "vanilla") || (order == "twist"))
> icecream = order;
> if ((order == "hot fudge") || (order == "chocolate") || (order ==
> "strawberry"))
> topping = order;
> if ((order == "yes") || (order == "no"))
> sprinkles = order;
>
> }while ((icecream == "") || (topping == "") || (sprinkles ==""));
> if (sprinkles == "yes")
> cout << "You ordered "<< icecream << " ice cream with " << topping << "
> sauce and sprinkles."<< endl;
> else
> cout << "You ordered "<< icecream << " ice cream with " << topping << "
> sauce without sprinkles."<<endl;
> }
>
--
John Gaughan