# c++ vectors • ### Question

• Im very new to vectors and i need help. I would like to display a number to be of a certain size of three digits only. i am randomizing numbers between 0 and 500 and in the number is 9, i want it to display as 9.00 and if its 45 display it as 45.0 and if say 365 then leave it as 365. here is my code but i cant get the length() function to work on a vector. im displaying to a file byw thats why it is ofs. I am also trying to display it in rows of 10 numbers long by 50 rows down, if you know how to do that please help.

for (int i = 0; i < 50; ++i){
ofs << std::fixed;
if (num[i].length() == 3){
L = 0;
}
else if (num[i].length() == 2){
L = 1;
}
else if (num[i].length() == 1){
L = 2;
}
ofs << i << ". " << setprecision(L) << num[i] << endl;

}
}

I also need to display this as i said

10 numbers across and 50 rows down displaying all the numbers.  Im going to try to see what i can do. Thanks.

Saturday, April 4, 2015 10:54 PM

• here is my code but i cant get the length() function to work on a vector.

for (int i = 0; i < 50; ++i){
ofs << std::fixed;
if (num[i].length() == 3){
L = 0;
}
else if (num[i].length() == 2){
L = 1;
}
else if (num[i].length() == 1){
L = 2;
}
ofs << i << ". " << setprecision(L) << num[i] << endl;

}
}

There are numerous issues with the code you posted.

Is num a vector<int>, a vector<string>, or what?

Are you trying to convert ints to floats or doubles?
If so, that could result in some discrepancies.

Why not keep it simple? e.g. -

```#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
vector<int> vint;
vint.push_back(45);
vint.push_back(9);
vint.push_back(365);

for(size_t s=0; s<vint.size(); ++s)
{
cout << vint[s];
if(vint[s] < 10) cout << ".00";
else if(vint[s] < 100) cout << ".0";
cout << '\n';
}

return 0;
}
```

- Wayne

• Marked as answer by Sunday, April 5, 2015 1:43 AM
Sunday, April 5, 2015 1:00 AM

### All replies

• here is my code but i cant get the length() function to work on a vector.

for (int i = 0; i < 50; ++i){
ofs << std::fixed;
if (num[i].length() == 3){
L = 0;
}
else if (num[i].length() == 2){
L = 1;
}
else if (num[i].length() == 1){
L = 2;
}
ofs << i << ". " << setprecision(L) << num[i] << endl;

}
}

There are numerous issues with the code you posted.

Is num a vector<int>, a vector<string>, or what?

Are you trying to convert ints to floats or doubles?
If so, that could result in some discrepancies.

Why not keep it simple? e.g. -

```#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
vector<int> vint;
vint.push_back(45);
vint.push_back(9);
vint.push_back(365);

for(size_t s=0; s<vint.size(); ++s)
{
cout << vint[s];
if(vint[s] < 10) cout << ".00";
else if(vint[s] < 100) cout << ".0";
cout << '\n';
}

return 0;
}
```

- Wayne

• Marked as answer by Sunday, April 5, 2015 1:43 AM
Sunday, April 5, 2015 1:00 AM
• Thank you i got it wayne, it was a double in the vector btw. sorry about that, this is what i did if you care.

for (int i = 0; i < 50; ++i){
ofs << std::fixed;

if (num[i] < 10){
L = 2;
}
else if (num[i] < 100){
L = 1;
}
else {
L = 0;
}

ofs << i << ". " << setprecision(L) << num[i] << endl;

}

Sunday, April 5, 2015 1:44 AM