A counter program using classes. I'm really desperate.

I've been sitting her for nearly four hours trying to figure this out... Please help, I'm on the verge of tears here.

Alright, so here's the problem prompt that I was given in the class, just for clarity's sake:

Define a class for a type called CounterType. An object of this type is used to count things, so it records a count that is a nonnegative whole number. Include a mutator function that sets the counter to a count given as an argument. Include member functions to increase the count by one and to decrease the count by one. Be sure that no member function allows the value of the counter to become negative. Also, include a member function that returns the current count value and one that outputs the count. Embed your class definition in a test program.

The wording was a bit confusing to me, but I've given it my best shot. It basically shows the user a number, in this case I have it set to 5, and asks them if they want to add 1 or subtract 1 from it. Then ask them if they want to continue.

Some things to note is that the code does run. However, it doesn't display the number '5', it just displays some random junk numbers. No matter if I select to add or subtract it will always add 1, and it only ever shows 6, even if I add or subtract multiple times... I feel like I'm on the right track, but there's something wrong...

here's my code:

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
90
91
92
93
94
95
96
97
#include <iostream>
using namespace std;

class Countertype
{
    private:
		int count;
		int countup;
		int countdown;
		int currentcount;
		int outputcount;
	public: 

		//setters below

		void setcount (int _count)
		{
			count = _count;
		}
		void setcountup (int _countup)
		{
			countup = _countup;
		}
		void setcountdown (int _countdown)
		{
			countdown = _countdown;
		}
		void setcurrentcount (int count)
		{
			currentcount = count;
		}
		void setoutputcount (int _outputcount)
		{
			outputcount = _outputcount;
		}

		// getters below

		int getcount ()
		{
			return count;
		}
		void getcountup ()
		{
			countup = count + 1;
			count = count + 1;
		}
		void getcountdown ()
		{
			if (count <= 0)
			{
				cout << "This program won't count negative numbers." << endl;
			}
			else
				countdown= count - 1;
				count = count - 1;
		}
		int getcurrentcount ()
		{
			return currentcount; //I think there must be something wrong with this because I get junk numbers whenever I run the code...
		}
		void getoutputcount (Countertype & testing, int& useranswer)
		{
			if (useranswer = 1)
				cout << testing.getcountup () << endl; //here and on the line below it says that no operater << matches these operands.
			else if (useranswer = 2)
				cout << testing.getcountdown () << endl;
			else
				cout << "You must have entered something wrong." << endl;
		}

};

int main ()
{
	Countertype testing;

	int useranswer;
	char keepgoing;
	testing.setcount (5);

	do
	{
	cout << testing.getcurrentcount() << endl;
	cout << "Do you want to add one to the number (type 1) or subtract one from it? (type 2)" << endl;
	cin >> useranswer;

	testing.getoutputcount (testing, useranswer);
	
	cout << "Do you want to keep going? Y/N" << endl;
	cin >> keepgoing; }
	
	while (keepgoing == 'y' || keepgoing == 'Y');

	system ("pause");
	return 0;
}


Any help anyone could provide would be incredible!
Last edited on
Topic archived. No new replies allowed.