-1

My problem is that I get an error when trying to add hard-coded text, "334 " before my decoded user input:

 received = buf;
    if(strncmp(buf, "334 ", 4) == 0){
        decoding(received.c_str() + 4, strlen(buf + 4), buf);
        received = "334 " + buf;
    }

Here is the error I get:

invalid operands of types 'const char[5] and 'char[300] to binary 'operator+'

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
dragsl4y3r
  • 55
  • 9
  • 5
    You should make up your mind whether you are trying to write C or C++. – Jesper Juhl Apr 24 '18 at 17:07
  • Use `strncat()` or `strcat()` – Jazzwave06 Apr 24 '18 at 17:07
  • 6
    You're attempting to add two *pointers*, and that doesn't make much sense. It would make much more sense if you use [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) for all string variables in your code. – Some programmer dude Apr 24 '18 at 17:08
  • I don't need to use std::string because I have using namespace std; at the top of my code. – dragsl4y3r Apr 24 '18 at 17:10
  • 4
    @ms01249 When people say "use `std::string`" they mean use the standard string type. Whether you use `std::string` or `string` with `using namespace std;`, it's the same thing. People aren't saying that you should use `std::string` instead of `string`. Though, that would also be good advice. See [this question](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – François Andrieux Apr 24 '18 at 17:12
  • What type is `received`? If `received` is a pointer, `received = buf;` will make `received` point to `buf`, which is probably not what you want. – David Schwartz Apr 24 '18 at 17:12
  • 2
    @DavidSchwartz It must be `std::string`, since it has a `c_str()` method. – Barmar Apr 24 '18 at 17:13
  • 1
    Let me rephrase my comment: You should use [the standard library string class](http://en.cppreference.com/w/cpp/string/basic_string). – Some programmer dude Apr 24 '18 at 17:13
  • @Barmar The code doesn't compile, so we don't know that for sure. – David Schwartz Apr 24 '18 at 17:13
  • received is a string, buf is a char array – dragsl4y3r Apr 24 '18 at 17:13
  • received = buf; works perfectly fine and decodes the output for me. But I want to add "334 " to the beginning of that decoded text. – dragsl4y3r Apr 24 '18 at 17:14
  • 1
    To preform concatenation with `operator+` you need an intervening `std::string`. You can't directly add `char*` or `char[]` to concatenate. Using `received` would work. Try `received = "334"; received += buf;`. – François Andrieux Apr 24 '18 at 17:15
  • Your logic looks bogus, if `buff` starts with "334 " put `"334 " + buff` into received (and buff srtarts with "334 " already) – Slava Apr 24 '18 at 17:15
  • I skip this because I am decoding more than one message. I skip "334 " and "330 " to decode the desired string. – dragsl4y3r Apr 24 '18 at 17:18
  • great advice @FrançoisAndrieux ! That worked! – dragsl4y3r Apr 24 '18 at 17:20

2 Answers2

4

std::string has a constructor that takes a char *, so you can create a std::string from buf like this:

std::string buf_str(buf);

From cplusplus.com's list of std::string constructors:

string (const char* s);

Copies the null-terminated character sequence (C-string) pointed by s.

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
  • buf is a char array with a set max size of 300. I set received = buf before I decode, skip the first 4 characters, in this case, "334 " and decode. Then I set that new decoded string to received = buf; again. I want to add the "334 " back on after I decode. – dragsl4y3r Apr 24 '18 at 17:17
  • 3
    I'd suggest using the [cppreference](http://en.cppreference.com/w/cpp/string/basic_string/basic_string) explanation instead as it explains things a little more thoroughly (and is usually better than cplusplus.com from what I've seen). – scohe001 Apr 24 '18 at 17:18
  • 1
    @scohe001 I agree, but in this case, I thought the example from [cplusplus.com](http://www.cplusplus.com/reference/string/string/string/) was simpler, showing more clearly that there's a `std::string` constructor that can take a `char *`. – Ronan Boiteau Apr 24 '18 at 17:38
  • 1
    cplusplus.com trades a bit of accuracy for a lower barrier to entry. When preaching to the newb, that lower barrier can be very important. Face it, a lot of the time cppreference looks like Martian to the uninitiated. Right off the bat, the new programmer has to figure out what a `basic_string` is. Shouldn't be hard, but why is it even necessary? – user4581301 Apr 24 '18 at 17:50
0

I dunno if that can suit you but you can consider using the stringstream class so that you can merge the text variables as:

stringstream longone;
string text;
longone << received << "334 ";
string=longone.str()
Aldo_U
  • 5
  • 6