-3

I have a snippet of code that takes the output of another function (that function gets user input) and replaces all spaces with dashes. Or, rather, that's what it's supposed to do. Instead, it takes the first word of the string and ignores the rest (e.g. 'Hello World' -> 'Hello'). Here is the snippet:

void info::name(string* name, string title){
    char arr[title.size() + 1];
    strcpy(arr, title.c_str());
    int o = 0;
    while(arr[o] != 0){
        if(arr[o] == ' '){
            arr[o] = '-';
        };
        o++;
    };
    *name = arr;

Is there any reason why this wouldn't work?

EDIT: What do you mean by combining C-style arrays and std::string?

EDIT2: I tried using std::replace, but the same thing happens.

EDIT3: I can't get getline() to work. Here is how I'm using it:

getline(cin, *title, "/n");

Why is this not working?

FINAL_EDIT: I finally got it to work! Here is what worked for me:

void info::title(string* title){
    cout << "Enter the name of your mod: ";
    getline(cin, *title); cout << endl;}
void info::name(string* name, string title){
    replace(title.begin(), title.end(), ' ', '-');
    *name = title;}

Once again, thanks, all!

2 Answers2

3

Here is a demonstrative program that shows how the effect you want can be achieved.

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

struct A
{
    static std::string name( const std::string &title )
    {
        std::string result;
        result.reserve( title.size() );

        std::replace_copy_if( title.begin(), title.end(),
                              std::back_inserter( result ),
                              []( char c ) { return c == ' '; }, '-' );

        return result;                            
    }
};

int main() 
{
    std::string title;
    std::string name;

    std::cout << "Enter a title: ";

    if ( std::getline( std::cin, title ) ) name = A::name( title );

    std::cout << name << std::endl;

    return 0;
}

If to enter string "Hello World"

then the output will look like

Enter a title: Hello World
Hello-World

I used the standard algorithm std::replace_copy_if only to show how to use lambda expressions. For example you could substitute also the tab character for the dash.

std::replace_copy_if( title.begin(), title.end(),
                      std::back_inserter( result ),
                      []( char c ) { return c == ' ' || c == '\t'; }, '-' );

Otherwise you can use standard algorithm std::replace_copy the following way

std::replace_copy( title.begin(), title.end(),
                   std::back_inserter( result ),
                   ' ', '-' );

As you see the task can be done without using an intermediate array. Moreover this declaration of an array

char arr[title.size() + 1];

is not C++ compliant declaration though some compilers can have their own extensions that allow such declarations..

Also there is a typo in your function. I think you mean

*name = arr;

instead of

*name = title;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Is there any reason why this wouldn't work?

Yes you create temporary array, modify it and then assign unmodified original string to string pointed by first argument.

You do not need temporary array at all and passing a pointer is not a C++ way to do it:

std::string info::name( std::string title )
{
    std::replace( title.begin(), title.end(), ' ', '-' );
    return title;
}

if you do want to use loop (required by homework etc) just do it on string itself:

std::string info::name( std::string title )
{
    for( size_t i = 0; i < title.length(); ++i ) {
        if( title[i] == ' ' )
            title[i] = '-';
    }
    return title;
}
Slava
  • 43,454
  • 1
  • 47
  • 90