# Thread: STL Vector problem with code

1. ## 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();
}```

2. Code:
```    for (forLoopNumber = 0; forLoopNumber <= numberOfElements; forLoopNumber++){
sum += array[forLoopNumber];
}
cout<<"Average equals: " << sum/numberOfElements;```
First of all, your for condition doesn't look quite right.

Second, the expression sum/numberOfElements is of type int, meaning that any fractional part of the quotient will be discarded (number will be rounded down).

3. ok I belive I fixed the problem

somthing in the for loop wasn't working out... I also changed those that needed to be changed to float

Fixed code:

Code:
```#include<iostream>
#include<vector>

using namespace std;

int main()
{
float numberOfElements;
vector<int> array;
float sum=0;
int arrayNumber = 0;
int loopNumber = 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);
}
}
while (loopNumber < numberOfElements){
sum += array[loopNumber];
loopNumber++;
}
cout<<"Average equals: " << sum/numberOfElements;
cin.get();
}```

Some optimization in the code:
Code:
```#include<iostream>
#include<vector>

using namespace std;

int main()
{
float numberOfElements;
vector<int> array;
float sum=0;
int arrayNumber = 0;
int loopNumber = 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;
}

else {
arrayNumber++;
array.push_back(1);
}
}

for (loopNumber = 0; loopNumber < numberOfElements; loopNumber++){
sum += array[loopNumber];
}

cout<<"Average equals: " << sum/numberOfElements;
cin.get();
}```

4. You are completely mistreating the vector. The whole point of the vector is that you do not need to worry about size. Read into a temp variable, then push that back. Don't push back an arbitrary number and read directly into the vector.
You basically got multiple if statements in the first loop. You can easily replace the second one with "else". Less typing, less prone to bugs and less code duplication. Better for all.
And the last loop should really be a for loop.
You've also borked the indentation in some places, especially in your first loop and the if statements.

5. jeese lol tear it apart.... ok i see what your saying I'll work on it..
thanks for the input... I've done what I know exactly how to do I will fix the vector a little later..
thanks again

6. Good training! Then you won't make the same mistake next time

7. Originally Posted by jamort
Code:
```    float numberOfElements;
// ...
cout<< "How many numbers will you be entering?";
cin>> numberOfElements;```
Hmm so if I would like to enter 2.3 elements that's okay?
Nope, aparently 2.3 means an infinite number of items....

8. Code:
```#include<iostream>
#include<vector>

using namespace std;

int main()
{
int numberOfElements;
int arrayNumber = 0;
int loopNumber = 0;
vector<int> array;
float sum=0;
bool finished = false;

cout<< "How many numbers will you be entering?";
cin>> numberOfElements;
cin.ignore();

cout<<" Enter the numbers:\n\t\t\t";

array.reserve(numberOfElements);

while(finished == false){
cin>> array[arrayNumber];
cin.ignore();

if (arrayNumber + 1== numberOfElements){
finished = true;
}

else {
arrayNumber++;
}
}

for (loopNumber = 0; loopNumber < numberOfElements; loopNumber++){
sum += array[loopNumber];
}

cout<<"Average equals: " << sum/numberOfElements;
cin.get();
}```
ok so i think theres probably something that i still need to fix, but i need to get a book on stl cause the one that i have doesnt really go into it

9. Don't trust the user.

int temp;
cin >> temp;
array.push_back(temp);

10. Don't think I did what you said to do right.... let me know... I edited this because the way i had if there was a 0 or negative number entered it would mess up

Code:
```#include<iostream>
#include<vector>

using namespace std;

int main()
{
int numberOfElements = 0;
int arrayNumber = 0;
int loopNumber = 0;
vector<int> array;
float sum=0;
bool finished = false;
while (numberOfElements <= 0){
cout<< "How many numbers will you be entering?" << endl;
cin>> numberOfElements;
cin.ignore();

if (numberOfElements <= 0){
cout<< endl << "Error... Please enter a positive integer"<<endl;;
}
}

cout<<" Enter the numbers:\n\t\t\t";

array.push_back(numberOfElements);

while(finished == false){
cin>> array[arrayNumber];
cin.ignore();

if (arrayNumber + 1== numberOfElements){
finished = true;
}

else {
arrayNumber++;
}
}

for (loopNumber = 0; loopNumber < numberOfElements; loopNumber++){
sum += array[loopNumber];
}

cout<<"Average equals: " << sum/numberOfElements;
cin.get();
}```

11. Code:
```#include<iostream>
#include<vector>

using namespace std;

int main()
{
int numberOfElements = 0;
int arrayNumber = 0;
int loopNumber = 0;
vector<int> array;
float sum=0;
bool finished = false;
int temp;
while (numberOfElements <= 0){
cout<< "How many numbers will you be entering?" << endl;
cin>> numberOfElements;
cin.ignore();

if (numberOfElements <= 0){
cout<< endl << "Error... Please enter a positive integer"<<endl;;
}
}

cout<<" Enter the numbers:\n\t\t\t";

//    array.push_back(numberOfElements);

while(finished == false){
cin >> temp;
array.push_back(temp);
cin.ignore();

if (arrayNumber + 1== numberOfElements){
finished = true;
}

else {
arrayNumber++;
}
}

for (loopNumber = 0; loopNumber < numberOfElements; loopNumber++){
sum += array[loopNumber];
}

cout<<"Average equals: " << sum/numberOfElements;
cin.get();
}```

12. ok... I see now so when you pushback an array the number that is passed becomes the new value...
thanks again... I got a book coming on stl and hopefully it will be here tomorrow...