Cin to a vector

How do i cin to a vector. It seems right but the program prinitng out values of zero instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61


#include <iostream>
#include <vector>

using namespace std;

//Function Declarations
void fillVector(vector<float>& temperatureVector);
//fillVector
//@param vector<float>)& - the vector to be filled

void printVector(const vector<float>& temperatureVector);

//Constants


int main()
{
		
	cout << "Type in how many measurements you have done: ";
	
	int sizeOfVector;
	cin >> sizeOfVector;

	vector<float>temperatureVector(sizeOfVector);

	fillVector(temperatureVector);
	printVector(temperatureVector);
	
	return 0;
}


// Function Definitions
void fillVector(vector<float>& temperatureVector)
{

	
	for(int i = 0; i < temperatureVector[i]; i++)
	{
		cout << "Type in temperatures";
		
		float input;
		cin >> input;
		temperatureVector.push_back(input);
	}

}

void printVector(const vector<float>& temperatureVector.)
{
	cout << "Vector: ";	

	for (unsigned int i = 0; i < temperatureVector..size(); i++) {
		cout << temperatureVector.[i] << " ";
	}
	cout << endl;
}

The code shouldn't even compile. Take a close look at the loop conditions (both of them).
It seems right

Not sure how it can be right when it doesn't compile (lines 51, 55 and 56).
I can´t figure it out how to get values into the vector. What am I doing wrong?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Ovning_13_3.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <vector>

using namespace std;

//Function Declarations
void fillVector(vector<float>& temperatureVector);
//fillVector
//@param vector<float>)& - the vector to be filled

void printVector(const vector<float>& temperatureVector);

//Constants


int main()
{
		
	cout << "Type in how many measurements you have done: ";
	
	int sizeOfVector;
	cin >> sizeOfVector;

	vector<float>temperatureVector(sizeOfVector);

	fillVector(temperatureVector);
	printVector(temperatureVector);
	
	return 0;
}


// Function Definitions
void fillVector(vector<float>& temperatureVector)
{

	
	for(int i = 0; i < temperatureVector.size(); i++)
	{
		cout << "Type in temperatures";
		
		float input;
		cin >> input;
		temperatureVector.push_back(input);
	}

}

void printVector(const vector<float>& temperatureVector)
{
	cout << "Vector: ";	

	for (unsigned int i = 0; i < temperatureVector.size(); i++) {
		cout << temperatureVector[i] << " ";
	}
	cout << endl;
}

vector<float>temperatureVector(sizeOfVector); Creates a vector filled with sozeOfVector zeroes.
Your fillVector adds another sizeOfVector values to the end of vector.
Tried to simplify it. Still I can´t figure out how to fill the vector wit float values. What am I doing Wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

#include <iostream>
#include <vector>

using namespace std;

int main()
{

	int sizeOfVector;
	cin >> sizeOfVector;

	vector<float>temperatureVector(sizeOfVector);
		
	cout << "Type in how many measurements you have done: ";

	float input;

	for(int i= 0; i < temperatureVector.size(); i++) {
	{
		cout << "Type in temperatures";
		cin >> input;
		temperatureVector.push_back(input);
	}

	cout << temperatureVector[i] << " ";
	
	return 0;
}
You are filling it. It is just you are creating equal amount of zeroes before input.

If you enter 3 as vector size and 1 2 3 as input you will get {0, 0, 0, 1, 2, 3}

You should either use push_back on empty vector or overwrite previously created values:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//1
vector<double> temperatureVector; //Do not use floats unless you have good reason for that
for(int i= 0; i < sizeOfVector; ++i) {
    double input;
    std::cin >> input;
    temperatureVector.push_back(input);
}
//2
vector<double> temperatureVector(sizeOfVector);
for(int i= 0; i < temperatureVector.size(); ++i) {
    double input;
    std::cin >> input;
    temperatureVecto[i] = input;
}
Topic archived. No new replies allowed.