How to use a while loop to ensure that the program ask the user if he would to continue?

I have 3 questions that I don't know how to do:-
1. void makeJuice(……)
To set all the data members(except for price) to the appropriate variables. Make function call to display();

2. In main() function get input from the user on the content for each fruit and water and pass the data to method makeJuice(….) so that the values can be set to the appropriate variables. Remember to call display() from the makeJuice()

3. Use a while loop in your main to ensure that the program ask the user if he would like to make a fruit juice first before creating the object and calling the functions. Using the while loop your program will continue to execute until user enters ‘n’ to quit.

Please teach me...
Thx.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>

using namespace std;

class FruitJuice
{
private:
    float water,mango,orange,apple,watermelon,lemon,price;

public:
    void makeJuice(float w,float m,float o,float a,float wm,float l)
    {
        water = w;
        mango = m;
        orange = o;
        apple = a;
        watermelon = wm;
        lemon = l;
    }


    void display()
    {
        float total;
        total = water + mango + orange + apple + watermelon + lemon;

        cout<<":::Your juice details"<<endl;
        cout<<"------------------------------------"<<endl;
        cout << "Water           : " << water << "ml" << endl;
        cout << "Mango           : " << mango << "ml" << endl;
        cout << "Orange          : " << orange << "ml" << endl;
        cout << "Apple           : " << apple << "ml" << endl;
        cout << "Watermelon      : " << watermelon << "ml" <<endl;
        cout << "Lemon           : " << lemon << "ml" << endl;
        cout << ":::Total ml             : " << total << "ml" << endl;
    }


    void calcPayment()
    {
        float price = 0.00;
        float totalprice;

          if(water > 0 || mango > 0 || orange > 0 || apple > 0 || watermelon > 0 || lemon > 0)
        {
            totalprice = price + (water * 0.00) + (mango * 0.10) + (orange * 0.15) + (apple * 0.15) + (watermelon * 0.12) + (lemon * 0.05);
        }
        cout << ":::Thank you, the price of your drink is RM "<< setiosflags(ios::fixed) << setprecision(2) << totalprice << endl;

    }

};


int main()
{
    FruitJuice J;
    float water, mango, orange, apple, watermelon, lemon;
    cout << ".....Making Fruit Juice.............." << endl;
    cout << "Set amount of water in ml                : ";
    cin >> water;
    cout << "Set amount of mango in ml                : ";
    cin >> mango;
    cout << "Set amount of orange in ml               : ";
    cin >> orange;
    cout << "Set amount of apple in ml                : ";
    cin >> apple;
    cout << "Set amount of watermelon in ml           : ";
    cin >> watermelon;
    cout << "Set amount of lemon in ml                : ";
    cin >> lemon;
    cout <<"TQ. Please wait a while..."<< endl;
    J.makeJuice(water, mango, orange, apple, watermelon, lemon);
    J.display();
    J.calcPayment();

    return 0;

}


The output should be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 Welcome to the Fruit juicer shiok
------------------------------------
Would you like to make a fruit juice? [y/n]
y
.....Making Fruit Juice..............
Set amount of water in ml                : 10
Set amount of mango in ml                : 10
Set amount of orange in ml               : 10
Set amount of apple in ml                : 0
Set amount of watermelon in ml           : 0
Set amount of lemon in ml                : 0
TQ. Please wait a while...
:::Your juice details
------------------------------------
Water           :10ml
Mango           :10ml
Orange          :10ml
Apple           :0ml
Watermelon      :0ml
Lemon           :0ml
:::Total ml             :30
:::Thank you, the price of your drink is RM2.50

Would you like to make another fruit juice? [y/n]
y
.....Making Fruit Juice..............
Set amount of water in ml                : 10
Set amount of mango in ml                : 30
Set amount of orange in ml               : 20
Set amount of apple in ml                : 10
Set amount of watermelon in ml           : 0
Set amount of lemon in ml                : 0
TQ. Please wait a while...
:::Your juice details
------------------------------------
Water           :10ml
Mango           :30ml
Orange          :20ml
Apple           :10ml
Watermelon      :0ml
Lemon           :0ml
:::Total ml             :70
:::Thank you, the price of your drink is RM7.50

Would you like to make another fruit juice? [y/n]
y
.....Making Fruit Juice..............
Set amount of water in ml                : 20
Set amount of mango in ml                : 10
Set amount of orange in ml               : 40
Set amount of apple in ml                : 0
Set amount of watermelon in ml           : 0
Set amount of lemon in ml                : 0
TQ. Please wait a while...
:::Your juice details
------------------------------------
Water           :20ml
Mango           :10ml
Orange          :40ml
Apple           :0ml
Watermelon      :0ml
Lemon           :0ml
:::Total ml             :70
:::Thank you, the price of your drink is RM7.00

Would you like to make another fruit juice? [y/n]
n
Press any key to continue

i believe that you must to create a char like this:

int main()
{

char ops;

do{

cout<<"Would you like to make another fruit juice? [y/n]";

cin>>ops;}

while(ops!='n');

cout<<"Thank you for your visit";
return 0;

}
I did that but when I want to continue, it keep ask "Would you like to make another fruit juice? [y/n]".
How can i make it loop from void makejuice when user prompt "y"?
do {
your main function
char yesOrNo
cout << "Again? (y/n)";
cin >> yesOrNo
} while (yesOrNo != n);
return 0;

something like this...
put your entire main function in loop, not just prompt.
also, your makeJuice function does nothing...
Make your main a loop and create a string and the loop continues until the string is != to y
if the size of the string is 1, the check if the string[0] is 'y' (tolower it, so that caps don't matter).
49: cout << ":::Thank you, the price of your drink is RM "<< setiosflags(ios::fixed) << setprecision(2) << totalprice << endl;

So are you from Malaysia?
Last edited on
remember to use : ' ' in the letter 'n'; or you can put in the while:
while(ops=='y');
yall are overcomplicating things. This is really easy:

1
2
3
4
5
6
7
8
9
10
11
12
13
string whatever;
if(whatever.size() == 1)
{
     whatever[0] = tolower(whatever[0]);
     if(whetever[0] == 'y')
     {
          return true;
     }
     if(whatever[0] == 'n')
     {
           return false;
     }
}


Easy...
@ngbeslhang
Yes I am.

My problem solved. Thank you guys!
@studentcpp
Oh really? I'm also from Malaysia! :D
Topic archived. No new replies allowed.