This may take your time || aww come onn ?

This is a question about my precious homework..

First I call Which function with the parameter "tomato" and with some other parameters..

1
2
3
4
void Which(parameters..)
{
	cout << name << ", do you want to buy some " << vegetable << " (Y/N): ";
	cin >> answer1;


(vegetables are tomato and eggplant, so the first question asks if the user wants some tomato)

1
2
3
4
5
6
if (answer1 != 'Y' && answer1 != 'N')
		{
			cout << name << ", you have entered an incorrect character." <<endl;
			cout << endl;
		}
}


Now the problem starts..

(1) If the user presses 'Y' then the function calls Price function and wants from user to enter the price of tomato.

(2) If the user presses 'N' then the function calls itself again but this time asks user if he/she wants some "eggplant". and again user have two choices (Y/N)

(3) If this time user chooses 'Y' then the function calls Price function and wants from user to enter the price of eggplant.

(4)If user chooses 'N' again then the program shows some proper message and finishes.

Now I need to do all this Y/N choices in only one function with if/else statements. I tried to do it but when I say 'N' to the "do you wanna buy some tomato" question, the program asks if the user wants some eggplant. And when I say 'Y', the program uses the situation (1) instead of situation (3).

So how can I make the program use the 3rd situation??

Last edited on
Do you have to use recursion? It seems a terrible solution to me.
How bout you post all your code?
yes I have to use recursion =/

I haven't finished yet cuz I couldnt pass this step but here's some of it
some told me to use bool types but it just messed me up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

void Which(string name, double budget, string vegetable)
{
	char answer1;
	cout << name << ", do you want to buy some " << vegetable << " (Y/N): ";
	cin >> answer1;

	if (answer1 != 'Y' && answer1 != 'N')
	{
		cout << name << ", you have entered an incorrect character." <<endl;
		cout << endl;
	}

	if (answer1 == 'Y')
	{
		Price(name, budget, "tomato");
	}

	if (answer1 == 'N')
	{
		cout << name <<	", you have selected not to buy any " << vegetable << ". You have " << budget << " TL." <<endl;
		Which(name, budget, "eggplant");
	}
}
Last edited on
else (budget >= 1 && budget <= 150);
This doesn't make any sense; it needs to be either else if ( stuff ) or just else. And that semicolon needs to go away.

Price(name, budget, "tomato");
The third parameter needs to be vegetable.
Last edited on
well that wasn't actually my question =/

well that could be it // wow thats actually really smart thanks
Last edited on
Well I'd do something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Which(string name, double budget, string vegetable, bool firstCall = true)
{
	char answer1;
	cout << name << ", do you want to buy some " << vegetable << " (Y/N): ";
	cin >> answer1;
        if(firstCall)
        {
               // The code you already have
               if (answer1 == 'N')
	       {
		        cout << name <<	", you have selected not to buy any " << vegetable << ". You have " << budget << " TL." <<endl;
                        // Note the false that signals that the function is being called from inside itself
		        Which(name, budget, "eggplant", false);
	        }
	}
        else
        {
        // Write the logic for the second case
        }
}

Or yeah, you can compare the vegetable string instead of adding a parameter.
But really, this looks sooo ugly.
Last edited on
The second part sort of was. To get the program to finish when the user says "N" to eggplant, you could create a static variable and increment it when the function is called, and end the program when it is called a second time, or check if vegetable is "eggplant" and end the program if it is.
Topic archived. No new replies allowed.