At 3/24/2009 01:21 PM, you wrote:
>Hello list!
>
>My name is Mark and I'm a new member.  I'm a programmer with a 
>background in VB, .NET, php and a few other languages but not a lot 
>of experience in c or c++.
>
>A buddy of mine is taking a programming class in c++ and I normally 
>offer assistance but I'm stuck on this one and I have to know the 
>answer out of curiosity.
>
>His goal is to ask the user 3 questions about the type of ice cream 
>they want to order.
>
>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?
>
>As it stands now, the program works fine if the user doesn't select 
>chocolate for the first or second question but if they do, it sets 
>both the the first and second value to chocolate regardless of 
>whether they actually did that.
>
>TIA,
>
>Mark
>
>#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;
>}

It occurs because both icecream and stopping can be chocolate. So if 
you reply chocolate, it will set icecream and topping to chocolate.

You might want to check to see if icecream/topping/sprinkles are null 
("") before setting a value. You can/should also do if ... else if .. else

~Rick 

Reply via email to