Good morning everyone, first post here but I have been lurking for a bit and you all offer wonderful advice and support. As a brief introduction, I am a sophomore engineering student who is currently taking an 8-week Intro to C++ class online. Through the first half of the term I have discovered that coding is a bit of a struggle for me, even through I am dedicating the necessary time. Despite I have been able to complete all the assignments thus far.
My issue is our latest program which is described below; the problem is I am totally confused by the concept of a vector and have no idea how to start coding this program. Any guidance would be appreciated, thank you!
Write a program that reads an unspecified number of integers from STDIN, terminated with EOF.
The only output of your program (so no prompts or other output please!) should be all of the odd values output in the reverse of the order they were input in (one value per line) followed by all of the even values also output in the reverse of the order they were entered (one value per line.)
Your program should make use of the vector, as described in class and the textbook.
Thank you for all the suggestions so far. Using my textbook and an example I found online I have build the code below. I am stuck at this point, the program compiles but only accepts one input then hangs. Very frustrated at this point and any advice would be greatly appreciated.
#include <iostream>
#include <iomanip>
#include <vector>
usingnamespace std;
int main ()
{
int input = 0;
cout << "Enter a number(-9 to quit): ";
cin >> input;
vector <int> even; // declare a vector
while (cin >> input)
{
if ( input % 2 == 0)
{
even.push_back(input);
}
else
{
vector <int> odd; // declare a second vector
while (cin >> input)
{
if (input % 2 != 0)
{
odd.push_back(input);
}
}
vector <int>::iterator It;
for (It = even.begin(); It != even.end(); ++It) // REVERESE THESE TO REVERSE OUTPUT???
cout << *It << '\t'; // output the current value that It is *pointing to
// print a new line
cout << endl;
for (It = odd.begin(); It != odd.end(); ++It)
cout << *It << '\t'; // output the current value that It is *pointing to
cout << endl;
cin.get();
return 0;
}
}
}
#include <iostream>
#include <vector>
int main ()
{
// declare both the vectors before the loop
// we need these to be alive till the end, where we print out their contents
std::vector<int> even_numbers ;
std::vector<int> odd_numbers ;
int number = 0;
// for each number that the user enters till a -9 is entered (or input fails)
while( std::cin >> number && number != -9 ) // no prompts or other output please!
{
if( number%2 == 0 ) even_numbers.push_back(number) ; // even number
else odd_numbers.push_back(number) ; // odd number
}
// all of the odd values output in the reverse of the order
// they were input in (one value per line)
// so we use reverse_iterators: rbegin() and rend()
// auto: http://www.stroustrup.com/C++11FAQ.html#autofor( auto iter = odd_numbers.rbegin() ; iter != odd_numbers.rend() ; ++iter )
std::cout << *iter << '\n' ;
// followed by all of the even values also output in the reverse ...
for( auto iter = even_numbers.rbegin() ; iter != even_numbers.rend() ; ++iter )
std::cout << *iter << '\n' ;
}
Thank you for you reply JLBorges. The way you have this coded makes more sense to me, although I am confused on how the auto iter works. This looks like it is very close to running although my compiler gives me an warning citing "warning: ‘auto’ changes meaning in C++11; please remove it". Also that iter what not declared...Please help. The topic of vectors is very confusing to me.