calculator with using function and for loop

hi guys
i'm gonna make a calculator and just add 5 numbers,i wanna using for loops for getting 5 number but i have problem

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

int num,i;
void getnumber();
int sum();

int main(){
	
	int m;
	m=sum();

        cout<<m;
	
	return 0;
}

void getnumber(){
	
	for(i=1;i<=5;i++)
	
	cout<<"enter number: ";
	cin>>num;
}

int sum(){
	
	int sumi;
	sumi+=i;
	return(sumi);
	
}


Last edited on
Hey there.
Lets start from main.
According to your program, m is a return value of sum [sumi]
What does sum do? Adds i to it. What is i? Nothing, just has a garbage default value in it.
Remember that i is just used to run through the loop. i is not the value of what you entered!
You did not call getnumber() anywhere so it never touches that. :D
In getmain, think of m as a box in memory.
Suppose I enter
1
2
3
4
5
It first takes 1 and puts it into m. Then it removes 1 and puts 2 into the box etc.

I would suggest learning from a good book :)
Here is a way :
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;
int main() {
cout << "Enter numbers";
int n,sum = 0;
for(int i = 0; i < 5; i++){
cin >> n;
sum+=n;
}
cout << "Thats " << sum << " when added!";
return 0;
}
Last edited on
you are not calling the function getnumber() in the function main() so,this will not work
try this code:


#include <iostream>
using namespace std;


void getnumber();
int main(){

getnumber();
return 0;
}

void getnumber(){ int sumi=0;
int num[5];//declare an array of five numbers
cout<<"enter number: ";

for(int i=0;i<5;i++)//accept five numbers
{cin>>num[i];
sumi=sumi+num[i];} //for addition of all inputed numbers
cout<<sumi;;

}
Last edited on
i have problem

What problem?

A few obvious problems:

line 23: Only this line is within range of the for statement. If you want to include multiple statements, use {}
21
22
23
24
	for (i=1;i<=5;i++)
	{  cout<<"enter number: ";
	    cin>>num;
        }

Note that the above code will overlay num for each iteration of the loop.
Line 19: You never call getnumber().

Line 29: sumi is an uninitialized variable (garbage).

Line 30: You're adding i to sumi. i is the index to your for loop, not a value you want to sum.



thanks ephraimr, yash8087, AbstractionAnon
i understood
really thanks for help
alitt, check a c++ book, such as programming principles and practice using c++
sure
thanks a lot
excuse me guys
i have a new problem
i don't know why answer is wrong

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

#include <iostream>
using namespace std;

int num;
void getnumber();
int sum();


int main(){
	
	getnumber();
	int m;
	m=sum();
	cout<<m;
	
	
	
	return 0;
}

void getnumber(){
	
	for(int i=1;i<=3;i++){
		
		cout<<"enter number: ";
		cin>>num;
	}
	
}

int sum(){
	
	int sumi;
	sumi+=num;
	return(sumi);
	
	
}  
i used array and changed to this but doesn't work

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

#include <iostream>
using namespace std;

int num[5],i;
void getnumber();
int sum();


int main(){
	
	getnumber();
	int m;
	m=sum();
	cout<<m;
	
	
	
	return 0;
}

void getnumber(){
	
	for(i=1;i<=5;i++){
		
		cout<<"enter number: ";
		cin>>num[i];
	}
	
}

int sum(){
	
	int sumi;
	sumi+=num[i];
	return(sumi);
	
}
1
2
3
4
5
6
7
int sum(){
	
	int sumi;
	sumi+=num[i];
	return(sumi);
	
}

should be
1
2
3
4
5
6
7
8
int sum(){
	
	int sumi = 0;
        for(int i =0; i < 5; i ++)
	sumi+=num[i];
	return(sumi);
	
}

because, you have to go through the array elements
and
1
2
3
4
5
6
7
8
9
void getnumber(){
	
	for(i=1;i<=5;i++){
		
		cout<<"enter number: ";
		cin>>num[i];
	}
	
}

should be
1
2
3
4
5
6
7
8
9
void getnumber(){
	
	for(i=0;i<5;i++){
		
		cout<<"enter number: ";
		cin>>num[i];
	}
	
}

because, in an array in c++, the "boxes" are numbered from 0 to n-1
int a[5] creates an array with the boxes a[0] a[1] a[2] a[3] a[4] in it
Last edited on
The variable sumi is not initialized to any value in either alitt's code or ephraimr's code, so it contains junk.

Junk + some values is still junk.
ephraimr really thanks
another question
i don't why sum doesn't work

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;

int num[5],counter=0;
void getnumber();
int sum();
void print();

int main(){
	
	char op;
	
	while(1){
		
		system("cls");
		
		cout<<"1- input 2- sum 3- print \n";
		cin>>op;
		
		switch(op){
			case '1':{
				getnumber();
				break;
			}
			case '2':{
				int total;
				total=sum();
				cout<<total;
				break;
			}
			case '3':{
				print();
				break;
			}
			
			
		}
		
	}
	return 0;
}
void getnumber(){
	
	cout<<"enter number: ";
	cin>>num[counter];
	counter++;
}
int sum(){
	
	int sumi=0;
	for(int i=0;i<counter;i++)
		sumi+=num[i];
		return(sumi);
}
void print(){
	system("cls");
	for(int i=0;i<=counter;i++){
		cout<<num[i]<<endl;
	}
	getch();	
}
Several problems with your program.

1) getnumber has no protection against entering more than 5 numbers. Doing so will write past the end of num[].

2) Globals should be avoided.

3) print is going to print out too many numbers. Line 58 should be i<counter, not <=.
thanks
i made array[5] but i'd like to add 2 or 3 numbers when i call sum
i wont insert 5 numbers to add,maybe id like to insert 4 numbers
thanks again
you're right
getnumber is not protect against more 5 number
how should i write getnumber?
A simple if statement and return is all you need.

1
2
3
4
5
6
7
8
9
10
11
12
static const int MAXNUM = 5;
int num[MAXNUM];

void getnumber()
{   if (counter >= MAXNUM)
     {  cout << "Array is full" << endl;
         return;
     }
	cout<<"enter number: ";
	cin>>num[counter];
	counter++;
}

Note that I've made MAXNUM a constant. By using MAXNUM, instead of the hard coded value 5, if you ever want to change the size of the array, all you have to do is change that one line.
Last edited on
thank you so much
my problem has solved
Topic archived. No new replies allowed.