switch in c++

I have a class which takes three arguments, number of row, column and value.
I need to modify the vale during runtime and I'm using switch for that. but I get this error "uninitialized local variable 'item' used", when I initialize item, the switch just work on one case. I need to change the item and see the different. any help would be appreciated.
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
template <class T>
LSM<T>::LSM(int nRows, int nCols, T *inputData)
{
	m_nRows = nRows;
	m_nCols = nCols;
	m_nElements = m_nRows * m_nCols;
	m_matrixData = new T[m_nElements];
	int item;
	switch (item) {

	case 1:
		for (int i = 0; i < m_nRows; i++) {
		
			for (int j = 0; j < m_nCols; j++) {
		
				if (i == j) {
		
					m_matrixData[i*j] = (1 / 6)*inputData[i*j];
				}
				else
				{
					m_matrixData[i*j] = (1 / 12)*inputData[i*j];
				}
			}
		
		}

		break;
	case 2:
		for (int i = 0; i < m_nRows; i++) {
		
			for (int j = 0; j < m_nCols; j++) {
		
				m_matrixData[i*j] = (1 / 3)*inputData[i*j];
			}
		}
	
		break;
	}

}
Last edited on
1
2
int item;
	switch (item) {


error "uninitialized local variable 'item' used"


correct. item is defined but not initialised - so it's value is unknown. You're then using this unknown value for the switch...

What is item supposed to represent - and how is it supposed to be initialised/set to a value?
Last edited on
We are supposed to have two 3*3 matrices The elements of these two matrices are constant, but they have the different coefficient. In the class I defined the coefficients for each case, then in the main code I specify the case number and I want to have these matrices, but I don't know where is my mistake?
Last edited on
you do be multiplying everything by zero everywhere. consider (1.0/12)*
and consider
num *= 1.0/12
instead of
num = 1.0/12*num
Last edited on
Yes, you're right,thanks
Well you can make 'item' a class member and set it as part of the class constructor...
Thanks
Topic archived. No new replies allowed.