why sum is 0

hey guys
im gonna calculate but i dont know why sum is 0,

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
#include <iostream>
#include <conio.h>
using namespace std;

struct calc{
	
	int num;

};

calc data[20];
int counter=0;

void getnumber();

void multiply();

int main(){
	
	int op;
	
	while(true){
		
		system("cls");
		
		cout<<"1- input 2- multiply \n";
		cin>>op;
		
		switch(op){
			
			case 1:getnumber();
			break;
			
			case 2:multiply();
			break;
			
		}
		
	}
	
	return 0;
}

void getnumber(){
	
	cout<<"enter number: ";
	cin>>data[counter].num;
	counter++;
}

void multiply(){
	
	int sum;
	
	for(int i=0;i<counter;i++){
		
		sum*=data[i].num;
	}	
	
	cout<<"sum is: "<<sum;
	getch();
}
Because your variable sum is uninitialized, meaning it's value is garbage.

So when you do this sum*=data[i].num; which is short for sum = sum * data[i].num; It's like doing garbage equals to garbage times a number, which for obvious reasons doesnt work.

You can't initialize it to 0 because then everything will be 0 because anything times 0 is 0. So initialize it to 1.

int sum = 1;
Last edited on
data[counter].num is different than counter.
In your multiply()'s for statement, counter is ==0, so it doesn't need to run.
Same as in the getnumber() function, you do counter++. If the point of that is to get to the next array element, then it's correct, but otherwise, nope.
Last edited on
wow
can i say i love you?
i had this problem for long time and you solved for me
really appreciate
No problem my friend. If you need more help, just post.
You're very welcome!

@Zarman.
I'm pretty sure he was talking to me :p

data[counter].num is different than counter.


I'm not sure what you mean by this, it makes very little sense. His counter variable is global, and he increments it in the getnumber function.

A few tips for you @alitt.

Don't use Global variables.
And use std:: instead of using namespace std;

for example:

1
2
3
int x;
std::cout << "Hello World!" << std::endl;
std::cin >> x;


Give this a google search and read about it :)
Last edited on
@TarikNeaj the variable "i" is for the array element. It has to be 0 or else it will never access the first element of the array.
If you look at the for-loop it is 0. "i" starts at 0, what are you talking about? I ran his program with the fixes I suggested and it worked perfectly fine.
thank you so muchhhhhhhh
Oh sorry, yeah I see the sum thing. I though you were speaking about the variable "i"
hey guys
one more problem
it works for add and multiply and it has some problem for minus and divide
i dont know how should i initialize sum for minus and divide

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
#include <iostream>
#include <conio.h>
using namespace std;

struct calc{
	
	int num;

};

calc data[20];
int counter=0;

void getnumber();

void Add();
void Multiply();
void Divide();
void min();

int main(){
	
	int op;
	
	while(true){
		
		system("cls");
		
		cout<<"1- input 2- Add 3- Multiply 4- Divide 5-Minus \n";
		cin>>op;
		
		switch(op){
			
			case 1:getnumber();
			break;
			
			case 2:Add();
			break;
			
			case 3:Multiply();
			break;
			
			case 4:Divide();
			break;
			
			case 5:min();
			break;
			
		}
		
	}
	
	return 0;
}

void getnumber(){
	
	cout<<"enter number: ";
	cin>>data[counter].num;
	counter++;
}

void Add(){
	
	int sum;
	
	for(int i=0;i<counter;i++){
		
		sum+=data[i].num;
	}	
	
	cout<<"sum is: "<<sum;
	getch();
}

void Multiply(){
	
	int sum=1;
	
	for(int i=0;i<counter;i++){
		
		sum*=data[i].num;
	}	
	
	cout<<"sum is: "<<sum;
	getch();	
}

void Divide(){
	
	float sum;
	
	for(int i=0;i<counter;i++){
		
		sum/=data[i].num;
	}	
	
	cout<<"sum is: "<<sum;
	getch();
}

void min(){
	
	int sum;
	
	for(int i=0;i<counter;i++){
		
		sum-=data[i].num;
	}	
	
	cout<<"sum is: "<<sum;
	getch();
}


it works for add

No. It does not. It merely appears to work. You do use uninitialized variable. By sad coincidence the result just happes to be what you do expect.

What do you expect as result from the three functions?

Let say the input is {2, 5}. The three calculations are effectively:
X + 2 + 5
(Y / 2) / 5
Z - 2 - 5

You have to initialize X, Y, and Z so that the value of those three equations becomes what you expect, just like you chose that
W * 2 * 5

has to equal 10 and thus W has to be 1.


PS. I have 42 values to input. How will your program handle that?
Last edited on
For add and minus, they should be initialized to 0. You should never use a variable that is not initialized.

When it comes to divide it is a bit more complex, and I think that is because of the actual design of this assignment. It needs some work. But you can solve the problem by doing this -

1
2
3
4
5
6
7
8
9
10
11
12
void Divide(){
	
	float sum = (float)data[0].num; //  initialize sum to the first number inside the array
	
	for(int i=0;i<counter - 1;i++){ // run through the array but skip the first value (the first value is inside of sum)
		
		sum/=data[i+1].num; // do the division starting from the second value in the array.
	}	
	
	cout<<"sum is: "<<sum;
        getch();
}
Last edited on
really really thanks again
Topic archived. No new replies allowed.