In: Computer Science
Write a short C++ program that takes all the lines input to standard input and writes them to standard output in reverse order. That is, each line is output in the correct order, but the ordering of the lines is reversed.
Please use vector datatype standaard library #include <vector>
Thanks
Note :
Check is this is the exact requirement do you want?If you want me to do any changes I will do it.Thank You.
______________________
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//Derclaring variable
string line;
//Declaring vector
vector<string> vec;
/* This loop continues to execute
* until the user enters -1 as input
*/
while(true)
{
//Getting the setence entered by the user
cout<<"Enter a Sentence :";
std::getline(std::cin, line);
//Checking whether the sentence is valid or not
if(line!="-1")
{
//Inserting sentence into vector.
vec.push_back(line);
continue;
}
else
break;
}
cout<<"\nThe elements in the vector are :\n";
//This for loop will display the elements inside the vector
for(int i=0;i<vec.size();i++)
{
cout<<vec[i]<<endl;
}
//Displaying the elements in the vector
cout<<"\n\nThe elements in the vector in reverse order are
:\n";
//This for loop will display the elements inside the vector
for(int i=vec.size()-1;i>=0;i--)
{
cout<<vec[i]<<endl;
}
return 0;
}
__________________________
output:
________Thank You