-1

My main function should be like this.

int main(int argc, char **argv) {
vector<string> v;
string s;
v.push_back(string("fee"));
v.push_back(string("fi"));
v.push_back(string("foe"));
v.push_back(string("fum"));
join(v, '/', s);
cout << s << '\n';
return 0;
}

My output should be: fee/fi/foe/fum

I did:

#include <vector> 
#include <iostream> 
#include <cstdlib>  


using namespace std;

void join(vector<string> v, char c , string s){

      for (int i=0; i<v.size(); i++)
       cout << v[i];
}

int main(int argc, char **argv) {   

vector<string> v;
string s;

v.push_back(string("fee"));
v.push_back(string("fi"));
v.push_back(string("foe"));
v.push_back(string("fum"));

join(v,'/',s);
cout<<s<<'\n';

return 0;

}

I cant printed '/' this char. How can ı do? How can I send char from main function to join function?

cout<

gugguk
  • 11
  • 5
  • 3
    You're "sending" it just fine. You're not doing anything with it. Where's the code that does anything with `c`? Also, what's the purpose of passing "s" to `join()`, when it also does absolutely nothing with it, either? You need to write all the code that your C++ program needs to do, C++ won't just do it for you. Your `join()` doesn't do anything with the parameter it gets, nor with the third parameter, either, so nothing happens to them. It is obvious that the third parameter must be a reference, so you need to fix this too. For more help, see your C++ textbook. – Sam Varshavchik Apr 16 '20 at 13:21
  • I believe the intent is that `join()` not use `cout`. so you are probably further away from finishing your assignment than you think. Be willing to take a few steps back and try a different approach. – JaMiT Apr 16 '20 at 13:25

1 Answers1

0

Firstly: How to change string in function

Secondly: you need to use and concat the c argument to your string.

Thirdly: escape early last iteration as to not add extra '/' at the end.

Here is a solution for you:

#include <vector> 
#include <iostream> 
#include <cstdlib>  

using namespace std;

void join(vector<string> v, char `c` , string & s){
    for (unsigned int i=0; i<v.size(); i++) {
        s += v[i];
        if (i >= (v.size() - 1)) {
            break; // escaping in the last iteration
        }
        s += c; // concatenating string
    }
}

int main(int argc, char **argv) {   
    vector<string> v;
    string s;

    v.push_back(string("fee"));
    v.push_back(string("fi"));
    v.push_back(string("foe"));
    v.push_back(string("fum"));

    join(v,'/',s);
    cout<<s<<'\n';

    return 0;
}

I honestly hate changing variables passed to a function / method. Much simpler and more readable is to return the result.