devolviendo una cadena de una función de c ++

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";
    //cin>>input;
    cout<<reverseInput(input);
    return 0;
}

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

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

string input="Test string";

a:

string input;
cin>>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?

preguntado el 08 de noviembre de 11 a las 14:11

Your reverse function has a bug in it. You should use i>0 para for condition. On the last iteration i==0 y j==input.length() Resultando en reverse[input.length()]=input[-1], both of which are out of bounds. -

9 Respuestas

El problema es con cin. It stops reading after the first space character is read.

See the "cin and strings" section of this tutorial: http://www.cplusplus.com/doc/tutorial/basic_io/

Puede usar el getline(cin, input); para hacer lo que quieras.

respondido 08 nov., 11:18

cin>>input; reads a word, not a line.

Utilice, por ejemplo, getline(cin, input); to read a line

respondido 08 nov., 11:18

cin >> input reads a word. To read an entire line you should use getline

getline(cin, input);

A debugger is very useful in this cases, you can just see the values of the variables stepping through the program.

A simple cout << input; would have helped you too but if you still don't have a good IDE with integrate debugger I would suggest you to use one. Eclipse is good and open source. Visual studio 2010 express is good and free if you are on windows.

respondido 08 nov., 11:18

Thanks for the input. I will start using a debugger. - Zubizaretta

Prueba esto: istream& getline ( istream& is, string& str );

It takes an entire line from a stream you give, e.g. cin and saves it into a string variable. Ejemplo:

getline(cin, input);

cin.getline(...) would work on C-style character buffers.

respondido 08 nov., 11:18

Inplace reverse function was already answered in detail here:

¿Cómo se invierte una cadena en su lugar en C o C ++?

contestado el 23 de mayo de 17 a las 15:05

This won't help in his case, as he still reads the string incorrectly. - Christian Rau

Agree, but getting only first word was already answered several times :-) - puntero

Ah sorry, I overread that he explicitly asked for an in-place version. - Christian Rau

El problema con tu código es que std::cin reads character till it encounters a character for which std::isspace(c) devoluciones true. So spaces and newlines are all such characters which returns true when passing to std::isspace.

So what you need basically is, std::getline:

std::string input;
if ( std::getline(std::cin, input))
{
    std::cout << reverseInput(input);
}
else
{
    std::cout <<"error while reading from standard input stream";
}

respondido 08 nov., 11:18

As for your question about references and copying:

string& reverseInput(string& input)
{
    for (i = 0, j = input.length()-1; i < j; i++, j--) 
    {
         char c = input[i];
         input[i] = input[j];
         input[j] = c;
    }
    return input;
}

You pass your argument as reference, and you return a reference. No copying involved, and in a body, you don't define any new string, you are working on the same instance.

respondido 08 nov., 11:18

This is not an error in your reverse function, but the standard behaviour of istream::operator>>, which only reads until the first whitespace character.

respondido 08 nov., 11:18

You need to use cin.getline(), cin >> s will only read the first word (delimited by space)

respondido 08 nov., 11:18

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.