Some Beginner/array Help.
Hey guys, I've been a long time fan of programming and have dabbled into it abit here and there in some java/html etc. I've decided to have a go at C++ and I'm having a few problems. I understand the basics of variables/functions and how things work, but im having a few slight problems.
I wanted to create a small program that asked the user to input a series of numbers, and then display the sum of these numbers. Ive been reading some tutorials and example code and to my understanding the best way to do this would be to create an array to store the data in, and create a function to enter the data, and a function to calculate the sum. I believe in theory I have the right Idea but im having a few small problems.
When it comes to functions, I understand how they work for the most part, but I am a bit confused as to where the arguments if any come from in the () when declaring the function. If somebody could help me out with this it would be great.
Anyways here is the code that I have came up with, but when compiling I get some errors.
Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
// declare some variables
int number;
int arraylength;
int array1[arraylength];
//funtction to get the numbers entered
int numbers(){
for (i = 0;i <= arraylength;i++) {
cout << "Please enter number " << i << " of " << arraylength;
cin >> number;
array1[i] = number;
}
}
//function to get the sum of the numbers entered in the array
int sumofarray(array1[i];arraylength) {
for (i = 0; i <= arraylength;i++){
acumulate = acumulate + array1[i];
}
return acumulate;
}
//main function
int main(int nNumberofArgs, char* pszArgs[]){
int sum;
cout << " This program lets you enter a series\n of numbers and gives you the sum.\n Please enter how many numbers you want to add";
cin >> arraylength;
cout << " you have chosen " << arraylength << " numbers. /';
//call the function to enter numbers into the array
numbers();
//get the sum of the numbers entered in the array
sum = sumofarray();
cout << " You have entered all the numbers. The sum of these numbers is " << sum;
system("PAUSE");
return 0;
}
And here is the error log:
Quote:
\arrays.cpp(12) : error C2057: expected constant expression
\arrays.cpp(12) : error C2466: cannot allocate an array of constant size 0
\arrays.cpp(18) : error C2065: 'i' : undeclared identifier
\arrays.cpp(18) : error C2065: 'i' : undeclared identifier
\arrays.cpp(18) : error C2065: 'i' : undeclared identifier
\arrays.cpp(20) : error C2065: 'i' : undeclared identifier
\arrays.cpp(24) : error C2065: 'i' : undeclared identifier
\arrays.cpp(31) : error C2065: 'i' : undeclared identifier
\arrays.cpp(31) : error C2143: syntax error : missing ')' before ';'
\arrays.cpp(31) : fatal error C1903: unable to recover from previous error(s); stopping compilation
1>arrayfun - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If someone here more experienced could lend me a hand and point me in the right direction as to what im doing wrong, it would be great. Thanks.