I'm trying to create a function that adds all the arrays together however I'm trying to figure out how to make my program know how many arrays have been populated and how many of them to add together. I would add all 501 arrays together but assuming that all the arrays have a value of 0 would be the wrong thing to do....here's what I have so far. My compiler says that "sum" is an undeclared identifier but I thought the compiler would know that Sum means addition after including the cmath header no?
// Project 4.cpp : main project file.
#include "stdafx.h"
#include<iostream>
#include<string>
#include<cmath>
usingnamespace std;
int main()
{
int n[501];
int b;
int choice;
for(int i=0; i<501; i++)
{
cout << "Please enter all your numbers and type -1 when you are done." << endl;
cin >> n[i];
if(n[i] == -1)
{
break;
}
}
for(int i=0; i<501; i++)
{
sum += n[i];
}
cout << "Type 1 if you want the sum of all the numbers you've inputted or 2 for the average." << endl;
cin >> choice;
if(choice = 1)
{
cout << "The sum of all your numbers is " << sum;
}
system("PAUSE");
return 0;
}
My compiler says that "sum" is an undeclared identifier but I thought the compiler would know that Sum means addition after including the cmath header no?
Where in the documentation for cmath did you find that it said it would declare a variable called sum as an integer for you?
Sorry if my questions made me seem as if I'm not doing any research on my own....my professor leaves us on our own to figure out the problems which is great but the book I have to go back to is C++ Without Fear A Beginners Guide That Makes You Feel Smart. I find myself coming to this site for a little push in the right direction and I appreciate it guys. I also hadn't noticed this site has some clear and understandable documentation on this stuff. Thanks for pointing that out MikeyBoy!