I'm trying to build a program which outputs the inputs in reverse order until the word "stop" is entered. for example, if I enter
dog cat monkey
desk chair
burger cheese ham
stop
it should be outputting:
burger cheese ham
desk chair
dog cat monkey
So far I have the following:
{
string x, y;
do {
cin >> x;
y = y + x;
} while (x != "stop");
int reverse = y.getNumLines();
while (reverse >= 0) {
cout << y.getLine(reverse) << "\n";
reverse = reverse -1;
}
return 0;
}
It reads my input correctly, but for the y.getNumlines
and y.getLine
, I get error with no outputs. Can anyone help me out?
Why not swap the order you are appending?
#include <iostream>
int main(int argc, const char * argv[])
{
std::string x, y;
while ( (std::getline(std::cin,x)) && x != "stop"){
y = x + "\n" + y; // "" + "foo bar" 2.) "second input" + "foo bar"
}
std::cout<< y;
return 0;
}
Nico's answer might be a better path for you, but for those who prefer a more idiomatic approach, I recommend a vector of lines that you can easily print backwards once stored. The first thing would be a Line
class that you can use to idiomatically read lines of input instead of stopping at spaces:
struct Line {
std::string str;
};
std::istream &operator>>(std::istream &in, Line &line) {
std::getline(in, line.str);
if (line.str == "stop") {
in.clear(std::ios::failbit);
}
return in;
}
std::ostream &operator<<(std::ostream &out, const Line &line) {
out << line.str;
return out;
}
All our Line
needs is a string with the actual text, and I've added input and output support for it, for the reason you'll see next. Notice that the input is through getline
rather than >>
. This is the main purpose of the Line
class. I also made it fail when "stop" is entered. If that's left out, it will continue reading until the EOF.
Now you can reuse the line class with minimal effort in order to shape input however you want. Next, we get to use what we just created (or possibly had left from something else):
int main() {
std::istream_iterator<Line> in1(std::cin), in2;
std::vector<Line> inputs(in1, in2);
std::reverse_copy(std::begin(inputs), std::end(inputs), std::ostream_iterator<Line>(std::cout, "\n"));
}
The first thing we do is form a vector by reading input (using our input support for Line
). When the end of file is reached, or "stop" is entered, it will stop, and every line input will be nicely stored in our vector.
Next, we call reverse_copy
to copy everything in our vector (each line of input) to the standard output, separated by newlines, but all done backwards, so we end up with every line in the reverse order.
And that's it! Once the Line
class has been made, the algorithm for getting line inputs and reversing them can stay the same as if we were doing it with words, but the switch to lines only requires replacing a few instances of std::string
with Line
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.