Using push_back

I am looking up an online tutorial for C++ programming and it asks to use a push_back method to get 5 values and save them in an array. I have done the program without the push_back method because I don't understand it. How can I add something like that to this program. I know that the line 2 needs to be there for the push_back, but not sure how to add it to a loop that gets the values. Thanks for your help.

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
#include <iostream>
#include <vector>

using namespace std;

//Declaring constants
const int MAX = 5;

int main() 
{
	//Initialize some components
	double sum = 0;
	int myData[MAX];

	// first loop - get the data
	for (int count = 0; count< MAX; count++)
	{
		cout << "Enter Number:  ";
		cin >> myData[count];
	}
	// second loop - accumulate the total
	for (int count = 0; count < MAX; count++)
	{
		sum += myData[count];
	}
	cout << "The sum is " << sum << endl;

	//Creating end point for the program
	system("PAUSE");

	// Returning 0
	return 0;
}
Line 13 should be:
std::vector<int> myData;
Now myData is an object that has the push_back method.

Next, the input loop should preferably be a while, where the condition tests both size of the array and success of input (into a variable). Then the variable is pushed into the myData in the body of the loop.

The summation loop should then loop over the actual vector and not presume it to have MAX elements. There is std::accumulate() to use instead of explicit loop.
I tried to add a do while loop but for some reason the code output hasn't changed from when I edited it. Can you see a mistake I can't somewhere?

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
#include <iostream>
#include <vector>

using namespace std;

//Declaring constants
const int MAX = 5;

int main() 
{
	//Initialize some components
	double sum = 0;
	vector<int> myData;

	// now read data from cin until a zero is typed
	cout << "\nType in exam scores ... enter zero to quit";

	//Declaring variables
	int i;
	double data;

	// first loop - get the data
	do
	{
		cout << "\nscore  " << i++ << ": ";
		cin >> data;
		if (data != 0) // this is a valid score, save it
		{
			myData.push_back(data);
		}
	} while (data != 0);

	// second loop - accumulate the total
	for (int i = 0; i < MAX; i++)
	{
		sum += myData[i];
	}
	cout << "The sum is " << sum << endl;

	//Creating end point for the program
	system("PAUSE");

	// Returning 0
	return 0;
}
Line 7: Not needed.

Line 19,25: i is uninitialized. This will print garbage.

Line 34: As keskiverto pointed out, the loop limit should be myData.size(), not MAX. This will allow any number of grades to be input. The vector will adjust it's size to accommodate.
Some issues:

You have now a vector of integers, but you push double values into it.

'i' is uninitialized before the loop, and thus value shown on line 25 is undefined.

You don't test whether the input on line 26 succeeds, and therefore the value of 'data' on line 27 might be undefined.

Comparing a double against 0 is nefarious.

You reintroduce 'i' on line 34. It has already been declared on line 19.

You continue to add up MAX elements in the for loop regardless of how many elements the myData actually has.
I've tried to add some of the fixes mentioned, but I still cannot get the sum to arrive on the output.

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
#include <iostream>
#include <vector>

using namespace std;

//Declaring constants

int main() 
{
	//Initialize some components
	double sum = 0;
	vector<double> myData;

	// now read data from cin until a zero is typed
	cout << "\nType in up to five decimals for a sum ... enter zero to quit";

	//Declaring variables
	int i = 1;
	double data;

	// first loop - get the data
	do
	{
		cout << "\nNumber " << i++ << ": ";
		cin >> data;
		if (data != 0) // this is a valid score, save it
		{
			myData.push_back( data );
		}
	} while (data != 0);

	// second loop - accumulate the total
	for ( i; i <= myData.size(); i++)
	{
		sum += myData[i];
	}
	cout << "The sum is " << sum << endl;

	//Creating end point for the program
	system("PAUSE");

	// Returning 0
	return 0;
}
The problem is your for loop at line 33. You're not initializing i and instead using the last value of i which was used to count the number of values (line 24). Your termination condition is also faulty. Assuming you initialize i to 0, <= will cause you to subscript past the end of the vector. If you enter 5 values, those values are [0]-[4]. [5] is not valid.

 
  for (int i=0; i < myData.size(); i++)


Hi all.
DEnumber50 try to use vector functions to access the vars... . like .at()
read the functions of vector. I'm sure you will correct your mistake after you read this functions and used them.
be careful about line 35. and try to write clear code! and do not write useless comments like Returning 0.
Thanks

Here is your code, But please read the vectors functions, they will help you in future.

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
#include <iostream>
#include <vector>

using namespace std;

int main() 
{
	double sum = 0;
	vector<double> myData;
	cout << endl<<"Type in up to five decimals for a sum ... enter zero to quit";
	int j = 0;
	double data;
	do
	{
		cout << endl <<"Number " << ++j << ": ";
		cin >> data;
		if (data != 0)
		{
			myData.push_back( data );
		}
	} while (data != 0);
	for (int i = 0; i <= myData.size()-1; i++)
		sum += myData.at(i);
	cout << "The sum is " << sum / myData.size() << endl;
	cin.get();  // try to do not use system function. cin.get() will wait to cin an elemnt from keyboard as the system("PAUSE") do.
	cin.get(); // try to do not use system function.
	return 0;
}
Last edited on
Thanks for the help on this one.
Topic archived. No new replies allowed.