Reversing multiple inputs

Program Challenge:
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Done", "done", or "d" for the line of text.

My question:
So far I have been able to output the reverse of the first line and end the code but how do I check and reverse the next line of input?

How it should look like:
Input:
Hello there
Hey
done

Output:
ereht olleH
yeH

My Current Output:
ereht olleH

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main() {
   string inputText;
   int i; 
   
   getline(cin, inputText);
   
   for ((i = (inputText.length() - 1)); i >= 0; i--)
   {
      cout << inputText[i]; 
   }
   
   
   if ((inputText == "done") || (inputText == "Done") || (inputText == "d") ){
      cout << endl; 
   }

   return 0;
}
Last edited on
You need to use a loop.

1
2
3
4
5
6
string inputText;
while(inputText != "Done" && inputText != "done" && inputText != "d")
{
  // read you text and print it reversed
}
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

int main() {
    std::string inputText;

    for (std::string inp; std::getline(std::cin, inp) && inp != "Done" && inp != "done" && inp != "d"; std::cout << '\n')
        std::copy(inp.rbegin(), inp.rend(), std::ostream_iterator<char>(std::cout, ""));
}

Thanks again @ thmm
---
here's the updated program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main() {
   string inputText;
   int i; 
   
   getline(cin, inputText);
   
   while (inputText != "Done" && inputText != "done" && inputText != "d"){
           for ((i = (inputText.length() - 1)); i >= 0; i--){
                 cout << inputText[i];
            }
            cout << endl;
            cin >> inputText;
   }
   
   while ((inputText == "done") || (inputText == "Done") || (inputText == "d") ){
      break;
   }

   return 0;
}
You can simplify by tidily piling a lot into the while decision making:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main() {
    string inputText;
    
    while (
           cout << "Enter a line of text ... bla, bla to quit: "
           &&  getline(cin, inputText)
           && (inputText != "Done" && inputText != "done" && inputText != "d")
           )
    {
        
        for ( int i = inputText.length() - 1; i >= 0; i--)
        {
            cout << inputText[i];
        }
        cout << endl;
    }
    
    return 0;
}



Enter a line of text ... bla, bla to quit: bla bla bla
alb alb alb
Enter a line of text ... bla, bla to quit: Don't quit yet
tey tiuq t'noD
Enter a line of text ... bla, bla to quit: Done
Program ended with exit code: 0
@seaminusminus - what's the purpose of L18-19 ?

L8 uses getline(), but L15 uses >> ??
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
   vector<string> term = { "Done", "done", "d" };
   for ( string str; getline( cin, str ) && find( term.begin(), term.end(), str ) == term.end(); )
      cout << string( str.rbegin(), str.rend() ) << '\n';
}
Topic archived. No new replies allowed.