STL Vector problem with code
ok so i intended for this code to find the average of a numberOfElements defined by the person using the program... do i'm trying to use vector to pushback the array and make it larger, ad it works until it averages out the numbers and gives an insance answer... I'm not sure why, and help is appreciated...
thanks
CJ
Code:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int numberOfElements;
vector<int> array;
int sum=0;
int arrayNumber = 0;
int forLoopNumber = 0;
bool finished = false;
cout<< "How many numbers will you be entering?";
cin>> numberOfElements;
cin.ignore();
array.push_back(1);
cout<<"\n Enter the numbers:\n\t\t\t";
while(finished == false){
cin>> array[arrayNumber];
cin.ignore();
if (arrayNumber + 1== numberOfElements){
finished = true;
}
if (arrayNumber + 1 != numberOfElements){
arrayNumber++;
array.push_back(1);
}
}
for (forLoopNumber = 0; forLoopNumber <= numberOfElements; forLoopNumber++){
sum += array[forLoopNumber];
}
cout<<"Average equals: " << sum/numberOfElements;
cin.get();
}