[try Beta version]
Not logged in

 
Could I have used any pointers to better this program?

Mar 29, 2013 at 7:50pm
If anyone cares to look over it, this is the program: (Well only the header and the class functions

Header File
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
#ifndef FISH_H
#define FISH_H
#include <iostream>
#include <string>
using namespace std;

struct Fish
{
	string fishType;
	double fishLen;
	double fishWeight;
	string actions;
};

class Fishing
{
	private:
		Fish fish;
	public:
		//Constructors
		Fishing();
		Fishing(string,double,double);
		//Mutator Functions
		void setFishType(string t)
		{ fish.fishType=t; }
		void setFishLen(double);
		void setFishWeight(double);
		void setActions(string a)
		{ fish.actions=a; }
		//Accessor Functions
		string getFishType()
		{ return fish.fishType; }
		double getFishLen()
		{ return fish.fishLen; }
		double getFishWeight()
		{ return fish.fishWeight; }
		string getActions()
		{ return fish.actions; }
		//Other
		void getWhatToDo();
		void display();

};

#endif 


Functions:
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 "Fish.h"

Fishing::Fishing()
{
	fish.fishType = fish.actions = "Default";
	fish.fishLen = fish.fishWeight = 0;
}

Fishing::Fishing(string type,double len, double wid)
{
	fish.fishType=type;
	fish.fishLen=len;
	fish.fishWeight=wid;
}

void Fishing::setFishLen(double l)
{	
	if(l>=0)
		fish.fishLen=l; 
	else
		fish.fishLen=1;
}

void Fishing::setFishWeight(double w)
{ 
	if(w>=0)
		fish.fishWeight=w; 
	else
		fish.fishWeight=1;
}

void Fishing::getWhatToDo()
{
	string type=fish.fishType;
	double len=fish.fishLen,
		   weight=fish.fishWeight;

	if(weight>=2)
	{
		if(type=="Sheepshead")
		{
			if(len<15)
				fish.actions="Throw Back";
			else if(len>=15 && len <=21)
				fish.actions="Keep";
			else
				fish.actions="Tag";
		}
		else if(type=="Red Drum")
		{
			if(len<20)
				fish.actions="Throw Back";
			else if(len>=20 && len <=28)
				fish.actions="Keep";
			else
				fish.actions="Tag";
		}
		else
		{
			if(len<16)
				fish.actions="Throw Back";
			else
				fish.actions="Keep";
		}
	}
	else
		fish.actions="Throw Back";
}

void Fishing::display()
{
	cout<<getFishType()<<", ";
	cout<<getFishLen()<<" inches, ";
	cout<<getFishWeight()<<" lbs, ";
	cout<<getActions()<<endl;
}


The thing is, we are on the section about pointers, but am I forced to use pointers here? I am just confused as to the whole variable/pointer usage, the teacher said we would now always have to use pointers for everything, but what if they are not needed?
Mar 29, 2013 at 8:00pm
thats nice and simple readable code, I dont see where you need pointers, your teachers teaching you nicely.

Whats your code actually do to need pointers though?
Mar 29, 2013 at 8:08pm
Well we have to ask the user to enter the number of fish he wants to enter data for, that is when we have to use a pointer to make a dynamic array. The only reason I am confused is because of what he said, that we should now get used to using many pointers instead of regular variables.

I would enter the rest of the program here but it would be too long I think.
Mar 29, 2013 at 8:14pm
The only reason I am confused is because of what he said, that we should now get used to using many pointers instead of regular variables.


Relax. Pointers are a relic. In modern C++ (the 2011 standard, C++11, which you probably won't learn much about), you are encouraged to use smart pointers such as std::unique_ptr for dynamic memory allocation.

And where you can, you should use references instead of pointers.

And also, any self-respecting C++ programmer will use std::string instead of character arrays char[] and character pointers char*.
Mar 29, 2013 at 8:20pm
Fair enough, thanks for the confirmation
Topic archived. No new replies allowed.