Trying to take a value from inside a function

Hello! I am trying to do an assignment that requires me to use functions. In the second option "Display Total Daily Sale" I want to obtain the total amount of money from option one "Purchase Drink(s)" and place it inside but I cannot find a way to do so. Any help at all will be appreciated, thank you!

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
 #include <iostream>
using namespace std;
void purchaseDrinks();
void dailyTotal();
int main()
{
    int selection, password;

    cout<<"   ~Welcome to Sport Drink Menu~ \n ~Please selection and option below~"<<endl;
    cout<<" ------------------------------------"<<endl;
    cout<<" 1) Purchase Drink(s) \n 2) Display Total Daily Sale \n 3) End Program"<<endl;
    cin>>selection;

   if(selection==1){purchaseDrinks();}
   if(selection==2){dailyTotal();}
   else cout<<"Thank you for using Sport Drink Menu! ";
   return 0;
}
void purchaseDrinks()
{
    int total, drinkSize, amount;

        cout<<"        ~Which size?~ "<<endl;
        cout<<"--------------------------------"<<endl;
        cout<<" 1)Small\n 2)Medium\n 3)Large"<<endl;
        cin>>drinkSize;
        while(drinkSize<1 || drinkSize>3)
        {
            cout<<" Please enter valid option."<<endl;
            cout<<"        ~Which size?~ "<<endl;
            cout<<"--------------------------------"<<endl;
            cout<<" 1)Small\n 2)Medium\n 3)Large"<<endl;
            cin>>drinkSize;
        }
        cout<<endl;

    switch(drinkSize)
    {
            case 1:
            cout<<" How many small drinks will you be purchasing? "<<endl;
            cin>>amount;
            total= amount*10;
            break;
            case 2:
            cout<<" How many medium drinks will you be purchasing? "<<endl;
            cin>>amount;
            total = amount*15;
            break;
            case 3:
            cout<<" How many large drinks will you be purchasing? "<<endl;
            cin>>amount;
            total = amount*18;
            break;
    }
    cout<<" Your total is : $"<<total;
    cout<<" Thank you for using Sport Drink Menu! ";
}
void dailyTotal()
{
       int password;

       cout<<" Enter Password :";
       cin>>password;

    if(password !=7319)
    {
        for(int i=0; i<3; i++)
            {
                cout<<" Access Denied. "<<3-i<<" Attempts Remaining"<<endl;
                cin>>password;
            }
        cout<<" LOCKED ";
    }
       //cout<<"test"; // cout<<" Total Daily Purchase : $"<<total somehow?
}
You can use either a class/struct if you know how or use global variables (fine for small projects but a bad practice for the future).

Using global variable
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
82
#include <iostream>

using namespace std;

int total = 0; //Global variable


void purchaseDrinks();
void dailyTotal();
int main()
{
    int selection, password;

    cout<<"   ~Welcome to Sport Drink Menu~ \n ~Please selection and option below~"<<endl;
    cout<<" ------------------------------------"<<endl;
    cout<<" 1) Purchase Drink(s) \n 2) Display Total Daily Sale \n 3) End Program"<<endl;
    cin>>selection;

   if(selection==1){purchaseDrinks();}
   if(selection==2){dailyTotal();}
   else cout<<"Thank you for using Sport Drink Menu! ";
   return 0;
}
void purchaseDrinks()
{
    int drinkSize, amount;

        cout<<"        ~Which size?~ "<<endl;
        cout<<"--------------------------------"<<endl;
        cout<<" 1)Small\n 2)Medium\n 3)Large"<<endl;
        cin>>drinkSize;
        while(drinkSize<1 || drinkSize>3)
        {
            cout<<" Please enter valid option."<<endl;
            cout<<"        ~Which size?~ "<<endl;
            cout<<"--------------------------------"<<endl;
            cout<<" 1)Small\n 2)Medium\n 3)Large"<<endl;
            cin>>drinkSize;
        }
        cout<<endl;

    switch(drinkSize)
    {
            case 1:
            cout<<" How many small drinks will you be purchasing? "<<endl;
            cin>>amount;
            total= amount*10;
            break;
            case 2:
            cout<<" How many medium drinks will you be purchasing? "<<endl;
            cin>>amount;
            total = amount*15;
            break;
            case 3:
            cout<<" How many large drinks will you be purchasing? "<<endl;
            cin>>amount;
            total = amount*18;
            break;
    }
    else
        cout<<" Your total is : $"<<total;
    cout<<" Thank you for using Sport Drink Menu! ";
}
void dailyTotal()
{
       int password;

       cout<<" Enter Password :";
       cin>>password;

    if(password !=7319)
    {
        for(int i=0; i<3; i++)
            {
                cout<<" Access Denied. "<<3-i<<" Attempts Remaining"<<endl;
                cin>>password;
            }
        cout<<" LOCKED ";
    }
    else
        cout << "Total: " << total << endl;
}


Using classes:
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
82
83
84
85
#include <iostream>

using namespace std;

class Store
{
public:
    void purchaseDrinks();
    void dailyTotal();
private:
    int total;

};
int main()
{
    int selection, password;
    Store store;

    cout<<"   ~Welcome to Sport Drink Menu~ \n ~Please selection and option below~"<<endl;
    cout<<" ------------------------------------"<<endl;
    cout<<" 1) Purchase Drink(s) \n 2) Display Total Daily Sale \n 3) End Program"<<endl;
    cin>>selection;

   if(selection==1){store.purchaseDrinks();}
   if(selection==2){store.dailyTotal();}
   else cout<<"Thank you for using Sport Drink Menu! ";
   return 0;
}
void Store::purchaseDrinks()
{
    int drinkSize, amount;

        cout<<"        ~Which size?~ "<<endl;
        cout<<"--------------------------------"<<endl;
        cout<<" 1)Small\n 2)Medium\n 3)Large"<<endl;
        cin>>drinkSize;
        while(drinkSize<1 || drinkSize>3)
        {
            cout<<" Please enter valid option."<<endl;
            cout<<"        ~Which size?~ "<<endl;
            cout<<"--------------------------------"<<endl;
            cout<<" 1)Small\n 2)Medium\n 3)Large"<<endl;
            cin>>drinkSize;
        }
        cout<<endl;

    switch(drinkSize)
    {
            case 1:
            cout<<" How many small drinks will you be purchasing? "<<endl;
            cin>>amount;
            total= amount*10;
            break;
            case 2:
            cout<<" How many medium drinks will you be purchasing? "<<endl;
            cin>>amount;
            total = amount*15;
            break;
            case 3:
            cout<<" How many large drinks will you be purchasing? "<<endl;
            cin>>amount;
            total = amount*18;
            break;
    }
    cout<<" Your total is : $"<<total;
    cout<<" Thank you for using Sport Drink Menu! ";
}
void Store::dailyTotal()
{
       int password;

       cout<<" Enter Password :";
       cin>>password;

    if(password !=7319)
    {
        for(int i=0; i<3; i++)
            {
                cout<<" Access Denied. "<<3-i<<" Attempts Remaining"<<endl;
                cin>>password;
            }
        cout<<" LOCKED ";
    }
    cout<<"Total: " << total << endl;
}
Last edited on
Topic archived. No new replies allowed.