All, Thanks for the input. I'll see what we can do to get it working. The instructor is very specific about what can be used in the program. My assumption is that he wants to use more basic techniques in an attempt to bring the students along and will introduce more advanced topics at a later date. As far as relaying it was homework - I've seen that a lot of those typical homework posts myself and didn't want anyone to just do it. Additionally, it's not my assignment but I must confess that I've been rather intrigued with helping my friend. He does 99% of the work but tends to get caught on some of the smaller things and this has allowed me to get a little exposure to c++. Mark
--- On Tue, 3/24/09, John Gaughan <[email protected]> wrote: From: John Gaughan <[email protected]> Subject: Re: [c-prog] Intro and first question (getline) To: [email protected] Date: Tuesday, March 24, 2009, 10:23 PM 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 [Non-text portions of this message have been removed]
