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

 ``1234`` ``````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)

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

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.

 ``123456789101112131415161718192021222324`` `````` 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." <
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
 ``1234567891011121314151617181920`` ``````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." <

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.