-3

I wrote a program that reverses the string that is inputted by the user, but it doesn't work. I did it using string reverse_name(name.rbegin(), name.rend()) from Reverse the string in C++, but it doesn't work and gives me the error:

no viable conversion from 'std::__cxx11::basic_string<char,
      std::char_traits<char>, std::allocator<char> >::reverse_iterator' (aka
      'reverse_iterator<__normal_iterator<char *, std::__cxx11::basic_string<char> > >') to
      'std::__cxx11::string' (aka 'basic_string<char>')
  string reversed_word = (word.rbegin(), word.rend());

My code:

#include <iostream>
using namespace std;

int main()
{
  string word, reversed_word;
  cin >> word;

  reversed_word = (word.rbegin(), word.rend());
  cout << reversed_word;

  return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    That shouldn't even compile. It's missing `#include `, and even after adding that the assignment is wrong. – WhozCraig Apr 29 '19 at 05:53
  • 1
    You didn't do it like the answers in the other question suggest at all. Just compare the solutions on a syntactical level before asking. – StoryTeller - Unslander Monica Apr 29 '19 at 05:53
  • Perhaps you are trying to *initialize* `reversed_word` with a pair of iterators? – user7860670 Apr 29 '19 at 05:58
  • I am trying to create a reversed string – Yolanda Hui Apr 29 '19 at 05:59
  • You're not copying the answer from the other question properly. Initialise `reversed_word` after you've retrieved `word` from `cin`, so you have `std::string reversed_word(word.rbegin(), word.rend());` like the answer in the linked question says. – Tas Apr 29 '19 at 06:00
  • @YolandaHui The linked question has *multiple* ways of doing it. Dare I ask why do something different? [Yet another way](https://ideone.com/hsHbal). – WhozCraig Apr 29 '19 at 06:01
  • I have already intialised reversed_word after getting word from cin. – Yolanda Hui Apr 29 '19 at 06:08
  • Possible duplicate of [Reverse the string in C++](https://stackoverflow.com/questions/52964280/reverse-the-string-in-c) – Karthik Vg Apr 29 '19 at 06:14

2 Answers2

5

This line is wrong:

reversed_word = (word.rbegin(), word.rend());

The error message is self explanatory. Here is a simplified version to help make it easier for you to understand:

no viable conversion from 'reverse_iterator' ... to ... 'std::string'

You can't assign a (reverse) iterator to a string, but that is exactly what you are trying to do. The expression (word.rbegin(), word.rend()) does not construct a new string, like you are expecting. It simply evaluates the two iterators as-is, separated by the comma operator, which returns the value on the right side. So the line above is effectively the same as this:

reversed_word = word.rend();

To do what you are attempting, you need to pass the iterators to the std::string constructor instead. Either like this:

string reversed_word;
...
reversed_word = string(word.rbegin(), word.rend()); 

Or like this 1:

string reversed_word(word.rbegin(), word.rend());

1: as shown in this answer to the question you linked to, and even shown in your own question where you say "I did it using ... ".

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
-1

The easiest way to do it.

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
  string word, copy;
  cin >> word;
  copy = word

  reverse(word.begin(), word.end());
  cout << copy << endl;
  cout << word << endl;

  return 0;
}