Hey. Thanks for all of the help with my other questions. This forum has been very helpful. In this program, there is a note: "chocolate" may refer to either the ice cream or the sauce; assume it refers to the ice cream if an ice cream flavor has not yet been selected. So, if I enter chocolate for the first question, it automatically skips the second (which asks for type of sauce) and goes right to the last question. How can I arrange it so that I can get input data to get an output such as: You have selected chocolate ice cream with hot fudge sauce and sprinkles?

#include <iostream>
#include <string>
using namespace std;
int main()
{
string softserve = "", topping = "", sprinkles = "";
string order;
do
{
if (softserve == "")
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"))
softserve = order;
if ((order == "hot fudge") || (order == "chocolate")
|| (order == "strawberry"))
topping = order;
if ((order == "yes") || (order == "no"))
sprinkles = order;
} while ((softserve == "") || (topping == "") || (sprinkles == ""));

if (sprinkles == "yes")
cout << "You ordered " << softserve << " ice cream with " << topping
<< " sauce and sprinkles." << endl;

else if (sprinkles == "no")

cout << "You ordered " << softserve << " ice cream with " << topping
<< " sauce without sprinkles." << endl;
}

Recommended Answers

All 28 Replies

Its not necessary to initialize std::strings with blanks because that is the default. Just declare them like this: std::string softserve, topping, sprinkles; I think your program needs more cin statements for data entry.

Well, I was just wondering why if the user enters chocolate for the first question, why does it input chocolate for the second question automatically.

If you wrote that then you should know why. You should also reformat the code to make it easier to read and understand. Example below:

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string softserve, topping, sprinkles;
    string order;
    do
    {
         if (softserve == "")
              cout << "Do you want chocolate, vanilla or twist?\n";  // endl not needed here
        if (topping == "")
<snip>

Ok. I did write the program. I wrote it using all of the variables I was supposed to use based on the assignment. After running it, I noticed this chocolate problem and was just looking for help.

The output should look like this:

I got that same output with the code you posted.

It's not the same. With my program, if you enter chocolate first, it skips right to the sprinkles question. It automatically enters chocolate for question 2. Another example (if you want to run it), is that if I enter twist first, and then chocolate for the sauce, and then yes or no for sprinkles (does not matter), it overrides the twist and says I ordered chocolate ice cream with chocolate sauce, when I really input twist ice cream with chocolate sauce.

The instructions you originally posted are very confusing. Is that the exact wording of the assignment, or just how you interpreted it. Please post exact assignment.

Write a program that asks the user to order an ice cream treat. The user selects
the type of ice cream, the type of sauce, and whether or not to add sprinkles. Use
the screen shots below as a guide. NOTE: "chocolate" may refer to either the ice cream
or the sauce; assume it refers to the ice cream if an ice cream flavor has not yet been selected.

these are the two screen shots he gave me as well

Sorry, I'll have to get to this tomorrow evening if someone else has not answered your questions by then.

Thanks Ancient Dragon. I really appreciate the help. Hopefully I have it by then.

My first unit using iostream XD

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string softserve, topping, sprinkles;
  string order;
  do
  {
    cout << "Do you want chocolate, vanilla or twist?" << endl;
    getline(cin, softserve);
  }
  while( (softserve!="chocolate") && (softserve!="vanilla") && (softserve!="twist") );
  do
  {
    cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
    getline(cin, topping);
  }
  while( (topping!="hot fudge") && (topping!="chocolate") && (topping!="strawberry") );
  do
  {
    cout << "Do you want sprinkles (yes/no)?" << endl;
    getline(cin, sprinkles);
  }
  while( (sprinkles!="yes") && (sprinkles!="no") );
  if (sprinkles == "yes")
    cout << "You ordered " << softserve << " ice cream with " << topping << " sauce and sprinkles." << endl;
  else
    cout << "You ordered " << softserve << " ice cream with " << topping << " sauce without sprinkles." << endl;
  cout << "Press a key to continue..." << endl;
  system("pause>nul");
}

