Switch function

Pardon the long code, it is just there to give the question context. The question is why the switch function isn't running the functions in the class??



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Purpose
// A program to demonstrate the application of a simple digital filter
// 
// Overview
// A sequence of data items and digital filter values need to be entered by the
// user. The application of the filter to the data involves a simple convolution 
// operation. The filtered data are stored separately. 
//
// Example
//  before filtering: 
//   data_in = [0 1 3 6 3 1 0]
//   filter = [-0.5 1 -0.5]
//  after filtering:
//   data_out = [-0.5 -0.5 3 -0.5 -0.5]
//  where
//   data_out[0]=data_in[0]*filter[0]+data_in[1]*filter[1]+data_in[2]*filter[2]
//   data_out[1]=data_in[1]*filter[0]+data_in[2]*filter[1]+data_in[3]*filter[2]
//   data_out[2]=data_in[2]*filter[0]+data_in[3]*filter[1]+data_in[4]*filter[2]
//   data_out[3]=data_in[3]*filter[0]+data_in[4]*filter[1]+data_in[5]*filter[2]
//   data_out[4]=data_in[4]*filter[0]+data_in[5]*filter[1]+data_in[6]*filter[2]
//
// The program checks the following
// 1. The data and filter values must have been entered before the filter is 
//    applied
// 2. The filter is not applied if the number of filter values is greater than
//    the number of input data values
// 3. The data and filter values must have been entered and the filter applied 
//    before the filtered data can be displayed

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

#define FILTER_FILENAME "filter.txt"
#define FILTER_MAX 100

class TheFilter {
public:
	TheFilter();
	~TheFilter();
	void EnterFilter();
	void ShowFilter();
	void Valid();
	void LoadFilter();
	void SaveFilter() const;
private:
	double LengthFilter;
	int ValueFilter;
};

TheFilter::TheFilter() {
	ValueFilter = 0;
	LengthFilter = 0;
}

TheFilter::~TheFilter() {
	cout << "Filter deleted" << endl;
}

void TheFilter::EnterFilter() {
	cout << "Please enter the number of values in the filter" <<endl;
	cout << "-----------------------------------------------" <<endl;
	cin >> LengthFilter;
	cout << "Please enter the values of the filter" <<endl;
	cout << "-------------------------------------" <<endl;
	cin >> ValueFilter;
	cout << "Filter entered" <<endl;
}

void TheFilter::ShowFilter() {
	cout << "The filter is" << ValueFilter <<endl;
}

void TheFilter::LoadFilter() {
//	fstream input_file;
//	input_file.open(FILTER_FILENAME, ios::in);
//	if (input_file.good()) {
//	input_file >>ValueFilter;
//	for (unsigned long i=0; i<ValueFilter; i++) {
//		input_file >>ValueFilter[i];
//	}
//	}
//	input_file.close();
//	cout <<"Filter loaded" <<endl;
}

void TheFilter::SaveFilter() const {
//	fstream output_file;
//	output_file.open(FILTER_FILENAME, ios::out)
//		if (output_file.good()) {
//		output_file <<ValueFilter <<endl <<endl;
//		for (unsigned long i=0; i<ValueFilter; i++) {
//			output_file <<  <<endl;
//		}
//	}
//	output_file.close();
//	cout <<"Filter saved" <<endl;
}

class TheData {
public:
	TheData();
	~TheData();
	void EnterData();
	void ShowData();
	void Valid();
	void LoadData();
	void SaveData();
private:
	unsigned long LengthData;
	char ValueData;
};

TheData::TheData() {
	LengthData = 0;
	ValueData = 0;
}

TheData::~TheData() {
	cout << "Data deleted" << endl;
}

void TheData::EnterData() {
	cout << "Please enter the number of data values" <<endl;
	cout << "--------------------------------------" <<endl;
	cin >> LengthData;
	cout << "Please enter the values of the data" <<endl;
	cout << "-----------------------------------" <<endl;
	cin >> ValueData;
	cout << "Data entered" <<endl;
}

void TheData::ShowData() {
	cout << "The data is" << ValueData <<endl;
}

void TheData::LoadData () {

}

void TheData::SaveData() {

}

class ApplyFilter {
public:
	ApplyFilter();
	~ApplyFilter();
	void FilterData();
	void ShowFinal();
	void SaveFinal();
private:
	unsigned long FinishedData;
};

ApplyFilter::ApplyFilter() {
	FinishedData = 0;
}

ApplyFilter::~ApplyFilter() {
	cout << "Filtered data deleted" << endl;
}

void ApplyFilter::FilterData() {

}

void ApplyFilter::ShowFinal() {
	cout << "The results are " << FinishedData <<endl;
}

void ApplyFilter::SaveFinal() {

}
 
int main() {

	int UserInput;
	std::string SavedFilter;
	std::string SavedData;
	std::string SavedFinal;
	TheData Data;
	TheFilter Filter;
	ApplyFilter Apply;
	fstream FilePointer; 

	while (1) {
		
		cout << "Welcome to this filtering programme, please select your option;" <<endl;
		cout << "---------------------------------------------------------------" <<endl;
		cout << "1. Enter new filter values" <<endl;
		cout <<	"2. Load existing filter values" <<endl;
		cout << "3. Show filter" <<endl;
		cout << endl;
		cout << "4. Enter new data values" <<endl;
		cout << "5. Load existing filter values" <<endl;
		cout << "6. Show data" <<endl;
		cout << endl;
		cout << "7. Apply filter" <<endl;
		cout << "8. Display filtered data" <<endl;
		cout << endl;
		cout << "9. Exit programme" <<endl;
		cout << "-----------------" <<endl;
		cout << endl;
		cout << "Please enter your option: " <<endl;

		cin >> UserInput;
		cout << endl;

		switch (UserInput) {
		case '1':
			Filter.EnterFilter();
			cout << "Would you like to save the new filter? [Y] [N]" <<endl;
			cin >> SavedFilter;

			if (SavedFilter == "Y") {
				Filter.SaveFilter();
			}
			break;

		case '2':
			Filter.LoadFilter();
			break;

		case '3':
			Filter.ShowFilter();
			break;

		case '4':
			Data.EnterData();
			cout << "Would you like to save the new data values? [Y] [N]" <<endl;
			cin >> SavedData;

			if (SavedData == "Y") {
				Data.SaveData();
			}
			break;

		case '5':
			Data.LoadData();
			break;

		case '6':
			Data.ShowData();
			break;

		case '7':
			Apply.FilterData();
			break;

		case '8':
			Apply.ShowFinal();
			cout << "Would you like to save the filtered data? [Y] [N]" <<endl;
			cin >> SavedFinal;

			if (SavedFinal == "Y") {
				Apply.SaveFinal();
			}
			break;

		case '9':
			return 0;

		default:
			cout << "Please select a valid option" << endl;
		}
	}
	
}
If you input 49 (ASCII code for 1) it will probably run case '1'.

The problem is that UserInput is an int while '1', '2', '3', etc. are characters.

To fix this problem you should use integer literals instead of character literals in the switch.

1
2
3
4
5
// wrong
case '1':

// right
case 1:
Last edited on
Perfect, thanks :)
Topic archived. No new replies allowed.