C++ Program using function overloading

Question: Write a C++ Program to find the sum of either all the elements of the array or sum of the even elements of the array or the odd elements of the array using function overloading. The user can decide which sum he wants to find out.

I have written the following C++ Program however it is giving an error message. The program and the error is given below. Can you help me to identify what is going wrong and how can I fix it?


#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int sum(int array[])
{
int n,sum=0;
cout<<"Enter n:";
cin>>n;
for(int i=0; i<n; i++)
{
sum=sum+array[i];
}
return sum;
}
int sum(int array[], choice)
{
int even=0,n;
cout<<"Enter n:";
cin>>n;

if(choice==2)
{
for(int i=0; i<n; i++)
{
if(array[i]%2==0)
{
even=even+array[i];
}
return even;
}
}
else
if(choice==3)
{
for(int j=0; j<n; j++)
{
if(array[j]%2!=0)
{
odd=odd +array[j];
}
return odd;
}
int main()
{
clrscr();
char a[20];
int add, choice,n;
cout<<"Enter n:";
cin>>n;
cout<<"Enter Elements:";
for(int k=0; k<n)
{
cin>>a[i];
}
cout<<"Sum Menu:"<<endl;
cout<<"1. Sum of all the elements in the array"<<endl;
cout<<"2. Sum of all the even elements in the array"<<endl;
cout<<"3. Sum of the odd elements in the array"<<endl;
cout<<"Enter your choice(1-3)";
cin>>choice;
if (choice==1)
{
add=sum(a);
cout<<"The sum of the elements in the array is:<<add<<endl;
}
else
if(choice==2)
{
add=sum(a, choice);
cout<<"The sum of the even elements in the array is:<<add<<endl;
}
else
if (choice==3)
{
add=sum(a, choice);
cout<<"The sum of the odd elements in the array is:<<add<<endl;
}
else
cout<<"Wrong choice!!";

return 0;
}





I am getting following ERROR:

ERROR:int sum(int array[], choice)
) EXPECTED


missing ending " on many strings.
<iostream> and <cstdio> are the correct includes. once you do that you need to put std:: in front of cout, cin, etc statements or add a using statement like using std::cin;

choice, second sum function lacks a type. (int, most likely).
odd, no such variable (missing declare).

the compiler should be telling you these things. Reading and dealing with errors, in small programs, fix the topmost error each time and try to compile it again. Errors create false secondary messages that are of no value, so this is a good approach for schoolwork.

also it will help if you put the code in code tags <> on the side panel -- so we can run the in-forum compiler to fix stuff.
Last edited on
indent your code properly and another one of your errors will be obvious.

1
2
3
4
5
int sum(int array[])
{
   int n,sum=0;
   cout<<"Enter n:";
   cin>>n;
hideous, ¿why don't just pass the `n' that you've got in main()?
Thanks Jonnin & ne555 for your inputs. I have included Indentation and int Type for the second sum function however I have getting some new ERRORS given below. Please check and advise.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

int sum(int array[])
{
int n,sum=0;
cout<<"Enter n:";
cin>>n;
for(int i=0; i<n; i++)
{
sum=sum+array[i];
}
return sum;
}
int sum(int array[], int c)
{
int even=0,odd=0,n;
cout<<"Enter n:";
cin>>n;

if(c==2)
{
for(int i=0; i<n; i++)
{
if(array[i]%2==0)
{
even=even+array[i];
}
return even;
}
}
else
if(c==3)
{
for(int j=0; j<n; j++)
{
if(array[j]%2!=0)
{
odd=odd +array[j];
}
}
}
return odd;
}
int main()
{
clrscr();
char a[20];
int add, choice,n;
cout<<"Enter n:";
cin>>n;
cout<<"Enter Elements:";
for(int k=0; k<n; k++)
{
cin>>a[k];
}
cout<<"Sum Menu:"<<endl;
cout<<"1. Sum of all the elements in the array"<<endl;
cout<<"2. Sum of all the even elements in the array"<<endl;
cout<<"3. Sum of the odd elements in the array"<<endl;
cout<<"Enter your choice(1-3)";
cin>>choice;
if (choice==1)
{
add=sum(a);
cout<<"The sum of the elements in the array is:<<add<<endl;
}
else
if(choice==2)
{
add=sum(a, choice);
cout<<"The sum of the even elements in the array is:<<add<<endl;
}
else
if (choice==3)
{
add=sum(a, choice);
cout<<"The sum of the odd elements in the array is:<<add<<endl;
}
else
cout<<"Wrong choice!!";

return 0;
}




Here are the new ERRORS:

ERROR: add=sum(a);
Could not find a match for sum(char * )
ERROR: add=sum(a,choice);
Could not find a match for sum(char *,int )
ERROR: add=sum(a,choice);
Could not find a match for sum(char *,int )

¿why did you declare `a' as char a[20];?
an example of an int array: {6, 9, 54, 42, 13}
an example of a char array: {'h', 'e', 'l', 'l', 'o', '\0'}
¿which one do you want?


> I have included Indentation
[code]
1
2
3
"put the code between these [code] tags"
   #then you'll have indentation
      and color
[/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
#include <iostream>
#include <valarray>
using namespace std;

int main()
{
   int N, choice = 1;

   cout << "Enter N: ";   cin >> N;
   valarray<int> A(N);

   cout << "\nEnter " << N << " elements: ";
   for ( int i = 0; i < N; i++ ) cin >> A[i];

   while( choice != 0 )
   {
      cout << "\nEnter 1 for sum of all, 2 for sum of even, 3 for sum of odd, 0 to stop: ";   cin >> choice;
      switch( choice )
      {
         case 0: return 0;
         case 1: cout << A.sum() << '\n'; break;
         case 2: cout << ( (1-A%2 ) * A ).sum() << '\n'; break;
         case 3: cout << (    A%2   * A ).sum() << '\n'; break;
         default: cout << "R.T.Q!\n";
      }
   }
}


Enter N: 5

Enter 5 elements: 1 2 3 4 5

Enter 1 for sum of all, 2 for sum of even, 3 for sum of odd, 0 to stop: 1
15

Enter 1 for sum of all, 2 for sum of even, 3 for sum of odd, 0 to stop: 2
6

Enter 1 for sum of all, 2 for sum of even, 3 for sum of odd, 0 to stop: 3
9

Enter 1 for sum of all, 2 for sum of even, 3 for sum of odd, 0 to stop: -1
R.T.Q!

Enter 1 for sum of all, 2 for sum of even, 3 for sum of odd, 0 to stop: 0
Last edited on
Thanks ne555 & lastchance for your inputs. They were very helpful. After declaring the array as an integer array, the program ran successfully. Thanks again.

Topic archived. No new replies allowed.