That should work fine.
Here you only accept answers in lowercase. and you do a do{}while(); for each input because you wanna reject each wrong answer for each input, not after all 3 inputs have been made, that would take more lines and achieve the same.
I just copied some of your codes and copy and pasted as I saw fit. My only real contribution, code wise, is the pause at the end, which I searched in cplusplus.com for ;)

instead of system(pause>nul) why didn't you just simply use cin.get() ? you wouldn't have to have done all that net searching :)

My first unit using iostream XD

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string softserve, topping, sprinkles;
  string order;
  do
  {
    cout << "Do you want chocolate, vanilla or twist?" << endl;
    getline(cin, softserve);
  }
  while( (softserve!="chocolate") && (softserve!="vanilla") && (softserve!="twist") );
  do
  {
    cout << "Hot fudge, chocolate or strawberry sauce?" << endl;
    getline(cin, topping);
  }
  while( (topping!="hot fudge") && (topping!="chocolate") && (topping!="strawberry") );
  do
  {
    cout << "Do you want sprinkles (yes/no)?" << endl;
    getline(cin, sprinkles);
  }
  while( (sprinkles!="yes") && (sprinkles!="no") );
  if (sprinkles == "yes")
    cout << "You ordered " << softserve << " ice cream with " << topping << " sauce and sprinkles." << endl;
  else
    cout << "You ordered " << softserve << " ice cream with " << topping << " sauce without sprinkles." << endl;
  cout << "Press a key to continue..." << endl;
  system("pause>nul");
}

That should work fine.
Here you only accept answers in lowercase. and you do a do{}while(); for each input because you wanna reject each wrong answer for each input, not after all 3 inputs have been made, that would take more lines and achieve the same.
I just copied some of your codes and copy and pasted as I saw fit. My only real contribution, code wise, is the pause at the end, which I searched in cplusplus.com for ;)

yes, this does work. but, it needs to ask all of the unanswered questions every time. so, if the user enters twist ice cream, it should ask for the sauce and sprinkles (not just the sauce). also, when it is started, it only asks for type of ice cream. it should ask all 3 questions to start

All of the questions need to be inside of the loop I guess. That is what is giving me trouble. Any ideas?

I spoke to my teacher and showed him the problem. He said it is a simple fix but of course he wouldn't tell me. I have been trying dozens of things (moving things around), but I cannot get it to work the way I want.

I'll work on the code once I get home, right now I got a class and I'm already late =s
But here's an idea:

The first question repeats once.
The second question repeats twice.
The third question repeats thrice

int i, j, k;
for( i=0; i<3  ; i++)
{
  for( j=0; j < 2 && b; j++)
  {
    for( k=0; k <1 && c; k++)
      cout << "Do you want chocolate, vanilla or twist?\n";
    cout << "Hot fudge, chocolate or strawberry sauce?\n";
  }
  cout << "Do you want sprinkles (yes/no)?\n";
  if ( i == 0)
   cin >> softserver;
  if ( i == 1)
    cin >> topping;
  if ( i == 2)
    cin >> sprinkles;
}

Well, I don't think that code may be going anywhere now that I've written some of it. But that's what I could come up in 15 minutes.
I'm 37 minutes late for class XD
Will get back with you...

ok thanks emotionalone

Just checking the forums at school before I go home.
Try what I just posted but instead of for() use while() and have int a, b, c; tell the algorithm whether or not the question should keep looping.

while( ... && a )
{
   while( ... & b )
   {
      while( ... & c)
   }
}

Somethign like that. That way the values a, b, c (which should be either 0 or different than zero - the convention I guess is to make it 1 for different than zero).
I'll be back later!

Thanks emotionalone. But, I have to admit, I am confused with the a, b, c.

