Trying to reverse a string?

closed account (2w64izwU)
Hey I'm new and I'm trying to reverse this string but it says repeatpls::reverse does not take 2 arguments? It's in my header file.. I'm assuming I have to add something in the parentheses after repeatpls::reverse but im not sure what :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

class repeatpls
{
   public:
      void gettext();
      void reverse();

   private:
        std::string words;

};

void repeatpls::gettext()
{
   
   std::cout << "Enter text : ";
   std::cin >> words;

}

void repeatpls::reverse()
{
   reverse(words.begin(), words.end());
   std::cout << "Your reversed text : " << words;
}
It's referring to line 25. It doesn't know which reverse you're talking about.

-Albatross
closed account (2w64izwU)
Oh ok, so am I just missing something I forgot to put in?
Mhmm. You're missing two things.

I'm assuming you're intending to use std::reverse. It's a function found in the <algorithm> header and is in the std namespace. Does that give you an idea of what's missing?

-Albatross
closed account (2w64izwU)
Ok I added #include <algorithm> and added std:: on line 25 so it says

 
std::reverse(words.begin(), words.end());


After that it works! But it only reverses the first word I type and doesn't even show the rest? Is there something I can do about that so it reverses a whole sentence instead of just the first word?

Thanks for helping me too :)
Last edited on
Space is considered a delimiter.
So when you input more words separated by space, only the first one is read into your words string.

Instead of using bare std::cin, use std::getline() like so:

std::getline(std::cin, words);

http://www.cplusplus.com/reference/string/string/getline/

std::getline() will read all the words because by default it doesn't consider space a delimiter.
closed account (2w64izwU)
Oooh ok thanks! It works perfectly now! Thank you guys :)
Topic archived. No new replies allowed.