hey, i am new here but i have taken a few c++ classes.

im having trouble with a very easy program and i just cant figure out why its doing this.

ive looked around the forum already and didnt find anything helpful so i thought id ask.

include <iostream>
using namespace std;

int add();
int subtract();
int times();
int divide();

int main()
{
    int a;
    
    cout<<"This program is a simple calulator. Its able to + - * or /\n";
    cout<<"the below table is a 'key' enter one of the numbers\n\n\n";
    cout<<" 1. add\n";
    cout<<" 2. subtract\n";
    cout<<" 3. times\n";
    cout<<" 4. divide\n\n";
    cout<<" ";
    cin>>a;
    cout<<"\n\n";
    
    if (a==1)
    add();
    else if (a==2)
    subtract();
    else if (a==3)
    times();
    else if (a==4)
    divide();
    else
    cout<<"you didnt enter a valid number.\n\n";
    
    system("PAUSE");
    return 0;
}

int add()
{
      
      int num, sum=1;
      cout<<"how many numbers would you like to add?\n";
      cin>>num;
      for (int i=1;i<num;i++)
      {
          int array[i];
          sum+=array[i];
          }
      cout<<"Your new number is  "<<sum<<"\n\n\n";
}

its able to run, but when i enter any number for the amount of numbers to add, it prints out some long number like 257487465896546549876546

my question is why is it giving me that long number and how can i fix it?

Well first off add should be void not an int... if your function returns an int... then it should have "return( some int )" in it somewhere.

What are you trying to add in the program? right now your not adding anything that will be helpful to the user.

int add()
{
      int num, sum=1;
      cout<<"how many numbers would you like to add?\n";
      cin>>num;
      for (int i=1;i<num;i++)
      {
          int array[i];
          sum+=array[i];
          }
      cout<<"Your new number is  "<<sum<<"\n\n\n";
}

Three problems with the ADD function
1) What he said above
2) starting sum at 1 will give you the wrong answer every time. Adding 5+4 will give you 10.
3) There are no values in array so you are adding garbage.

i changed int add() to void add() but no luck. its still giving me some weird really long number.

im trying to ask the user how many number they want to add, then have them enter the X amount of numbers like if the user enters 5 numbers, itll ask for the 5 numbers and then add them all together.

i forgot to delete out the sum=1 when posting. it shouldnt be 1..

hmm sorry if i made this confusing, i tryed a lot of things and only posted my last attempt. there should be a cout statement in the for loop like:
for (int i=1;i<num;i++) {
cout<<"\nenter number "<<i<<" ";
}

then it would take in the amount of numbers and add.

thanks for the help so far. im beginning to like this place :)

Well first off changing the int add to void add wouldnt remove errors... its just kind of a programming law to have void unless you want it to return something, Heres a more useful add function for you

void add(){
int num, tmp, sum=0;

cout<<"how many numbers would you like to add?\n";

cin>>num;

for (int i=0;i<num;i++)     //loop starts at 0 goes until i = num
{                                     //so if you enter 3 it will add as i = 0 then 1 then 2 and exit
cout<<endl<<"Enter number to add"<<endl;   //prompt user for number to add
cin>>tmp;                                                                //have tmp = user input
sum = sum+tmp;                                                    //sum = previous sum + tmp
}

cout<<"The sum of those numbers is:  "<<sum<<"\n\n\n"; //print sum

}

works great now, thanks for all the help

@ jelinky,
As a good practice, do a while-loop instead of a for-loop. This is because you can avoid getting total number of numbers to be processed(added,sub..). Assume in case the user is not sure of the total numbers to be processed a while-loop wil do fine. fyi, this works if the user knows the total numbers to be processed.

void add()
{
int num, tmp, sum=0; 
int count =0;
cout<<"Enter numbers to be processed.Incase you need to stop,enter any character\n"; 

cin>>num; 
while (num)
{      
count++;                              
  sum = sum+num;
}
cout<<"Total of %d values is %d"<<count<<sum<<endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.