I'm having a bit of trouble with this one, the goal of this program is to enter in 1 to 5 numbers, with a maximum of 5, into a vector and then popping as many numbers as the user wants until the vector contains nothing. My problem is that I can't figure out how to print out the numbers contained in the vector. I also am thinking about putting something in to detect whether or not there is anything in the vector so that the user doesn't crash the program. Any ideas on how I can solve these two problems?

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	int addnums;
	int index;
	char selection;
	int counter = 0;
	vector<int> values;

	values.push_back(1);
	values.push_back(2);
	values.push_back(3);
	values.push_back(4);
	values.push_back(5);

	

	do
	{
	
	cout << "How many entries would you like to add (up to 5): ";
	cin >> addnums;
	
		
		if (addnums <= 5)
		{
			for (index = 0; index < addnums; index++)
			{
				int tempnums;
				
				cout << "Enter integer " << (index + 1) << ": ";
				cin >> tempnums;
				values.push_back(tempnums);
				counter++;
			}

		}
		
		if (addnums >= 6)
		{
		cout << "Please choose a number in the range of 1 and 5\n\n";
		}
		else 
		break;
	}while (counter != addnums);

	
	
	do
	{
		cout << "Press E to End or Press P to pop a value: ";
		cin >> selection;

		if(selection == 'p' || selection == 'P')
		{ 
			cout << "The integer you popped was: " ; << values.begin();
			values.erase (values.begin());
		}
		else 
			break;
	}while(selection == 'p' || selection == 'P');
	
	
	return 0;
}

Recommended Answers

All 6 Replies

you can use a for loop to print using the following syntax(just like an array)

cout << values[i] << endl;

>>values.erase (values.begin());
That should erase the entire array.

Here is one way to print all the integers in the array

for(int i = 0; i < values.size(); i++)
   cout << values[i] << "\n";

Here is another way using an iterator

vector<int>::iterator it = values.begin();
for( ; it != values.end(); it++)
   cout << *it << "\n";

Why are you not using vector::pop_back ?

do
{
    cout << "Press E to End or Press P to pop a value: ";
    cin >> selection;

    if (selection == 'p' || selection == 'P')
    {
        cout<<"The integer you popped was: "
        << values.back();//return last
        values.pop_back();//delete last
    }
    else
        break;
}while ( ! values.empty() );//till vector isn't empty

Sorry, forgot to clarify, I was going to use pop_back, but the project calls for popping the first integer in the vector each time, not the last, I was thinking about getting around that by using reverse, but screwed up and just decided to do it this way. Sorry, I'm still pretty new to all this.

I'll see if I can solve everything using all of your answers.

In that case change my 9th and 10th line to:

<< values.front();//return first
        values.erase( values.begin() );//delete first

Thanks a bunch guys, especially you Sidd, you saved my butt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.