Set a value by default constructor or other constructor

Hello,

I'm trying to learn c++ but there is something I can't figure out. How do I call this class in the first if statement to change the overflowValue?

Here is 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Counter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

class counterClass {
	
private:
    int counterCents;
    int counterDimes;
    int counterDollars;
    int counterTens;
	int totalCount;
	int overflowValue;

public:
    void incr1( int);
    void incr10( int);
    void incr100( int);
    void incr1000( int);
    void showCounter();
	void reset();
	void setTotalCount();

    bool overflow();

	counterClass(){
		overflowValue = 9999;
		counterCents = 0;
		counterDimes = 0;
		counterDollars = 0;
		counterTens = 0;
		totalCount = 0;
	}
	counterClass(int overflowVal){
		overflowValue = overflowVal; 
		counterCents = 0;
		counterDimes = 0;
		counterDollars = 0;
		counterTens = 0;
		totalCount = 0;
	}
};

void main()
{
    counterClass clicker;
    char letter; 
	int digit;
    bool booleanFlag;

	char changeOverflow;
	int overflowVal;
	
	for(int i = 0; i < 1; i++)
	{
		cout << "Do you want to use 9999 as value for overflow or change this? (Y/N)" << endl;
		cin >> changeOverflow;
		if (changeOverflow == 'y' || changeOverflow == 'Y')
		{
			cout << "Please insert your overflow limit" << endl;
			cin >> overflowVal;
			cout << "Now insert values to counter" << endl;
			counterClass::counterClass(overflowVal);

		}
		else if (changeOverflow == 'n' || changeOverflow == 'N')
		{

			break;
		}
		else
		{
		i--;
		cout << "Invalid value, please try again" << endl;
		}
	}
    cout << "(a)cents\n(s)dimes\n(d)dollars\n(f)tens\n(o)overflow\n";
    cin >> letter;

    while(letter != 'e' || letter != 'E') 
    {
        cout << "Enter digit 1-9 \n";
        cin >> digit;
        switch(letter)
        {
            case 'a':
                clicker.incr1(digit);
                break;
            case 's':
                clicker.incr10(digit);
                break;
            case 'd':
                clicker.incr100(digit);
                break;
            case 'f':
                clicker.incr1000(digit);
                break;
            case 'o':
                clicker.overflow();
                break;
        }
 
 
        cout << "(a)cents\n(s)dimes\n(d)dollars\n(f)tens\n(o)overflow\n";
        cin >> letter;
		if (letter == 'o' || letter == 'O')
			clicker.overflow();
    }
}

void counterClass::incr1(int digit)
{
    counterCents += digit;
    if(counterCents > 9)
    {
        counterCents = counterCents - 9;
        counterDimes += 1;
    }
		setTotalCount();
}
void counterClass::incr10(int digit)
{
    counterDimes += digit;
    if(counterDimes > 9)
    {
        counterDimes = counterDimes -9;
        counterDollars +=1 ;
    }
		setTotalCount();
}
void counterClass::incr100(int digit)
{
    counterDollars += digit;
    if(counterDollars > 9)
    {
        counterDollars = counterDollars - 9;
        counterTens += 1;
    }
		setTotalCount();
}
void counterClass::incr1000(int digit)
{
    counterTens += digit;
	setTotalCount();
 
}
bool counterClass::overflow()
{
    cout << "The count will be reset because of overflow.\n";
    reset();
	return(0);
}

void counterClass::reset() 
{
counterCents = 0;
counterDimes = 0;
counterDollars = 0;
counterTens = 0;
}

void counterClass::setTotalCount()
{
	int totCents = counterCents;
	int totDimes = counterDimes * 10;
	int totDollars = counterDollars * 100;
	int totTens = counterTens * 1000;

	totalCount = totTens + totDollars + totDimes + counterCents;

	if (overflowValue > totalCount){
		cout << "The total count is: " << totalCount << endl;
		cout << "The overflow values is: " << overflowValue << endl;
	}
	else {
		overflow();
	}
}


Thanks in advance!
closed account (Lv0f92yv)
You could create a mutator (member function that changes a value) and change it from inside the class....

So:
1
2
3
4
void counterClass::changeOverFlowValue( int newVal )
{
   overflowValue = newVal;
}


then call it on the class;

myInstanceclicker.changeOverFlowValue( newval );

Currently, counterClass::counterClass(overflowVal); doesn't do anything except instantiate a new class object and not assign anything with which to reference it...

Last edited on
Thanks for responding, I just saw that the assignment requires me to have two constructors, how would I do to make sure that I have two of them where on is the default and the other one changes the value of the overflow value?
closed account (Lv0f92yv)
Overload your constructor.
Same function signature, but a change in at least one of the parameters will overload that function.

In the overloaded constructor, take an argument that will be the overflow value and assign yours accordingly.
Topic archived. No new replies allowed.