returning a string from a c++ function

I'm a beginner and I've been going through a book on C++, and I'm on a chapter on functions. I wrote one to reverse a string, return a copy of it to main and output it.

string reverseInput(string input);

int main()
    string input="Test string";
    return 0;

string reverseInput(string input)
    string reverse=input;
    int count=input.length();
    for(int i=input.length(), j=0; i>=0; i--, j++){
    return reverse;

The above seems to work. The problem occurs when I change the following code:

string input="Test string";


string input;

After this change, the reverse function returns only the reverse of the first inputted word, instead of the entire string. I can't figure out where I am going wrong.

Lastly, is there a more elegant way of doing this by using references, without making a copy of the input, so that the input variable itself is modified?


The problem is with cin. It stops reading after the first space character is read.

See the "cin and strings" section of this tutorial:

You can use getline(cin, input); to do what you want.

Need Your Help

Set Properties via runtime for Multiple Controls

c# winforms button

I have a form that contains about 25 Buttons. I want to set the same property for the Multiple buttons based on user input. The properties i want to change are,

When should I use RESTful?

web-services rest

I don't exactly know what RESTFUL is. Sure I know it's a mapping of a URL to a resource. But how's that different from an ajax call to a codeigniter URL which echos some JSON?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.