Functions

Hello, first time poster and begginer coding. Basically, my program below isn't storing and returning the values I need in order to continue into the next function. I have tried everything I could think of and searched the forums and I cannot find anything. Please Help!
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
86
87
88
89
 #include <iostream>
#include <cmath>

using namespace std;

double books(int, char, char, float);
int bookCost (int, float);
double profit (float);

int main()
{
    int code;
    int cost;
    int copies;
    int enrollment;
    char req_opt;
    char used;
    float price;
    int numBooks;
    int priceBook;
    float total;
    float totalProfit;


    cout << "What is the code of the book?"<< endl;
    cin >> code;
    cout << "What is the cost of one copy?"<< endl;
    cin >> cost;
    cout << "How many books do you currently have?"<< endl;
    cin >> copies;
    cout << "Is the book required or optional?( R or O)" << endl;
    cin >> req_opt;
    cout << "Has the book been used before ( Y or N)?"<< endl;
    cin >> used;
    cout << "What is the prospective class enrollment?"<<endl;
    cin >> enrollment;

    numBooks = books(enrollment, req_opt, used, price);

    priceBook = bookCost(cost, total);
    totalProfit = profit (price);

}

double books(int enrollment, char req_opt, char used, float total)
{
    float copiesN;

   if (req_opt =='r' && used == 'n' || req_opt == 'R' && used == 'N')
    {
        copiesN = enrollment * 1.9;
    }
    else if (req_opt == 'r' && used == 'y' || req_opt == 'R' && used == 'Y')
    {
        copiesN = enrollment * 1.65;

    }
    else if (req_opt == 'o' && used == 'n'|| req_opt == 'O' && used == 'N')
    {
        copiesN = enrollment * 1.4;
    }
    else
    {
        copiesN = enrollment * 1.2;
    }
    total = copiesN - 5;

     cout << "Total number of books needed are "<< total << endl;

    return total;
}
int bookCost(int cost, float total)
{
    float price;
    cout << cost<<endl<< total<< endl;
    price= cost * total;
    cout << "The total price of all books is $"<< price<< endl;
    return price;

}
double profit(float price)
{
    float tProfit;
    tProfit = price * .2;
    cout << "The total profit is $ "<< tProfit<< endl;
    return tProfit;
}

The local variables price and total in main() are never set. In particular, the parameter total in function books() is a different variable. So is the parameter total in bookCost()
Last edited on
Thank you!!!! It works perfectly now!
Topic archived. No new replies allowed.