output numbers in pairs

Hello all! I'm trying to write a program that will cout every two numbers I enter. For example, if I were to enter 34 56 78 12 34 67 they would be output in pairs:

34 56
78 12
34 67

This code simply does nothing, so I'm wondering what I've done wrong! Thank you!

1
2
3
4
5
6
7
8
9
10
11
int entryvariable = 0;
int numberofentries = 0;
vector<int>vector1;


while (cin>>entryvariable)
	vector1.push_back(entryvariable);
	++numberofentries;

if (numberofentries % 2 == 0)
	cout<<vector1[numberofentries - 1] << vector1[numberofentries] << "\n";
You're just missing a bracket:
1
2
3
4
5
while (cin>>entryvariable)
{
	vector1.push_back(entryvariable);
	++numberofentries;
}


You're program is going to crash though. If the vector has one entry, you can not access vector[1].

Not to mention a vector's size() method.
Last edited on
1
2
3
4
5
6
while (cin>>entryvariable)
	vector1.push_back(entryvariable);
	++numberofentries;

if (numberofentries % 2 == 0)
	cout<<vector1[numberofentries - 1] << vector1[numberofentries] << "\n";


I think you must have brought the block brackets/curly bractest('{}') for the while loop.

So it would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
int entryvariable = 0;
int numberofentries = 0;
vector<int>vector1;


while (cin>>entryvariable)
{//open it here
	vector1.push_back(entryvariable);
	++numberofentries;

if (numberofentries % 2 == 0)
	cout<<vector1[numberofentries - 1] << vector1[numberofentries] << "\n";
}//and close it here 


HTH,
Aceix.
Last edited on
Yeah, it doesn't do anything. It just crashes. :(
Topic archived. No new replies allowed.