Last night as I was typing this message my internet got cut off U_U
Here is what I had for you last night but couldn't post it then:

I could finally solve it.
I used the idea you had on the draft you posted on the first page.
You wanna have a while() that only checks whether or not the 3rd answer string is empty or not. Then just do 3 if() each with 3 comparisons, 1 with the softserve, 1 with the topping and 1 with the sprinkles. Be careful with the order of these if() statements because they will all get evaluated each time you run through the loop and if a string changes, it should change where it won't make other if() true.
You will ask for the softserve as long as the softserve stream is empty, you can make sure it stays empty when an invalid input has been made by not copying the string directly to it but using an auxiliary stream, which you will use for all inputs.
I'll give you a lil hint code

while()
{
  if();
  if();
  if();
  getline(cin, choice) //choice is the auxiliary string
  if();
  if();
  if();
}

The first set of 3 if()'s will print the question, the second set will decide which choices compare the input to.
I have the finished code but I thought you might like some help instead of the code itself. I can post the code the next time I log in if that's what you need or you can post yours after reading this post :)

Good luck.

Thanks emotionalone. I appreciate your time and effort. This language is very confusing to me. I felt I was close with the one of the first page. Could you please PM me the code?

Sorry it took me so long, I had left the code at home and didn't have time to write it again in the middle of the day. Here is the code

#include <iostream>

using namespace std;

int main()
{
  string softserve, topping, sprinkles;
  string choice;

  while( sprinkles == "")
  {
    if( softserve == "" )
      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, choice);

    if( sprinkles == "" && topping != "" && softserve !="")
      if( choice == "yes" || choice =="no" )
        sprinkles = choice;

    if( sprinkles == "" && topping == "" && softserve !="")
      if( choice == "hot fudge" || choice =="chocolate" || choice == "strawberry" )
        topping = choice;


    if( sprinkles == "" && topping == "" && softserve =="")
      if( choice == "chocolate" || choice =="vanilla" || choice == "twist" )
        softserve = choice;

  }

  if (sprinkles == "yes")
    cout << "You ordered " << softserve << " ice cream with " << topping << " sauce and sprinkles." << endl;
  else
    cout << "You ordered " << softserve << " ice cream with " << topping << " sauce without sprinkles." << endl;
  cout << "Press a key to continue..." << endl;
  system("pause>nul");
}

It should work just how you want it to. Any questions or comments don't hesitate.

thank you very much. i may have more questions about other programs later.

Just for forum reference(lies XD), you can mark this thread as solved =3

I was looking at this solution for the problem since I was having the same difficulty with it. I found an issue with the supposed solved code for the problem. If the user enters a yes or a no as their choice for sprinkles, the program still repeats all the questions. The sprinkles answer will only be taken if it is the last questions answered. Here is my final code. Any help?:

int main()
{
  string flavor, topping, sprinkles, choice;
  int terminate = 0;

  while( sprinkles == "")
	{
		if (terminate++ > 30)
		{
			break;
		}

		if( flavor == "")
		{
			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, choice);
		
		if( sprinkles == "" && topping != "" && flavor != "")
		{
			if( choice == "yes" || choice =="no" )
			{
				sprinkles = choice;
			}
		}

		if( sprinkles == "" && topping == "" && flavor != "")
		{
			if( choice == "hot fudge" || choice == "chocolate" || choice == "strawberry" )
			{
				topping = choice;
			}
		}


		if( sprinkles == "" && topping == "" && flavor == "")
		{
			if( choice == "chocolate" || choice == "vanilla" || choice == "twist" )
			{
				flavor = choice;
			}
		}

	}

  if (sprinkles == "yes")
  {
	  cout << "You ordered " << flavor << " ice cream with " << topping << " sauce and sprinkles." << endl;
  }

  else
  {
	  cout << "You ordered " << flavor << " ice cream with " << topping << " sauce without sprinkles." << endl;
  }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.