954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

another program

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;
}
joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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.

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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>
Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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.

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

The output should look like this:

Attachments screen_shot.jpg 37.6KB
joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

I got that same output with the code you posted.

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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.

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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.

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

these are the two screen shots he gave me as well

Attachments screen_shot.jpg 37.6KB screen_shot_2.jpg 31.44KB
joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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 ;)

emotionalone
Light Poster
33 posts since Oct 2008
Reputation Points: 10
Solved Threads: 4
 

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 :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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.

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Any ideas?

joed13k1941
Light Poster
47 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

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...

emotionalone
Light Poster
33 posts since Oct 2008
Reputation Points: 10
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You