I Have No Clue What I'm Doing

Fair Warning, this question is a beginner crap fest.
My code is completely nonfunctional due to my general lack of knowledge.

Please just skip if this seems too tedious.

So I am in a second semester c++ class, but I don't know c++, like any of it, at all... The reason for this is that my school apparently doesn't have second semester classes for other languages, but requires a second semester to be take regardless of what language you took the first semester.
So with that in mind, and the fact that i'm already bad at OOP:

I am trying to make a program with an object of a swimming pool, with basic dimensions for the pool as well as some basic function, like figure out how much time it would take to fill the pool.

I had it all running in one file, but I have to separate into multiple, and when I tried to do that, well... it stopped working.

If someone could just point out the biggest mistakes I'm making I would appreciate it.

main.cpp
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
  #include <iostream>

#include "swimmingPoolImp.cpp"

// driver function
int main()
{
    swimmingPool sp(40, 30, 5, 1000, 20, 40);

    std::cout << "Total Volume of the pool: " << sp.getLength() * sp.getWidth() * sp.getDepth() << "\n\n";

    std::cout << "Current Volume of water in pool: " << sp.getVolume() << " cubic feet\n\n";
    std::cout << "Volume needed to fill the pool: " << sp.waterNeeded() << " cubic feet\n\n";

    double timeToFill = sp.timeToFill();
    std::cout << "Time needed to fill the pool: " << timeToFill << " minutes\n\n";
    std::cout << "Filling the pool for " << timeToFill << " minutes...\n\n";
    sp.addWater(timeToFill);

    double timeToDrain = sp.timeToEmpty();
    std::cout << "Time needed to drain the pool: " << timeToDrain << " minutes\n\n";
    std::cout << "Draining the pool for " << timeToDrain << " minutes...\n\n";
    sp.drainWater(timeToDrain);

    std::cout << "Current Volume of water in pool: " << sp.getVolume() << " cubic feet\n\n";

    return 0;
}


swimmingPool.h
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
// SwimmingPool class
class swimmingPool
{
private:
    double length;
    double width;
    double depth;
    double currVolume;
    double inFlowRate;
    double outFlowRate;

public:
    // parameterized constructor with default values
    swimmingPool(double _length = 1, double _width = 1, double _depth = 1, double _volume = 1, double _inFlowRate = 1, double _outFlowRate = 1)
    {
        length = (_length > 0) ? _length : 1;
        width = (_width > 0) ? _width : 1;
        depth = (_depth > 0) ? _depth : 1;
        currVolume = (_volume > 0) ? _volume : 1;
        inFlowRate = (_inFlowRate > 0) ? _inFlowRate : 1;
        outFlowRate = (_outFlowRate > 0) ? _outFlowRate : 1;
    }

    // accessors
    void setLength(double _length) { length = _length; }
    void setWidth(double _width) { width = _width; }
    void setDepth(double _depth) { depth = _depth; }
    void setVolume(double _volume) { currVolume = _volume; }
    void setInFlowRate(double _inFlowRate) { inFlowRate = _inFlowRate; }
    void setOutFlowRate(double _outFlowRate) { outFlowRate = _outFlowRate; }

    // mutators
    double getLength() { return length; }
    double getWidth() { return width; }
    double getDepth() { return depth; }
    double getVolume() { return currVolume; }
    double getInFlowRate() { return inFlowRate; }
    double getOutFlowRate() { return outFlowRate; }

    double waterNeeded();
    double timeToFill();
    double timeToEmpty();
    void addWater(){ ;}
    double drainWater;
};


swimmingPoolImp.cpp
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
#include <iostream>
#include "swimmingPool.h"
using namespace std;

// swimmingPool::swimmingPool(double l,double w,double d,double cV,double iFR,double oFR);

    // function to determine the amount of water needed to fill a pool
    double swimmingPool::waterNeeded()
    {
        // find the volume of water that is unfilled in the swimming pool
        // this will be the amount of water needed to fill the pool
        double unfilledVolume = (length * width * depth) - currVolume;
        return unfilledVolume;
    }

    // function to determine the time needed to fill the pool
    double swimmingPool::timeToFill()
    {
        double hoursToFill = waterNeeded() / inFlowRate;
        return hoursToFill;
    }

    // function to determine the time needed to empty the pool
    double swimmingPool::timeToEmpty()
    {
        double hoursToEmpty = currVolume / outFlowRate;
        return hoursToEmpty;
    }

    // function to add water for a specific time
    void swimmingPool::addWater(double timeInMin)
    {
        double amountToAdd = timeInMin * inFlowRate;
        currVolume += amountToAdd;
    }

    // function to drain water for a specific time
    void swimmingPool::drainWater(double timeInMin)
    {
        double amountToDrain = timeInMin * outFlowRate;
        currVolume -= amountToDrain;
    }
Line 43 and 44 in swimmingpool.h are wrong. You wrote
1
2
void addWater(){ ;}
double drainWater;

Which doesn't match the definitions on lines 31 and 38 in swimmingPoolImp.cpp.
You should have written
1
2
void addWater(double timeInMin); 
void drainWater(double timeInMin);

