Hi, I'm having trouble writing a program that asks the user 3 questions.(3 possible answers for each) I am using a string to remember all the answers with a getline(cin,answer) statement.

My problem is the first two questions have choices for the answers that can be the same such as red, red.

The program works fine if the answers are all different, but when I put in "red" it automatically puts red for the first two answers and jumps to the 3rd question.

Any thoughts on how to get around this? I tried using a continute statement, but had no luck.

Thanks.

Recommended Answers

All 6 Replies

Let me see some of that sweet lovin' code..

Let me see some of that sweet lovin' code..

int main ()
{
    string icecream = "", sauce = "", sprinkles = "";
    string order;

    do
    {
        if (icecream == "")
            cout << "Do you want chocolate, vanilla, or twist?" << endl;
        if (sauce == "")
            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"))
            sauce = order;
        if ((order == "yes") || (order == "no"))
            sprinkles = order;
    }
        while ((icecream == "") || (sauce == "") || (sprinkles == ""));
    if (sprinkles == "yes")
        cout << "You ordered " << icecream << " icecream with " << sauce << " sauce with sprinkles." << endl;
    else if (sprinkles == "no")
        cout << "You ordered " << icecream << " icecream with " << sauce << " sauce without sprinkles." << endl;
}

You are using getline() to obtain an entire line of user input and storing that input into a <string> object called 'order'

You are then attempting to use boolean logic to test 'order' to test an entire line of user input that may contain several answers to the prompted questions asked by ye' program.

//Here you essentially ask the user 3 questions at once in which he/she has to reply to all 3 before hitting the [Enter] key:

if (icecream == "")            

     cout << "Do you want chocolate, vanilla, or twist?" << endl;        

if (sauce == "")            

     cout << "Hot fudge, chocolate or strawberry sauce?" << endl;        

if (sprinkles == "")            

     cout << "Do you want sprinkles (yes/no)?" << endl;

Here, getline() will have to accept all 3 answers to the above questions... all at once:

getline(cin, order);

So at this point, you have a string that probably looks like this: "chocolate hot fudge no", which is all of your answers in one string.

And now you are attempting to test 'order'; however, 'order' will never satisfy any of the tests.. because it holds 3 answers. All your boolean tests attempt to test one answer at a time:

//At this point, order would look something like this:
// "chocolate hot fudge yes" which will never satisfy any of your tests:

//No dice
if ((order == "chocolate") || (order == "vanilla") || (order == "twist"))

//Won't work
if ((order == "hot fudge") || (order == "chocolate") || (order == "strawberry"))

//Will never satisfy any of this
((order == "yes") || (order == "no"))

So how do we overcome this problem? There are 2 ways: parse 'order' into substrings (tokens) and test them individually or accept user input individually and test them individually.

I will suggest the second option because it's the easiest:

string ice_cream, topping, sprinkles;

cout << "What kind o' delicious ice cream would ye' like? ";
cout << "\nHot fudge, chocolate or strawberry sauce?";
cin >> ice_cream;

cout << "What you want on it... ";
cout << "\n\nHot fudge, chocolate or strawberry sauce?" ;
cin >> topping;

cout << "Sprinkles?  (yes/no) "; 
cin >> sprinkles;

Now you can do stuff like this:

if (ice_cream == "chocolate" || ice_cream == "vanilla" || ice_cream == "twist")

if (topping == "hot fudge" || topping == "chocolate" || topping == "strawberry")

if (sprinkles == "yes")

else

Thank you for the comments. Unfortunatly I have to do this program as I had listed with all the questions appearing at once.

After the first questions answer is satisfied only the remaining two questions should appear and so on.

Would you recommend breaking the order string down? If so how?

Ye' can still ask all your questions at once and still be able to accept user input individually:

cout << "What type of ice cream...? ";
cout << "\nWhat kind of topping...?";
cout << "\nSprinkles bueno? (si o no)";

cin >> ice_cream >> topping >> sprinkles;

Just like you can chain together output to the console, you can chain input from the user.

If you still would like a class on parsing strings into substrings.. lemme know.

Go redskins.

>>Would you recommend breaking the order string down? If so how?

I guess if you really have to. Here is an example, breakString2 is just
breakString1 using loops, see how much space is saved. Anyways, I
provided both so you can understand what one function is doing :

#include<iostream>
#include<string>

using namespace std;

 //breaks string according the finding of a space
//vector would be better but I am not sure if you know about them.
void breakString1(string& source, string& dest1, string& dest2, string& dest3){
	//include error checker here
	string firstFavThing = "";
	string secondFavThing = "";
	string thirdFavThing = "";

	//finds the end of the position of each word
	size_t foundPos1 = 0;	
	size_t foundPos2 = 0;	
	size_t foundPos3 = 0;

	foundPos1 = source.find(" "); //find end of first space deliminated string
	firstFavThing = source.substr(0,foundPos1);
	
	foundPos1++; //by pass the space

	foundPos2 = source.find(" ",foundPos1); //find end of second string
	secondFavThing = source.substr(foundPos1,foundPos2-foundPos1);

	foundPos2++; //bypass the space

	foundPos3 = source.find(" ",foundPos2); //find end of third string
	thirdFavThing = source.substr(foundPos2,foundPos3-foundPos2);

	dest1 = firstFavThing;
	dest2 = secondFavThing;
	dest3 = thirdFavThing;
}
void breakString2(string& source, string& dest1, string& dest2, string& dest3)
{	
   //include error checker here
	size_t start = 0;
	size_t end = 0;
	string subStr[3] = {""};
	int subStrIndx = 0;
	while(subStrIndx < 3)
	{
		end = source.find(" ",start);
		subStr[subStrIndx] = source.substr(start,end-start);
		subStrIndx++;
		start = ++end;		
	}
	dest1 = subStr[0];
	dest2 = subStr[1];
	dest3 = subStr[2];
}
int main(){	
	
	string allAnswer = "";
	
	cout<<"Enter 3 of your favorite things in the world : ";
	 //assumming input is like so :  basketball killing reading  and not basketballkillingreading
	getline(cin,allAnswer);

	string firstFavThing = "";
	string secondFavThing = "";
	string thirdFavThing = "";

	breakString1(allAnswer,firstFavThing,secondFavThing,thirdFavThing);
	
	cout<<"using breakString1 : " << endl;
	cout << firstFavThing << endl;
	cout <<secondFavThing <<endl;
	cout<<thirdFavThing << endl << endl;
			
	cout<<"using breakString2 : " << endl;
	breakString2(allAnswer,firstFavThing,secondFavThing,thirdFavThing);
		
	cout << firstFavThing << endl;
	cout <<secondFavThing <<endl;
	cout<<thirdFavThing << endl;

	return 0;
}
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.