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++.
>
>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.
>
Mark,
As I mentioned in my first reply, and as John mentioned, you should
use if/else if statements. getline() is not the problem. The problem
is in your logic.
Below is some code that will work. It still is not pretty, but it works.
~Rick
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string icecream = "", topping = "", sprinkles = "";
string order;
do
{
if (icecream == "")
cout << "Do you want chocolate, vanilla, or twist?"<<endl;
else if (topping == "")
cout << "Hot fudge, chocolate, or strawberry sauce?"<<endl;
else if (sprinkles =="")
cout << "Do you want sprinkles (yes/no)?"<< endl;
getline(cin, order);
if ( (icecream == "") && ((order == "chocolate") || (order ==
"vanilla") || (order == "twist")) )
icecream = order;
else if ( (topping == "") && ((order == "hot fudge") || (order ==
"chocolate") || (order == "strawberry")) )
topping = order;
else if ( (sprinkles =="") && ((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;
system("PAUSE");
return EXIT_SUCCESS;
}