Help with Classes return value

This is a homework assignment. I am stuck on this one problem and I can't figure it out. I need the program to output the revenue of the items sold. When there are no more items it should display a warning message. The problem I have is that I cannot get the revenue to display. No matter what I have tried I get a weird number for the revenue. Any help would be appreciated. The top code is what the teacher wrote and the bottom code is what I wrote. You need both to run the problem.

Teachers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Product.cpp"
#include <iostream>
using namespace std;

int main()
{
    Product p("Movie Ticket", 50, 9.99);

    p.makeSale();
    cout << p.toString() << endl;

    p.makeSale(23);
    cout << p.toString() << endl;

    p.makeSale(28);
    cout << p.toString() << endl;
}


Mine - The part I need help with the revenue (line 36) and the subtraction is off by 1(line 67).
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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class Product
{
    int units;
    int sale;
    float price;
    float revenue;
    string name;


    public:

        int rev(float revenue, float p, int u);
        void makeSale();
        void makeSale(int sale);

        Product()
        {
            units = 0;
            price = 0;
            revenue = 0;
        }
        Product(string n, int u, float p)
        {
            units = u;
            price = p;
            name = n;
        }
        string toString();
};

int rev(float revenue, float p, int u)
{
    revenue = u * p;
    return revenue;
}

string Product::toString()
{
    if(units <= 0)
    {
        stringstream ss;
        ss << "Warning: Sold out of " << name << "!";
        string result = ss.str();
        return result;
    }
    else
    {
        stringstream ss;
        ss << "Product name: " << name << " Units Left: " << units << " Price: " << price << " Revenue: $" << revenue;
        string result1 = ss.str();
        return result1;
    }
}

void Product::makeSale()
{

}

void Product::makeSale(int sale)
{
    units -=sale;
}
1
2
3
4
5
int Product::rev(float revenue, float p, int u)
{
    this->revenue = u * p;
    return this->revenue;
}
You never call that method, thought.

#include "Product.cpp" Don't do that. You should include the header, and link the source.
Thanks for your help. I changed the code but I get the same output as before.
The first part of the code is written by the teacher. I submit my code online and it runs the int main() to get the results. I just have to write the classes. I can't modify that part of the code. Here is my part of the code now

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
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class Product
{
    int units;
    int sale;
    float price;
    float revenue;
    string name;


    public:

        int rev(float p, int u);
        void makeSale();
        void makeSale(int u);

        Product()
        {
            units = 0;
            price = 0;
            revenue = 0;
        }
        Product(string n, int u, float p)
        {
            units = u;
            price = p;
            name = n;
        }
        string toString();
};

int Product::rev(float p, int u)
{
    this->revenue = u * p;
    return this->revenue;
}

string Product::toString()
{
    if(units < 0)
    {
        units = 0;
        stringstream ss;
        ss << "Warning: Sold out of " << name << "!" << endl;
        ss << "Warning: Sold out of " << name << "!" << endl;
        ss << "Product name: " << name << " Units Left: " << units << " Price: " << price << " Revenue: $" << revenue;
        string result = ss.str();
        return result;
    }
    else if(units > 0)
    {
        stringstream ss;
        ss << "Product name: " << name << " Units Left: " << units << " Price: " << price << " Revenue: $" << revenue;
        string result1 = ss.str();
        return result1;
    }
}

void Product::makeSale()
{

}

void Product::makeSale(int u)
{
    units = units - u;
}
Topic archived. No new replies allowed.