Many programming languages are similar in the sense that you can apply ideas from one language to another, especially within C-derived languages. So you're not so far behind as you think, assuming you did write the rest of the program.
Last edited on
Thank you very much for your help. Hopefully I'm not as far behind as I feel.
Thanks to the answer from mbozzi, most of the serious errors have stopped, but now I am getting:
main.cpp swimmingPool.h swimmingPoolImp.cpp
/tmp/ccfY4QN2.o: In function `swimmingPool::waterNeeded()':
swimmingPoolImp.cpp:(.text+0x0): multiple definition of `swimmingPool::waterNeeded()'
/tmp/ccD7ITbL.o:main.cpp:(.text+0x0): first defined here
/tmp/ccfY4QN2.o:

As far as I understand, this means that I have repeats of my definition, likely cause by improper usage of includes, but I'm not quite sure where or how this is happening. If anyone could better explain to me how this works, I would appreciate it.
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
#include <iostream>

#include "swimmingPool.h" // <--

// driver function
int main()
{
    swimmingPool sp(40, 30, 5, 1000, 20, 40);

    std::cout << "Total Volume of the pool: " << sp.getLength() * sp.getWidth() * sp.getDepth() << "\n\n";

    std::cout << "Current Volume of water in pool: " << sp.getVolume() << " cubic feet\n\n";
    std::cout << "Volume needed to fill the pool: " << sp.waterNeeded() << " cubic feet\n\n";

    double timeToFill = sp.timeToFill();
    std::cout << "Time needed to fill the pool: " << timeToFill << " minutes\n\n";
    std::cout << "Filling the pool for " << timeToFill << " minutes...\n\n";
    sp.addWater(timeToFill);

    double timeToDrain = sp.timeToEmpty();
    std::cout << "Time needed to drain the pool: " << timeToDrain << " minutes\n\n";
    std::cout << "Draining the pool for " << timeToDrain << " minutes...\n\n";
    sp.drainWater(timeToDrain);

    std::cout << "Current Volume of water in pool: " << sp.getVolume() << " cubic feet\n\n";

    return 0;
}
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
// SwimmingPool class
class swimmingPool
{
private:
    double length;
    double width;
    double depth;
    double currVolume;
    double inFlowRate;
    double outFlowRate;

public:
    // parameterized constructor with default values
    swimmingPool(double _length = 1, double _width = 1, double _depth = 1, double _volume = 1, double _inFlowRate = 1, double _outFlowRate = 1)
    {
        length = (_length > 0) ? _length : 1;
        width = (_width > 0) ? _width : 1;
        depth = (_depth > 0) ? _depth : 1;
        currVolume = (_volume > 0) ? _volume : 1;
        inFlowRate = (_inFlowRate > 0) ? _inFlowRate : 1;
        outFlowRate = (_outFlowRate > 0) ? _outFlowRate : 1;
    }

    // accessors
    void setLength(double _length) { length = _length; }
    void setWidth(double _width) { width = _width; }
    void setDepth(double _depth) { depth = _depth; }
    void setVolume(double _volume) { currVolume = _volume; }
    void setInFlowRate(double _inFlowRate) { inFlowRate = _inFlowRate; }
    void setOutFlowRate(double _outFlowRate) { outFlowRate = _outFlowRate; }

    // mutators
    double getLength() { return length; }
    double getWidth() { return width; }
    double getDepth() { return depth; }
    double getVolume() { return currVolume; }
    double getInFlowRate() { return inFlowRate; }
    double getOutFlowRate() { return outFlowRate; }

    double waterNeeded();
    double timeToFill();
    double timeToEmpty();
    void addWater(double); // <--
    void drainWater(double); // <--
};
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
#include <iostream>
#include "swimmingPool.h"
using namespace std;

// swimmingPool::swimmingPool(double l,double w,double d,double cV,double iFR,double oFR);

    // function to determine the amount of water needed to fill a pool
    double swimmingPool::waterNeeded()
    {
        // find the volume of water that is unfilled in the swimming pool
        // this will be the amount of water needed to fill the pool
        double unfilledVolume = (length * width * depth) - currVolume;
        return unfilledVolume;
    }

    // function to determine the time needed to fill the pool
    double swimmingPool::timeToFill()
    {
        double hoursToFill = waterNeeded() / inFlowRate;
        return hoursToFill;
    }

    // function to determine the time needed to empty the pool
    double swimmingPool::timeToEmpty()
    {
        double hoursToEmpty = currVolume / outFlowRate;
        return hoursToEmpty;
    }

    // function to add water for a specific time
    void swimmingPool::addWater(double timeInMin)
    {
        double amountToAdd = timeInMin * inFlowRate;
        currVolume += amountToAdd;
    }

    // function to drain water for a specific time
    void swimmingPool::drainWater(double timeInMin) // <--
    {
        double amountToDrain = timeInMin * outFlowRate;
        currVolume -= amountToDrain;
    }
Total Volume of the pool: 6000

Current Volume of water in pool: 1000 cubic feet

Volume needed to fill the pool: 5000 cubic feet

Time needed to fill the pool: 250 minutes

Filling the pool for 250 minutes...

Time needed to drain the pool: 150 minutes

Draining the pool for 150 minutes...

Current Volume of water in pool: 0 cubic feet

Program ended with exit code: 0


Also, for this to be easily successful the 3 files need to be in the same directory so the class #includes don't produce file path issues.

As a suggestion in terms of the data structure it would simplify matters if the current and maximum depths of water in the pool were recorded rather than volumes. i.e Once the swimmingPool 'knows' the depth the relevant volume is determined by a method.
Last edited on
Thanks, I guess I should read up on how include works. I really appreciate everyones help btw.
Topic archived. No new replies allowed.